php - HTML output through JSON, activated by ajax -
background:
i have page dynamically pulls modal window, displays extended information on row (with multiple columns) through mysql. having issues json code not populate information correctly can outputted. have tried multiple nested arrays, while loops , loops. however, need return 1 full row of information database. after scratching head, asking of experts. pointers appreciated.
ajax code div population (works)
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 data div }, error: function(jqxhr, textstatus, errorthrown){ $('.view_modal_content').html(''); // load data div alert('error loading information'); } });
json code pull information , return html
<?php $customer_id=$_session['customer']['customer_id']; $id = (int)$_post['id']; $query = "select * collections_list id={$id} && customer_id=$customer_id limit 1"; //expecting 1 row $result = mysql_query( $query ); //$message = mysql_fetch_assoc( $result ); //expecting 1 row $message=array(); while ($row = mysql_fetch_assoc($result)) { $message[]=$row['agency_name']; $message[]=$row['account_number']; $message[]=$row['phone']; } $json = array(); $json['html'] = '<p><pre><code>id:'.$id.'.<br>agency name: '.$message[0].'<br>account number:'.$message[1]."<br>phone:".$message[2].'</code></pre></p>'.'<br><br>test'; header('content-type: application/json'); echo json_encode( $json ); ?>
additional question:
is possible reference headers in array using " $message['agency_name'] "inside html gets returned?
after solving problem, need turn outputted html structure allow users view information in understandable format. know how in html, unfamiliar json... there way output information without having manually code structure?
thank in advance.
$con = mysql_connect("localhost","user","password"); if (!$con) { die('could not connect: ' . mysql_error()); } mysql_select_db(db_nname", $con); $result = mysql_query("select phone,agency_name '''' "); $rows = array(); while($r = mysql_fetch_assoc($result)) { $rows['results'][] = $r; } print json_encode($rows); ?>
and in html
<table id ="listtable"></table> var listdiv = $("#listtable"); $.getjson("whatever.php",function(json){ $.each(json.results,function(i,data){ listdiv.append("<tr><th>" + data.phone + "</th><th>" + data.agency_name + "</th></tr>"); }); });
and in append use data. , whatever fields data.agency_name data.phone
etc....
Comments
Post a Comment