Hi all,
PLATFORM:
jQuery, PHP, mySQL
WHAT I HAVE:
I am using jQuery to process information from a form and send all the POST data to a php file that does several operations and returns an array of data along with other necessary data. One of the arrays is an array of an array. I need to access this array of array values in such a way that I can append each of those nested array values to a table.
WHAT I NEED:
I have been battling out with the jquery code to get this to work for a long time but it does not work. So if any one can help me out with the jquery code, I would really appreciate that. Thanks in advance.
TABLE STRUCTURE
fname lname city
Ed Al SA
Bob B MN
Chris V KJ
PHP code
//Content of my PHP file that return json encoded data
$success = 1;
$new_rows_data['fname'] = array();
$new_rows_data['lname'] = array();
$new_rows_data['city'] = array();
$result = mysql_query("SELECT fname, lname, city FROM table LIMIT 3");
while( $row = mysql_fetch_array($result) )
{
$new_rows_data['fname'][] = $row['fname'];
$new_rows_data['lname'][] = $row['lname'];
$new_rows_data['city'][] = $row['city']
}
print json_encode(array('success' => $success, 'new_rows_data' => $new_rows_data));
jQuery Code:
$('#button_delete').click(function() {
$.ajax({
type: 'POST',
cache: false,
url: 'test.php',
data: $('#form1').serialize(),
dataType: 'json',
success: function(data) {
if(data.success == '1')
{
jQuery.each(data.new_rows_data, function(i, val)
{
$('#table').append('<tr>'+
'<td>'+val.fname+'</td>'+
'<td>'+val.lname+'</td>'+
'<td>'+val.city+'</td>'+
'</tr>');
});
}//END if
}
}) //END ajax
return false;
});
I am trying to get this new data to get appended to an existing table with id # table, so that i appears like the following DESIRED OUTPUT:
First Name Last Name City
Orko C OI ---> (Existing row of data)
Gordon Flash KS ---> (Existing row of data)
Ed Al SA ---> New row of data that just got appended
Bob B MN ---> New row of data that just got appended
Chris V KJ ---> New row of data that just got appended