0

I have a problem. I want to send 3-4 variables from a PHP File to my AJAX request (over json)... and I'm sure that my code is right, it doesn't work. It is doing nothing. If I'm doing just a normal "echo" everything works finde, but json is not working?

Here is my JS-code:

$.ajax({
       type: "POST",
       url: "test.php",
       data: "varA=" + varA + "&varB=" + varB,
       dataType: json,
       cache: false,
       success: function(data1){

       alert(data1.b);

       if (data1.a == "success"){
       alert("OK");

       location.href = "index.html";
       }

       else{
       alert("Not OK");
       }

       }
       });

And here is my PHP-code:

...
    $qry="SELECT * FROM database1 WHERE varA='".$_POST['varA']."' AND varB='".$_POST['varB']."'";
$result=mysql_query($qry);

if($result) {
    if(mysql_num_rows($result) == 1) {
        $test = mysql_fetch_assoc($result);
        echo json_encode(array('a' => 'success', 'b' => $test['database_entry']));
...

I don't have a clue why this AJAX code would not be fired! Hope you could help me, THANKS!

7
  • you data is not in the correct json format. it must be in name value pair Commented May 7, 2012 at 13:16
  • define "is not working" output the error message, also what did you debug with? Chrome inspector? firebug? etc; Commented May 7, 2012 at 13:17
  • I hope that you escape $_POST['varA'] and $_POST['varB']! Please use mysql_real_escape_string($str) in order to prevent executing any SQL commands by a hacker. Commented May 7, 2012 at 13:17
  • @Jakub: it's doing nothing. i'm working in xcode, and with my debugging method it is not even going into the ajax-request. Commented May 7, 2012 at 13:25
  • 1
    Even better than the old mysql_* functions, either use mysqli_* or change to PDO, the old mysql_* functions are going to be deprecated, and they are more vulnerable than using PDO Commented May 7, 2012 at 13:28

3 Answers 3

2

Send your data as a JSON object, not a self-generated query string:

data:  {"varA":  varA, "varB": varB},
dataType: json,
cache: false,
Sign up to request clarification or add additional context in comments.

1 Comment

thanks. but that is also not working. is it in the php file possible to get the variables like $bla = $_POST['varA']; or should I do that in another way?
1

You are maiking a mistake by writing

dataType: json,

here json is supposed to be in a string

dataType: 'json',

In your code it is trying to search for the variable json which is not available and hence undefined, and hence no ajax call is made

2 Comments

maaaaaan (or woman), thanks. now it works like a charm. thank you!
man it is. glad it helped..., please use firebug in firefox, chrome inspector where it clearly mentions what the error is.. and you can simply fix it... all the best for future prospects
1

The data you are sending to your ajax call is a string, wheras it should be an object or an array.

data: "varA=" + varA + "&varB=" + varB,

should be

data: {"varA":varA,"varB":varB},

2 Comments

thanks. but that is also not working. is it in the php file possible to get the variables like $bla = $_POST['varA']; or should I do that in another way?
try to print_r($_REQUEST) , to see if the variables pass in the first place.

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.