tags:

views:

63

answers:

2
$.ajax({
url: "_includes/contactform.php",
type: "POST",
data:'name=' + nameValue + "&email=" + emailValue + "&text=" + textValue + "&checkbox=" + checkValue + "&submit=true",  
dataType:"json",
success: function(data,result,res) {
alert(res);
console.log(data);
if(data.checkbox == "true"){
    $("#success h1").html("The form was sent successfully. Thank you for signing up for our newsletter.")
}

});

My script gets stuck when running.

here is the server return:

$json_array = array("checkbox" => $checkbox);
echo json_encode($json_array);
+1  A: 

Have a look in the Firebug Net tab to see what data is returned or if the server is returning a 500.

DLauer
+1  A: 

You can also look in the Firebug console and see what was sent and what was returned.

You may also want to have your contactform.php work with GET and test it from the browser, making certain that your script isn't having any problems.

Troubleshoot it one part at a time, and use firebug frequently. :)

Update: just looked at your javascript and the problem is in your success function.

Go to json.org, get a javascript file (look at the languages at the bottom of the page). Parse the data variable and get the json object. Then use that result in the javascript.

The problem is that you are assuming the json result is already a javascript object, and it is not. You need to convert it to one, either with using the eval function (very risky) or using a library to parse it.
Here is an example of what I am describing: http://www.factsandpeople.com/facts-mainmenu-5/26-html-and-javascript/89-jquery-ajax-json-and-php

or you can try this, which is $.getJSON http://docs.jquery.com/Ajax/jQuery.getJSON

James Black
i use safari's debugger. it comes back: {"checkbox":"false"}; it's like it's not getting returned correctly.
Phil
If you use Firebug on Firefox you can see what was actually returned in the request. If it has the json listed in your comment then it must be returning. Then it is a matter of how are you processing the result, what should be happening.
James Black
If your entire PHP script to handle this response is what you showed us and you're getting {"checkbox":"false"} as the server response, then it's because your value for $checkbox is 'false'. It also means you are assuming 'register_globals' is on (which, frankly, would be horrifying). I'm assuming a lot of things here, so straighten me out if I'm wrong so that I can help you more.
KyleFarris
@KyleFarris - The problem is in the javascript side, when he tries to get a property out of the data variable it will throw an error and exit the function, but continues to run, just doesn't execute the last line in the success() function.
James Black
@JamesBlack, you are correct. "False" (or "True") is what i'm trying to get back from the php script. It's like it's not returning the data formatted correctly. When I remove the line "dataType: json", then the scripts runs successfully, data is inserted in the database, but the return data is formatted as noted above.
Phil
What is returned is the JSON format, so you can then just parse it as I mentioned in my update, and for the moment just try var myObject = eval(data); if (myObject } else { alert('myObject is incorrect'); };
James Black
Sorry, i missed your edit. It gives me a parse error in my jquery.1.3.2.js file when i run the eval(data). Weird.
Phil
You may want to just return this: {"checkbox": "false"} That is the format for JSON, for an object. You need the quotes around the name and value. If the value is a number then you don't need the quotes.
James Black
{"checkbox": "false"}, is what is returned. I'm just having a problem accessing the "false".... THanks for all your help. If there is some other documentation you can point me to, i appreciate it.
Phil
Got it. thanks! :)
Phil