Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I convert the result of this postgres db query into json to return to my jquery call?
    primarykey
    data
    text
    <p>I'm creating a contest sign-up page for our annual math competition at my school. It requires some AJAX like behavior when you click and select an item in a dropdown list.</p> <p>I've gotten an event to fire when I select something in the dropdown (I'm currently showing an alert box):</p> <pre><code>&lt;script type="text/javascript"&gt; $(function() { $("#student").change(onStudentChange); }); function onStudentChange() { alert("Dropdown changed"); } &lt;/script&gt; </code></pre> <p>What this needs to do, is do an asynchronous call to the server to get a list of contests that student is currently registered for.</p> <p>I know I need to do a jquery ajax call. So I think my onStudentChange() function would look like this:</p> <pre><code>$.ajax({ type : 'POST', url : 'get_registered_events.php', dataType : 'json', data: { studentid : $('#student').val() }, success : function(data){ // Do something once we get the data }, error : function(XMLHttpRequest, textStatus, errorThrown) { // Display error } }); return false; }); </code></pre> <p>So I went ahead and created the <code>get_registered_events.php</code>, which I want to return the events the student is registered for. </p> <p>My problem is, I'm not experienced with PHP and I'm having difficulty figuring out how I would return the data the database gave me to this ajax call in the JSON format. Another snag I have is our school is using a very old version of PHP so I have to use this <a href="http://www.extjs.com/learn/Tutorial:Creating_JSON_Data_in_PHP#Less_than_PHP_5.2.0_and_no_root_access_.28.2ANIX.29" rel="nofollow noreferrer">PEAR JSON</a> library.</p> <p>Here is that PHP file I'm having trouble with:</p> <pre><code>&lt;?php if (!empty($_POST['studentid')) { include_once('JSON.php'); $json = new Services_JSON(); $dbconn = pg_connect("host=somehost dbname=somedb user=someuser password=somepassword") or die('Could not connect: ' . pg_last_error()); $query = 'SELECT contest.id, contest.title, FROM contest, student_contest WHERE student_id = '.$_POST['studentid'].' AND contest.id = contest_id'; $contests = pg_query($query) or die('Query failed: ' . pg_last_error()); // Here I need to convert the rows of $contests (contest.id, contest.title), into JSON and return it to that ajax call. } ?&gt; </code></pre> <p>So my question is, how do I convert $contests (its rows), into JSON (contest.id, contest.title), and return that back to the ajax call that will make it above.</p> <p>If anyone could point me in the right direction I would really appreciate it.</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.
 

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