php - JSON object is not returned with ajax -
i beginning play around json, , keep running trouble neither google nor have helped me with. have simple php script:
<?php $email = $_request['email']; if ( strpos($email,'@') !== false ) { $data = array('status' => 1 , 'msg' => 'sent') ; echo json_encode( $data ) ; } else { $data = array('status' => 0 , 'msg' => 'failed send') ; echo json_encode( $data ) ; } ?> i have following ajax call:
$('.submit').click(function() { $('div.load').html('<img src="images/load.gif" alt="loading..." id="loading" />'); //edit //creation of variables send var name = $('#name').val(); email = $('#email').val(); phone = $('#phone').val(); $.ajax({ type: "post", datatype: "jason", data: { name: name, email: email, phone: phone }, url: "test.php", success: function( data ) { $('.contact').append( data ) } }); return false; }); if php gets called without js (and form doesn't contain proper email address), following object (which want!): {"status":0,"msg":"failed send"}
however, if submitting js (ajax), json object never gets received. ideas?
thanks!
datatype: "jason", read:
datatype: "json", ;-)
also, have semi colons there should commas:
var name = $('#name').val(), // these 2 lines should comma-terminated email = $('#email').val(), // make correct var declaration phone = $('#phone').val();
Comments
Post a Comment