0

I'm running an AJAX call that passes some variables to a PHP script which is supposed to INSERT into a table. But it doesn't work ?

Code :

    <?php
$cn = mysql_connect('localhost','root','');

if ($cn) {
  mysql_select_db('test',$cn);
}

if (isset($_POST['saverecord'])) {
  $name = mysql_real_escape_string($_POST['name']);
  $gender = mysql_real_escape_string($_POST['gender']);
  $phone = mysql_real_escape_string($_POST['phone']);

  mysql_query('INSERT INTO `ajax_php` (name,gender,phone) VALUES ($name,$gender,$phone)');
  echo "IT WORK";
  exit;
}

 ?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title><?php echo "Ajax + PHP Tutorial"; ?></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  </head>
  <body>
    <table id="data">
      <tr>
        <td>
          Name :
          <input type="text" name="name" id="name">
        </td>
        <td>
          Gender :
          <select id="gender" name="gender">
            <option value="0">Male</option>
            <option value="1">Female</option>
          </select>
        </td>
        <td>
          Name :  <input type="text" name="phone" id="phone">
        </td>
      </tr>
    <input type="button" value="save" id="save">
    </table>
  </body>
</html>

<script type="text/javascript">
  $(function(){
    // Save Data
    $('#save').click(function(){
      //get values
      var name = $('#name').val();
      var gender = $('#gender').val();
      var phone = $('#phone').val();

      alert(name);
      $.ajax({
        url: 'index.php',
        type: 'POST',
        async: false,
        data: {
          'saverecord' : 1,
          'name': name,
          'gender': gender,
          'phone': phone
        },
        success: function(re) {
          if (re==0) {
            alert('success');
            $('#name').val('');
            $('#gender').val('');
            $('#phone').val('');
          }
        }
      });
    });
    // End
  });
</script>

Also it doesn't display IT WORK, but Success is alerted. I think that the problem is located in the given javascript code ...

1
  • Any errors provided? Do you log the HTTP requests being sent? (E.g., see the network tam in Chrome DevTools.) If you assume the problem is in javascript side, did you try to log the response into console? (Using console.log(..).) Commented Dec 20, 2015 at 16:21

2 Answers 2

1

Problem in PHP Code

You need to use double quotes in the query SQL so that the php variables are parsed properly and again you need single quotes around values.

mysql_query("INSERT INTO `ajax_php` (name,gender,phone) VALUES ('$name','$gender','$phone')");

Note: mysql_* functions are deprecated, so consider moving to mysqli_* functions ASAP.

Problem in JS Code

Your success function alerts success, doesn't alert the response from the server.

   success: function(re) {
      alert(re); // it will alert the response from the server.
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks , But it doesn't work ... like you see in the code I add echo "IT WORK"; to test if it work or no ... But it doesn't show any thing .. so the if statement is always false that's why i think that the problem is in the js code :)
0
mysql_query('INSERT INTO ajax_php (name, ...) VALUES ($name, ...)');

You need to use double quotes. Otherwise everybody is called $name in your data base because your variables are not evaluated.

2 Comments

Use prepared statements is the right answer here.. in addition to the fact that mysql_ functions are deprecated (and removed in PHP7) and using them in any newly written code is simply stupid.
Thanks , But it doesn't work ... like you see in the code I add echo "IT WORK"; to test if it work or no ... But it doesn't show any thing .. so the if statement is always false that's why i think that the problem is in the js code :)

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.