Search code examples
htmlformstwitter-bootstrappostphp-5.5

Need solution for "Notice: Undefined index: submit in in PHP $_POST['submit']"


I've this HTML code for posting a form data to the 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 Code for this HTML Form below:

` 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");
    }`

My Problem is Whenever i'm trying to Post the form with data by clicking the Create button, This below warning is showing in my 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
`

In my site there is many $_POST['--'] data. and for all of them my browser is showing this warning messages. How can i overcome this problem? Any help is appreciated.


Solution

  • Wow! I've solved this problem by my own. Just use this code before every check:

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

    MY code solution is this below:

    `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");
        }
    }`