需要解决方案“注意:未定义的索引:在PHP中提交$ _POST ['submit']”

时间:2017-04-16 16:36:01

标签: html5 forms twitter-bootstrap post php-5.5

我有HTML代码,用于将form数据发布到database

`  <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" class="form-horizontal" id="createform">
                      <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">Name</label>
                        <div class="col-sm-10">
                          <input type="text" name="name" class="form-control" id="name" placeholder="Name">
                        </div>
                      </div>
                      <div class="form-group">
                        <label for="address" class="col-sm-2 control-label">Address</label>
                        <div class="col-sm-10">
                          <textarea name="address" id="address" cols="30" rows="3" placeholder="Address"></textarea>
                        </div>
                      </div>
                       <div class="form-group">
                        <label for="contact" class="col-sm-2 control-label">Contact</label>
                        <div class="col-sm-10">
                          <textarea name="contact" id="contact" cols="30" rows="3" placeholder="Contacts"></textarea>
                        </div>
                      </div>
                      <div class="form-group">
                        <label for="contactemail" class="col-sm-2 control-label">Email</label>
                        <div class="col-sm-10">
                          <input type="email" name="emailcontact" class="form-control" id="contactemail" placeholder="Email">`enter code here`
                        </div>
                      </div>

                      <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                          <input type="submit" class="btn btn-default" value="Create" name="save"/>
                        </div>
                      </div>
                    </form>`

PHP以下HTML Form的代码:

` if($_POST['save'] == "Create"){
        $current_user = intval($_SESSION['id']);
        $query = "insert into addresses(name,address,contact,emailcontact,user_id) values('".$_POST['name']."','".$_POST['address']."','".$_POST['contact']."','".$_POST['emailcontact']."','".$current_user."')";
        mysqli_query($conn, $query);
        header("Location: addressbook.php");
    }`

我的问题是,每当我通过点击Post按钮尝试Create带有数据的表单时,warning下面显示的是web page

`Notice: Undefined index: save in C:\xampp\htdocs\working_files\Online_Address_Book_PHP\php\utility.php on line 9

Notice: Undefined index: delete in C:\xampp\htdocs\working_files\Online_Address_Book_PHP\php\utility.php on line 21
`

在我的网站中有很多$_POST['--']个数据。并且对于all of them我的browser正在显示此warning messages。我怎样才能克服这个问题?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

的!我自己解决了这个问题。只需在每个code之前使用check

`if(isset($_POST['save'])){
.......
}`

我的代码解决方案如下:

`if(isset($_POST['save'])){
if($_POST['save'] == "Create"){
        $current_user = intval($_SESSION['id']);
        $query = "insert into addresses(name,address,contact,emailcontact,user_id) values('".$_POST['name']."','".$_POST['address']."','".$_POST['contact']."','".$_POST['emailcontact']."','".$current_user."')";
        mysqli_query($conn, $query);
        header("Location: addressbook.php");
    }
}`