3

I have seen few posts in SO related to the same scenario as mine but did not find a proper resolution. So am posting question with my problem stuff.

I have an HTML form

 <form method="post" id="myForm">
      <label for="e_name">Name</label>
      <input name="e_name" id="emp_name" value="" type="text" data-theme="a">
      <label for="date">Date</label>
      <input name="date" id="emp_dob" value=""  data-theme="a">
      <label for="gender">Gender</label>
      <select name="gender" id="emp_gender" data-role="slider" data-theme="a" data-inline="true">
        <option value="male">Male</option>
        <option value="female">Female</option>        
      </select>
      <label for="address">Address</label>
      <textarea name="address" id="emp_address" value="" type="text" data-theme="a"></textarea><br><br>      
      <input type="button" id="insert" value="Submit">      
    </form>

  <div id="someElement"></div>

And I have the following to perform my form elements submission to a PHP page-

$(document).ready(function(){ 
    $("#insert").click(function(e) { 
    e.preventDefault();
    alert("am in the Insert function now");
        $.ajax({
        cache: false,
        type: 'POST',
        url: 'insert.php',
        data: $("#myForm").serialize(),
        success: function(d) {
            $("#someElement").html(d);
        }
        });
    }); 
});

Here is my PHP -

 <?php
    $con=mysqli_connect("localhost","root","root","employee");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
      $name =" ";
      $dob =" ";
      $gender =" ";
      $address =" ";

        if(isset($_POST['emp_name'])){ $name = $_POST['emp_name']; }
        if(isset($_POST['emp_dob'])){ $dob = $_POST['emp_dob']; }
        if(isset($_POST['emp_gender'])){ $gender = $_POST['emp_gender']; }
        if(isset($_POST['emp_address'])){ $address = $_POST['emp_address']; }

        echo $name;
        echo $dob;
        echo $gender;


echo $address;


$sql="INSERT INTO emp_details (emp_name, emp_gender, emp_address) VALUES ('$name', '$gender', '$address')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }

echo "1 record added";

mysqli_close($con);
?> 

Now what happens is, when I enter some values in my form and click on Submit, action performs well and a new row inserts in the database. But all the rows are empty. They're just empty, not even "NULL". I tried to echo my field values in the PHP but there is no output there. My "1 Record Added" has come-up well, but no form values appear here.

Kindly help me sorting this out.

Thanks in advance.

2
  • var_dump($_POST); in your php file & check you actually getting data when your form is submitting ? Also it seems you are passing serialize data from ajax call. Commented May 2, 2013 at 8:52
  • This code vulnerable to hack through sql injection. Use prepared statements and sanitize all data from user. Commented May 2, 2013 at 8:55

2 Answers 2

1

$_POST[] references to the name attribute of html-tag, not id.

For example, $_POST['emp_name'] should be $_POST['e_name']

Furthermore, don't encapsulate your variables with single quotes:

"INSERT INTO emp_details (emp_name, emp_gender, emp_address) VALUES ('$name', '$gender', '$address')";

Do this instead:

"INSERT INTO emp_details (emp_name, emp_gender, emp_address) VALUES ('" . $name . "', '" . $gender . "', '" . $address . "')";

Or use bind_param() from mysqli ofcourse!

Sign up to request clarification or add additional context in comments.

5 Comments

Done.. I made a silly mistake ... considered ID instead of name... am so dumb, I forgot this basic. It's been more than 3 years I worked on this stuff.. thanks for reminding me that I need more basics strength. :) :)
You're welcome. I infer from this comment you've managed to solve your question?
may I ask you for any good and easy/quick tutorials available online for PHP?
Well this depends on what you're trying to accomplish. PHP is used for a numerous purposes. Though it's a well documented language. In any case PHP.net is your best friend.
@user1397891 I don't know how fell advanced you are, but in case you're interested W3Schools has some excellent examples, well implemented.
1

Make the id and name of your input elements same

<form method="post" id="myForm">
      <label for="e_name">Name</label>
      <input name="emp_name" id="emp_name" value="" type="text" data-theme="a">
      <label for="date">Date</label>
      <input name="emp_dob" id="emp_dob" value=""  data-theme="a">
      <label for="gender">Gender</label>
      <select name="emp_gender" id="emp_gender" data-role="slider" data-theme="a" data-inline="true">
        <option value="male">Male</option>
        <option value="female">Female</option>        
      </select>
      <label for="address">Address</label>
      <textarea name="emp_address" id="emp_address" value="" type="text" data-theme="a"></textarea><br><br>      
      <input type="button" id="insert" value="Submit">      
    </form>

Otherwise change your $_POST array keys.Because you will get the keys of $_POST array will be the name of input elements.But i recommend you to mak ethe name and id same

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.