Note that there are some explanatory texts on larger screens.

plurals
  1. POCant get jQuery ajax to work
    text
    copied!<p>I am trying to convert my webpage to ajax but I cant get it to return anything.</p> <p><strong>HTML</strong></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt; &lt;title&gt;test page&lt;/title&gt; &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; &lt;!-- $(document).ready(function(){ // events $("#test").click(function(){ var myData; myData = makeDataVar(); sendData(myData); }); // functions function makeDataVar(){ return "name=joe&amp;location=bostin"; } function sendData(myData){ alert("start mydata function"); $.ajax({ type: "POST", url: "http://www.mypage.com/submitMe.php", dataType : "json", data: myData, success: function (msg) { alert("Data saved: " + nameMsg); alert("Data saved: " + locationMsg); alert("data saved: " + error); }, error : function(XMLHttpRequest, textStatus, errorThrown) { alert("error " + errorThrown); } }); alert("end mydata function"); } }); // end document.ready //--&gt; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;button type="button" id="test" &gt;Add New&lt;/button&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>PHP</strong></p> <pre class="lang-php prettyprint-override"><code>&lt;?php $myVar = $_POST['name']; $return['nameMsg'] = "thanks $myVar"; $myVar = $_POST['location']; $return['locationMsg'] = "from $myVar"; $return['error'] = false; echo json_encode($return); ?&gt; </code></pre> <p>I am getting all of my javascript alerts except for those inside the ajax statement (neither success or error). It is almost like my script is skipping over that one statement. I am not sure what I am missing here but I have spent a couple of hours trying to figure it out. Any help you could offer would be greatly appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload