duanbei1598 2014-11-15 22:33
浏览 185

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.

  • 写回答

2条回答 默认 最新

  • duandi6531 2014-11-15 22:45
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)