Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I send JSON data from a PHP script to be used by jQuery?
    primarykey
    data
    text
    <p>I have a problem with some JSON data. I don't know how to take some data generated in PHP and turn that into something that I can use in my jQuery script. The functionality I need is this: I need to be able to click on images on the page, and depending on the selected element, I need to show results from my DB.</p> <p>Here's the HTML page that I've got:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;pippo&lt;/title&gt; &lt;script&gt;&lt;!-- Link to the JS snippet below --&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; Contact List: &lt;ul&gt; &lt;li&gt;&lt;a href="#"&gt; &lt;img src="contacts/pippo.png" onclick="javascript:change('pippo')"/&gt;pippo &lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt; &lt;img src="contacts/pluto.png" onclick="javascript:change('pluto')"/&gt;pluto &lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#"&gt; &lt;img src="contacts/topolino.png" onclick="javascript:change('topolino')"/&gt;topolino &lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Here's PHP code being called:</p> <pre><code>&lt;?php include('../dll/config.php'); $surname = $_POST['surname']; $result = mysql_query("select * from profile Where surname='$surname'") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $_POST['name'] = ucfirst($row['name']); $_POST['tel'] = $row['telephone']; $_POST['companymail'] = $row['companymail']; $_POST['mail'] = $row['email']; $_POST['fbid'] = $row['facebook']; } ?&gt; </code></pre> <p>Here's the Ajax JavaScript code I'm using:</p> <pre><code>&lt;script type="text/javascript"&gt; function change(user) { $.ajax({ type: "POST", url: "chgcontact.php", data: "surname="+user+"&amp;name=&amp;tel=&amp;companymail=&amp;mail=&amp;fbid", success: function(name,tel,companymail,mail,fbid){ alert(name); } }); return ""; } &lt;/script&gt; </code></pre> <p>Someone told me that this JS snippet would do what I want:</p> <pre><code>$.getJSON('chgcontact.php', function(user) { var items = [name,surname,tel,companymail,email,facebook]; $.each(user, function(surname) { items.push('surname="' + user + "'name='" + name + "'telephone='" + telephone + "'companymail='" + companymail + "'mail='" + mail + "'facebook='" + facebook); }); /* $('&lt;ul/&gt;', { 'class': 'my-new-list', html: items.join('') }).appendTo('body'); */ }); </code></pre> <p>But it is not clear to me - I don't understand how I need to use it or where I should include it in my code.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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