Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually You are very close to success... just read these points.</p> <ul> <li>Whenever you are making an ajax request the browser sends a hit on ajax URL with respected parameters and details. On respected URL PHP code executes. It can return data in some format. It can not return data in directly PHP variable.</li> </ul> <p>Formats are:</p> <pre><code>text/html json xml ...... </code></pre> <p>Mainly for cross domain requests we use JSONP. And it means PHP script will return data in JSON. So you will just echo your <code>json_encode</code> in directly script. That will be the response of your ajax request.</p> <ul> <li>Now when you have got the data in ajax function, then jQuery uses success: function(response){ } where you response will come. So variable response will contain JSON. You can access JSON and put data in any tag by using jQuery selector. $().html(response.);</li> </ul> <p>These thing can be analyzed in any browser in debug console. That what is requested and what is returned. even you can use Firebug in Mozilla to inspect ajax request.</p> <p>So you will do three changes.</p> <p>In Ajax function write a success function:</p> <pre><code>var inputdata = $('#inputfield').val('ocean-view'); $('#myform').submit(function(e) { e.preventDefault(); $.ajax({ type: 'GET', url: 'http://serverB.com/detail.php', data: "inputdata="+inputdata, dataType: 'jsonp', success:function(response){ // response will be json type so access it // print ur json in html tag like resposne is {"data":23} $(&lt;jquery selector&gt;).html(reponse.data); } }); </code></pre> <p>});</p> <pre><code>&lt;?php // echo this $inputdata = $_GET['inputdata']; // perform you logic , // make an array to encode it in json echo $_GET['callback'].'('.json_encode($responseArr) .')'; ?&gt; </code></pre> <p>and remove PHP code from h4 tag.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI followed your advice and did an update on callback response, see the post. I changed ajax success: function(data){ $(body).html(data.query.results);}); now I am still not understanding how the variable $responseArr in json_encode() is created. Based on the callback response I provided in the update, do I need to change the variable name or it can be just anything? When I did the test, I left $responseArr as it is, my result returns "(null)".
      singulars
    2. COIt's any array . b/c json_encode function takes array as parameter. lete you wanna send a result like "query": { "count": 1, "created": "2013-07-29T13:01:12Z", "lang": "en-US", "results": { "h3": { "class": "mytitle", "content": "Example" } } } } then you need to create an array like $responseArr = array("query"=>array("count"=>1,"created"=>"2003-07-29T13:01:122","lang"=>"en-US","results"=>array("h3"=>array("class"=>"mytitle","content"=>"Example")))); echo json_encode($responseArr); $responseArr is not particular.
      singulars
    3. COI think I understand what you are trying to say. However, following the above example, I set $responseArr = array(1,2,3) & I echoed both &_GET['inputdata'] as well as the callback value. I tested the remote site by typing "http://serverB.com/detail.php&inputdata=ocean-view&callback=?". Well, the result shows up as "([1,2,3])" but I really need "ocean-view" to show up as well! That was the whole point I wanted it to be echoed from the remote server to the browser.
      singulars
 

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