1

When I try to submit data via AJAX to a PHP file, the submit works, and I can echo back a message from the PHP file. However, when I try to echo back the data I am submitting, or, echo back a message confirming that the data matches another variable in the PHP file(which it does), I still get a success message, but it says the data is not the same.

I am new to development and struggling a bit. Any help would be super.

The AJAX

$.ajax({
    type: "POST",
    url: "/check-cust.php",
    data: "1234",
    success: function(data) {
        if (data == "") {
            console.log("success, but no return");
        } else {
            alert(data); // show response from the php script.
        }
    },
    error: function() {
        alert("not working");
    }
});

The PHP

$temp_cust_id = "1234";

$data = $_POST['data'];

if ($data == $temp_cust_id) {
    echo "it works";
} else {
    echo "it doesnt work";
}

Is it maybe because I am submitting a JSON array, which is different to the string variable? That's just a guess though!

2
  • 1
    if youre unsure about your code, check each step separately to be sure in which line is really the issue, in your case, look how many things need to go right to get the right result in your 'controller'. There must be data in _POST, it must be in data property, then it must equal the other variable... The fact you dont get 'it works', doesn't mean you didnt get the data... use var_dump or print_r to learn how ex. _POST looks like just after you can read it, I bet it will help a lot... Commented Oct 23, 2015 at 8:23
  • Thanks everyone. Bit of a thick mistake from my side. Really appreciate the fast assistance Commented Oct 23, 2015 at 8:23

4 Answers 4

2

the way you are sending data is wrong,

data: {"data","1234"}
        ^      ^
        name    value

To put it all togeather,

$.ajax({
    type: "POST",
    url: "/check-cust.php",
    data: {data:"1234"},// this the line in which you have error.
    success: function(data) {
        if (data == "") {
            console.log("success, but no return");
        } else {
            alert(data); // show response from the php script.
        }
    },
    error: function() {
        alert("not working");
    }
});

Then in php file,

$temp_cust_id = "1234";

$data = $_POST['data'];

if ($data == $temp_cust_id) {
    echo "it works";// it will echo this for current output.
} else {
    echo "it doesnt work";
}
Sign up to request clarification or add additional context in comments.

Comments

2

This false data: "1234",, should be :

data: {
 data : 1234
}

Or give a better name :

data: {
  myData : 1234
}

And in server side :

$_POST['myData'];

Another way by using query string like so :

data : 'myData=Hello',
// or data : 'firstParam=Hello&secondParam=Mate',

Comments

1

For comparing the string strcmp

AJAX

data: {"data":"1234"}

PHP

if (!strcmp($data,$temp_cust_id))
    echo "it works";

Comments

1
post_data='1234';

 $.ajax({
    type: "POST",
    url: "/check-cust.php",
    data: "post_data="+post_data, //-------->>>Pass the data like this
success: function(data) {        
  if (data == "") {  
  console.log("success, but no return");


   } else {


  }

And in php file use:

$_POST['post_data'];

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.