Kavya Shree
Kavya Shree

Reputation: 1032

AJAX Post values not received in PHP

Below is my ajax file. Here I passed all my text box values. And I post that values to edu.php. There I want to update two tables like details and test.

But nothing is updating in my database.While I checked the value with var_dump the string seems to be empty.But while passing from ajax I checked it with an alert it shows all the values in text box. So I believe problem is happening while posting from ajax to php.

AJAX Code

$('#edubackgroundsubmit').click(function (event) {
  event.preventDefault();
  alert("Hello");
  var per_email = $('#per_email').val();
  var master_overall = $('#master_overall').val();
  var master_pass_year = $('#master_pass_year').val();
  var master_college = $('#master_college').val();
  var master_univ = $('#master_univ').val();

  var data1 ="master_qual="+master_qual+"&master_overall="
  +master_overall+"&master_pass_year="+master_pass_year+"&master_college="+master_college+"&master_univ="+master_univ
  +"&edu_flag="+edu_flag;
  alert(data1);
  $.ajax({
    type:"POST",
    url: "edu.php?per_mobile="+per_mobile,
    dataString1: data1
  }).done(function( dataString1 ) 
          {
    alert(dataString1);
    $('#edu_alert').append(
      '<div class="alert alert-success text-center">' +
      '<button type="button" class="close" data-dismiss="alert">' +
      '&times;</button>' + dataString1 + '</div>');
  });
});     

PHP File

if (isset($_POST['pass_year_12'])) {
    $pass_year_12 = $_POST['pass_year_12'];
} else {
    $pass_year_12 = "";
}
$l1   = "UPDATE test
    SET  edu_flag='$edu_flag'
        WHERE  per_mobile='$per_mobile'";
$l2   = "UPDATE details
    SET master_qual='$master_qual',
        master_overall='$master_overall',
        master_pass_year ='$master_pass_year ',
        master_college='$master_college'
       WHERE per_mobile='$per_mobile'";
$exec = mysqli_query($link, $l1);
if (mysqli_query($link, $l2)) {
  echo "Education Details Updated Successfully";
} else {
  echo "Error updating record: " . mysqli_error($link);
}

Upvotes: 0

Views: 89

Answers (2)

Sonu Bamniya
Sonu Bamniya

Reputation: 1115

Problem with your code is hat you are sending the data at edu.php?per_mobile

so you need to add isset condition before the code... like this

if(isset($_REQUEST['per_mobile'])){ //write your code here }

and also replace datastring to data. and then try.

Hope it may help.

For instance you can also use jQuery get or post method, that is more reliable and easy to learn.

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

This is wrong. What's this bit:

dataString1: data1

You need to change it as:

data: data1

The function $.ajax() POSTs only those given in the data attribute.

Upvotes: 1

Related Questions