Bootstrap - 自定义表单

I am using Bootstrap. I am trying to create a working form, but the submit function doesn't work. Here is my form HTML:

                <form class="form-horizontal" role="form" action="mph.php" method="POST">
                    <div class="modal-body">
                            <fieldset>

                            <!-- Form Name -->
                            <legend>Map Problem Reporter</legend>

                            <!-- Select Basic -->
                            <div class="form-group">
                              <label class="col-md-4 control-label" for="selectbasic">What map?</label>
                              <div class="col-md-6">
                                <select id="selectbasic" name="selectbasic" class="form-control">
                                  <option value="1">6 Stages of Parkour</option>
                                  <option value="2">20 Stages of Parkour</option>
                                </select>
                              </div>
                            </div>

                            <!-- Multiple Radios -->
                            <div class="form-group">
                              <label class="col-md-4 control-label" for="radios">What kind of problem?</label>
                              <div class="col-md-4">
                              <div class="radio">
                                <label for="radios-0">
                                  <input type="radio" name="radios" id="radios-0" value="1">
                                  Blocks
                                </label>
                                </div>
                              <div class="radio">
                                <label for="radios-1">
                                  <input type="radio" name="radios" id="radios-1" value="2">
                                  Commands / Command Blocks
                                </label>
                                </div>
                              <div class="radio">
                                <label for="radios-2">
                                  <input type="radio" name="radios" id="radios-2" value="">
                                  Entities / Items
                                </label>
                                </div>
                              <div class="radio">
                                <label for="radios-3">
                                  <input type="radio" name="radios" id="radios-3" value="">
                                  Other (describe in the next text field)
                                </label>
                                </div>
                              </div>
                            </div>

                            <!-- Textarea -->
                            <div class="form-group">
                              <label class="col-md-4 control-label" for="textarea">Describe the Problem</label>
                              <div class="col-md-4">                     
                                <textarea class="form-control" id="textarea" name="textarea"></textarea>
                              </div>
                            </div>

                            <!-- Select Basic -->
                            <div class="form-group">
                              <label class="col-md-4 control-label" for="selectbasic">What is the priority?</label>
                              <div class="col-md-6">
                                <select id="selectbasic" name="selectbasic" class="form-control">
                                  <option value="1">Huge! It breaks the map!</option>
                                  <option value="2">Big</option>
                                  <option value="">Noticeable</option>
                                  <option value="">Barely Noticeable</option>
                                  <option value="">Almost Hidden</option>
                                  <option value="">I do not know</option>
                                </select>
                              </div>
                            </div>
                            <input type="submit" name="submit" value="Save Data">
                            </fieldset>
                        </form>

And here is my current PHP:

<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "
";
$ret = file_put_contents('mpr.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
    die('There was an error writing this file');
}
else {
    echo "$ret bytes written to file";
}
}
else {
   die('no post data to process');
}
?>

When I click "Save Data" in the form, it doesn't update my mpr.txt with the information. Please help.

ds342222
ds342222 不,我删除并重新创建线程的重点是删除答案和注释(如果你愿意的话,开始“用干净的石板”)。
5 年多之前 回复
duaner5714
duaner5714 欢迎您改进您的问题,但现在删除这些只会浪费那些试图帮助您的人的时间。
5 年多之前 回复
douren7179
douren7179 不要让他们看到这一点,我不会“惩罚”未来的人。我将重新制作线程(一旦删除),并改变一些事情。
5 年多之前 回复
dongwei2983
dongwei2983 人们给了他们时间来帮助你。请不要通过删除或破坏这些来惩罚他们并阻止未来的访问者学习。
5 年多之前 回复
dstnlhhv791576
dstnlhhv791576 答案很明显!PHP正试图找到发布的field1和field2。但是在HTML表单中你没有定义这两个属性,因此PHP不会更新文件!
5 年多之前 回复
duanmao1319
duanmao1319 1.哪个提交功能?2.表格中的“保存数据”是什么?还添加了att。表单标签的方法如:<formmethod=“post”>
5 年多之前 回复

2个回答

I see 2 problems:

1) You have created 2 forms with

<form class="form-horizontal" role="form">

and

<form class="form-horizontal">

which should be 1

and

2) You are missing the action attribute in <form> tag i.e. it should be <form class="form-horizontal" role="form" action="<php file name>" method="<request type>">

correct these and I think you should be through.

You do not see the data in your .txt file because you do not have any field named field1 and field2 in your html page. The field names have to match in both html and php files to write/read the data correctly. Please correct the names and you should be through.

Also would appreciate if you accept the answer.

drdyf42880
drdyf42880 修改了我的答案
5 年多之前 回复

The answer is quit obvious! PHP is trying to find field1 and field2 posted. But in your HTML form you did not define these two properties, consequently PHP won't update the file!

EDIT:

If you submit the form and print_r($_POST) you will get the follow array:

Array ( [selectbasic] => 1 [textarea] => [submit] => Save Data )

As you see, there is no [field1] or [field2] in the response!

Which means PHP cannot proceed to to open and update the text file!

To prevent this, you should define field1 and field2 in your HTML form by adding these input tags or updating the existing ones.

For instance, if you are interested in the select tag and the textarea tag, you should whether update you HTML code or you php code.

You could go with PHP code by updating your if statement into if(isset($_POST['selectbasic'], $_POST['textarea'])) or in your HTML form change the name="selectbasic" into name="field1" and name="textarea" into name="field2 and keep your current php code and so your current PHP if statement that says: if(isset($_POST['field1']) && isset($_POST['field2']))

I hope this explains it all!

dongya1875
dongya1875 如果您觉得我的答案有帮助,请不要忘记将其标记为您问题的解决方案。 它适用于我,我已在我的服务器上测试过它。
5 年多之前 回复
douya2433
douya2433 你可以给我发电子邮件:karimwaiyl@gmail.com
5 年多之前 回复
douyinbo3361
douyinbo3361 只需更新这些行并使用以下代码即可使用:if(isset($ _ POST ['selectbasic'])&& isset($ _ POST ['textarea'])){$ data = $ _POST ['selectbasic']。 ' - '。 $ _POST ['textarea']。 “ ” 个; $ ret = file_put_contents('mpr.txt',$ data,FILE_APPEND | LOCK_EX); 我已经测试了它,它在我的服务器上工作。 PS:确保mpr.txt与mph.php文件位于同一目录中。
5 年多之前 回复
douyan6958
douyan6958 你也改变了这一行:$ data = $ _POST ['field1']。 ' - '。 $ _POST ['field2']。 “ ” 个; 进入$ data = $ _POST ['selectbasic']。 ' - '。 $ _POST ['textarea']。 “ ” 个; ?
5 年多之前 回复
doumei4964
doumei4964 不,那不是它! 当你提交表单时,PHP会在响应中查找$ _POST ['field1']和$ _POST ['field2']! 在您的代码中,您没有定义name =“field1”和name =“field2”
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问