Note that there are some explanatory texts on larger screens.

plurals
  1. POCoverting AJAX JSON function to return data objects instead of html into a modal window
    primarykey
    data
    text
    <h3>Background</h3> <p>Okay, I have a page which displays simple information on rows from a mysql database. For example, the table has 7 columns, but only #'s 1, 3 &amp; 4 are displayed on the page. On the right side of the row is a href link to open a modal window, and I am attempting to display all the rows, in a nicely formatted window with html/css/etc... </p> <p>After spending about 4 hours in tutorials on this, the closest thing I have come up with is the code base below, which does the job of passing the "id" to the json script, (almost) properly pulls the information, puts it in a single object line, and passes it back into my html/php page to be read into the modal window.</p> <p>I am politely asking for help to convert my codebase <b>so that it transfers all objects individually</b> instead of one gigantic line of php to echo into html (arse-backwards). I am rather new to JSON/AJAX/jquery, but can navigate and work with the html/css/php once I am able to figure this out. </p> <p>My apologies if the code examples are a little muffled and poor in syntax, I have spent hours trying to get this work as it is supposed to.</p> <h3>PHP Page With The AJAX</h3> <pre><code>$('.view_information').click(function(e) { // Button which will activate our modal //On Clicking the function, dynamically load the data for the viewing var data_id = $(this).data('id'); $.ajax({ url: 'view_agency_info.php', type: 'POST', data: {id: data_id}, dataType: 'json', success: function(data){ $('.view_modal_content').html(data.html); // LOAD THE DATA INTO THIS DIV //I want to be able to use... //$('.placeholder_name')xxxx //$('.placeholder_accountnumber')etc }, error: function(jqXHR, textStatus, errorThrown){ $('.view_modal_content').html(''); alert('Error Loading Information'); } }); </code></pre> <p>As you can see from the php page, as of right now, the mysql is being pulled into an array, then inserted individually into a single HTML object, then being passed back into PHP to be outputted. How can I convert this code to output more than just one object?</p> <h3>JSON</h3> <pre><code>&lt;?php $customer_id=$_SESSION['customer']['customer_id']; $id = (int)$_POST['id']; $query = "SELECT * FROM collections_list WHERE id={$id} &amp;&amp; customer_id=$customer_id LIMIT 1"; //expecting one row $result = mysql_query( $query ); //$message = mysql_fetch_assoc( $result ); //expecting just one row $message=array(); while ($row = mysql_fetch_assoc($result)) { $message[]=$row['agency_name']; $message[]=$row['account_number']; $message[]=$row['phone']; } $json = array(); $json['html'] = '&lt;p&gt;&lt;pre&gt;&lt;code&gt;id:'.$id.'.&lt;br&gt;Agency Name: '.$message[0].'&lt;br&gt;Account Number:'.$message[1]."&lt;br&gt;Phone:".$message[2].'&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;'.'&lt;br&gt;&lt;br&gt;test'; header('Content-Type: application/json'); echo json_encode( $json ); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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