0

There is a page called form.html .It directs to the page addtodatabase.php at the end there is submit button .

    <form action="addtodatabase.php" method="post">
    <form class="form-inline">
    <fieldset>
    <legend>Security Department User Registration</legend>
    <div class="form-group">
    <label for="Firstname">First Name</label>
    <input type="text" class="form-control" id="Firstname" name="firstname" placeholder="Text input"><br/>
    </div>

    <div class="form-group">
    <label for="Secondname">Second Name</label>
    <input type="text" class="form-control" id="Secondname" name="secondname" placeholder="Text input"><br/>
    </div>
    </form>

My addtodatabase.php page .

    $connect=mysqli_connect('localhost','root','','form_db');
    if(mysqli_connect_errno($connect))
    {
    echo 'Failed to connect:'.mysqli_connect_error();
    }
    $firstname=""; 
    $secondname="";
    if (isset($_POST)) { 
    $firstname  = isset($_POST['firstname']) ? $_POST['firstname'] : '';
    $secondname = isset($_POST['secondname']) ? $_POST['secondname'] : '';
    echo 'Your first name is ' .$firstname. '<br>'; 
    echo 'Your second name is ' .$secondname. '<br>'; 
    }

There are three errors

it doesn't get to the page addtodatabase.php. http://localhost:8080/form/form.html?
Notice: Undefined index: firstname in C:\wamp64\www\Form\addtodatabase.php on line 12. nothing is being added to database. only id is increment

Thanks in advance.

  • 1
    i think you missed submit button.. – sunilwananje Mar 07 '17 at 05:16
  • 1
    If you don't get there how do you get the error/notice? Why assign `$firstname=$_POST['firstname'];` twice? – chris85 Mar 07 '17 at 05:16
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – chris85 Mar 07 '17 at 05:23
  • $firstname=$_POST['firstname'];//trying to initialize the variable $secondname=$_POST['secondname'];//trying to initialize the variable if (isset($_POST)) { $firstname=$_POST['firstname']; $secondname=$_POST['secondname']; – Eiman Sadath Assadi Mar 07 '17 at 05:55
  • Initialize the variables with empty values. – chris85 Mar 07 '17 at 12:57

3 Answers3

0

Note- Check isset before get data.

Try this

 $connect=mysqli_connect('localhost','root','','form_db');

    if(mysqli_connect_errno($connect))
    {
    echo 'Failed to connect:'.mysqli_connect_error();
    }

    if (isset($_POST)) { 
    $firstname=$_POST['firstname'];
    $secondname=$_POST['secondname'];


    echo 'Your first name is ' .$firstname. '<br>'; 
    echo 'Your second name is ' .$secondname. '<br>'; 
    }
shubham715
  • 3,324
  • 1
  • 17
  • 27
0

is addtodatabase.php in the same folder as form.html?

if html is in c:\wamp64\www and addtodatabase.php is in C:\wamp64\www\Form\ then change the html to;

<form action="Form/addtodatabase.php" method="post">
<form class="form-inline">
<fieldset>
<legend>Security Department User Registration</legend>
<div class="form-group">
<label for="Firstname">First Name</label>
<input type="text" class="form-control" id="Firstname" name="firstname" placeholder="Text input"><br/>
</div>

<div class="form-group">
<label for="Secondname">Second Name</label>
<input type="text" class="form-control" id="Secondname" name="secondname" placeholder="Text input"><br/>
</div>
</form>
<input type="submit" value="submit">
</form>
Komal Bandi
  • 67
  • 1
  • 7
0

Before you access the post value, you had better check if it exists. So use

$firstname  = isset($_POST['firstname']) ? $_POST['firstname'] : '';
$secondname = isset($_POST['secondname']) ? $_POST['secondname'] : '';

to replace

$firstname=$_POST['firstname'];
$secondname=$_POST['secondname'];

edit:

$connect=mysqli_connect('localhost','root','','form_db');

if(mysqli_connect_errno($connect))
{
echo 'Failed to connect:'.mysqli_connect_error();
}


$firstname  = isset($_POST['firstname']) ? $_POST['firstname'] : '';
$secondname = isset($_POST['secondname']) ? $_POST['secondname'] : '';

echo 'Your first name is ' .$firstname. '<br>'; 
echo 'Your second name is ' .$secondname. '<br>'; 
}
LF00
  • 27,015
  • 29
  • 156
  • 295