javascript - Dealing with returned JSON data -
i'm querying database using ajax (jquery) data i'm getting proving difficult deal with. did notice others have posted similar issues , identified php code (specifically how data being allocated array) being issue. couldn't find solution. contents of returned data (when transform returned data string) looks this:
[{"id":"1","username":"admin","userpassword":"admin","useremail":"admin@admin.com","userregistrationip":"","registrationdatetime":"2015-05-28 21:22:54","userlastlogin":"2015-05-28 21:22:54"}]
i'd able pull individual element returned data, such userlastlogin field.
my ajax query:
$.ajax({ url: 'authenticate2.php', type: 'post', datatype: 'json', data: { username: $('#username').val(), }, success: function (res) { $('#resultbox').text(res); } });
my php below:
<?php $con = mysqli_connect('127.0.0.1','root','','db1'); if (!$con) { die('could not connect: ' . mysqli_error($con)); } mysqli_select_db($con,"db1"); $sql="select * users username = 'admin'"; $result = mysqli_query($con,$sql); $rows = array(); while($r = mysqli_fetch_assoc($result)) { $rows[] = $r; } echo json_encode($rows); mysqli_close($con); ?>
this'll you
js
success: function (res) { var res = [{"id":"1","username":"admin","userpassword":"admin","useremail":"admin@admin.com","userregistrationip":"","registrationdatetime":"2015-05-28 21:22:54","userlastlogin":"2015-05-28 21:22:54"}]; $.each(res,function(key,value){ alert(res[key].id); alert(res[key].username); alert(res[key].userpassword); alert(res[key].useremail); alert(res[key].userregistrationip); alert(res[key].registrationdatetime); alert(res[key].userlastlogin); }) }
Comments
Post a Comment