AJAX/PHP/Jquery Issue with callback -
i can't seem figure out why won't work. have stripped elements out , have striped done base example. can please tell me doing wrong. when click on button on html page initial alert, never alert call backs.
here code html file. test.html
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>untitled document</title> <script src="http://code.jquery.com/jquery-1.5.js"></script> <script language="javascript" type="text/javascript"> $(document).ready(function() { $("#test").click(function() { alert("!st"); $.ajax({ type : 'post', url : 'test_ajax.php', datatype : 'json', data: { email : 'jeremy' }, success : function(data){ alert(data.msg); }, error : function(xmlhttprequest, textstatus, errorthrown) { alert("error"); } }); return false; }); }); </script> </head> <body> <input name="test" id="test" type="button" value="click me"/> <div id="results"> hello </div> </body> </html>
now php: test_ajax.php
<?php if (empty($_post['email'])) { $return['error'] = true; $return['msg'] = 'you did not enter email.'; } else { $return['error'] = false; $return['msg'] = 'you\'ve entered: ' . $_post['email'] . '.'; } echo json_encode($return); ?>
please assistance appreciated.
thanks jeremy
the code looks clean. try use php buffering. put ob_start()
@ beginning of php file , in code
// cleans buffer ob_clean(); echo json_encode($return); die();
Comments
Post a Comment