简体   繁体   中英

Receive data posted with ajax

I have this code for sending data with ajax to a update.php page

$(document).ready(function() {
  $("#modify").click(function() {
    var a = $("#a").val();
    var b = $("#b").val();
    var c = $("#c").val();
    $.ajax({
      type: "POST",
      data: {
        a: 'a',
        b: 'b',
        c: 'c',
        id: 'id'
      },
      url: "update.php",
      success: function(result) {

      }
    });
  });
});

In the update page, I receive data like this

id = $_POST["id"];
a = $_POST["a"];
b = $_POST["b"];
c = $_POST["c"];

Is it correct or does it have a problem, because that doesn't work.

That works very well, but i think you wrote the strings instead in the variables 'data' object.

 var a = $("#a").val();
    var b = $("#b").val();
    var c = $("#c").val();
    $.ajax({
      type: "POST",
      data: {
        a: a,
        b: b,
        c: c,
        id: id
      },

I checked your code and it worked well, I suggest check the url again

a good job for finding error is adding console.log() to the success part, like this:

     url: "update.php", //check it 
      success: function(result) {
           console.log(result);

         // or you can do this instead of console.log(result)
        // alert(result)

       }

and also put this state in your php code :

<?php
   print_r($_POST);
?>

then after clicking on the button you can see the result, on the console of browser

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM