Note that there are some explanatory texts on larger screens.

plurals
  1. PO$.ajax + POST + JSON + PHP = why is not working?
    text
    copied!<p>I am now having a problem to send and receive data via $.ajax, json and PHP.</p> <p>I have my page JQM:</p> <pre><code>&lt;div data-role="page" id="json"&gt; &lt;div data-role="panel" id="painelMenu"&gt; &lt;div data-role="content"&gt; &lt;a href="index.html" data-role="button"&gt;Voltar&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;!-- /panel --&gt; &lt;div data-role="header" data-position="fixed"&gt; &lt;h1&gt;WGBN Mobile&lt;/h1&gt; &lt;/div&gt; &lt;!-- /header --&gt; &lt;div data-role="content"&gt; &lt;p&gt;Esta é a página 2.&lt;/p&gt; &lt;p&gt;&lt;a href="#painelMenu" data-role="button"&gt;painel&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="#" data-role="button" class="manda"&gt;Manda&lt;/a&gt;&lt;/p&gt; &lt;hr&gt; &lt;div id="mostra"&gt;&lt;/div&gt; &lt;/div&gt; &lt;!-- /content --&gt; &lt;div data-role="footer" data-position="fixed"&gt; &lt;h4&gt;&amp;copy; 2013 WGBN Software House.&lt;/h4&gt; &lt;/div&gt; &lt;!-- /footer --&gt; &lt;/div&gt; &lt;!-- /page --&gt; </code></pre> <p>With this JavaScript to run:</p> <pre><code>$(document).delegate("#json", "pageinit", function() { $(".manda").click(function(e) { $.ajax({ url : "json1.php", dataType : "json", data : '{"opc":"sim"}', success : function(data){ var html = ""; for($i=0; $i &lt; data.length; $i++){ html += "&lt;strong&gt;Nome:&lt;/strong&gt; "+data[$i].nome +" "+ data[$i].sobreNome; html += " &lt;strong&gt;Cidade:&lt;/strong&gt; "+data[$i].cidade html += "&lt;br /&gt;"; } $("#mostra").html(html); } }); return false; }); }); </code></pre> <p>And this PHP script on my server:</p> <pre><code>&lt;?php if ($_POST['opc'] == "sim"){ $var = Array( array( "nome"=&gt;"João", "sobreNome"=&gt;"Silva", "cidade"=&gt;"Maringá" ), array( "nome"=&gt;"Ana", "sobreNome"=&gt;"Rocha", "cidade"=&gt;"Londrina" ), array( "nome"=&gt;"Véra", "sobreNome"=&gt;"Valério", "cidade"=&gt;"Cianorte" )); echo json_encode($var); } ?&gt; </code></pre> <p>But I can not read the data sent to PHP, so it can run code on the server and return a JSON to clinte.</p> <p>An important detail, if I comment this line in javascrpt:</p> <pre><code>$(document).delegate("#json", "pageinit", function() { $(".manda").click(function(e) { $.ajax({ url : "json1.php", dataType : "json", /*data : '{"opc":"sim"}',*/ success : function(data){ var html = ""; for($i=0; $i &lt; data.length; $i++){ html += "&lt;strong&gt;Nome:&lt;/strong&gt; "+data[$i].nome +" "+ data[$i].sobreNome; html += " &lt;strong&gt;Cidade:&lt;/strong&gt; "+data[$i].cidade html += "&lt;br /&gt;"; } $("#mostra").html(html); } }); return false; }); }); </code></pre> <p>and these lines in PHP:</p> <pre><code>&lt;?php //if ($_POST['opc'] == "sim"){ $var = Array( array( "nome"=&gt;"João", "sobreNome"=&gt;"Silva", "cidade"=&gt;"Maringá" ), array( "nome"=&gt;"Ana", "sobreNome"=&gt;"Rocha", "cidade"=&gt;"Londrina" ), array( "nome"=&gt;"Véra", "sobreNome"=&gt;"Valério", "cidade"=&gt;"Cianorte" )); echo json_encode($var); //} ?&gt; </code></pre> <p>I have shown in my normal return JQM page.</p> <p>How do I get PHP to recognize that data sent via JSON? This is just a test, because solving this I send complete forms for $.ajax to PHP save in the database, the methodological POST in this case is imperative.</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