How do I alert multiple results with ajax from json arrray - php

I have this query
$result = mysql_query("SELECT * FROM ship_data WHERE id = $ship") or die(mysql_error());
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows = $r;
echo json_encode($rows);
}
And this bit of ajax to return the results
$.ajax({
type: "POST",
dataType: "json",
data: "ship=" + ship,
cache: false,
url: "/getdata.php",
success: function (data) {
alert(data.carrier);
}
});
If there is just one result in the array it works, if the array has multiple results in the array nothing is alerted.

Change your while loop to...
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
echo json_encode($rows);
And your JavaScript should always iterate over data with $.each().

Related

How to get ajax json loop response in javascript

I am using ajax call for sending value to php file and get response back into first page,(work fine), but when i am getting the result in loop form this response nothing will be return on first page.
My first page:
$.ajax({
url: "action.php",
method: "post",
data: {
'trader': selected_trader
},
dataType: 'JSON',
success: function (response) {
$('#date').html(response['date']);
$('#symbol').html(response['symbol']);
$('#status').html(response['status']);
$('#alert').html(response['alert']);
$('#company_name').html(response['company_name']);
}
})
My 2nd page:
if (isset($_POST['trader'])) {
$trader = $_POST['trader'];
$sql = "Select * from current_trader where status='$trader' ";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$data["date"] = date('Y:m:d', strtotime($row['date']));
$data["symbol"] = $row['symbol'];
$data["company_name"] = $row['company_name'];
$data["alert"] = $row['alert'];
$data["status"] = $row['status'];
echo json_encode($data);
}
}

json check empty data

I would check if my JSON response (data) is empty
$.ajax ({
type : 'POST',
url : get.php,
dataType: 'json',
success : function(data) {
console.log(data.lenght);
}
});
GET.PHP
$query = "SELECT * FROM TABLE";
$stmt = $this->db->mysqli->prepare($query);
$stmt->execute();
$result = $stmt->get_result();
$_assoc = array();
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
$_assoc = $row;
}
$stmt->close();
echo json_encode($_assoc);
JSON RESPONSE
{"foo":"6","bar":"3436","id":4,"code":""}
EMPTY JSON RESPONSE
[]
I tried with data.length but returned undefined.
UPDATE
I solved this way.
Object.keys(data).length
Thanks for yours downvote!

php ajax connection failure

Using the following I have tried a hundred different ways and all I get is the failure alert
Please could anyone spot my silly beginners mistake
myy.php
$con = mysqli_connect('adress','dbase','pass','table');
$result = mysqli_query("SELECT mycoat FROM $table");
$response = array();
while($array = mysqli_fetch_row($result)) {
$response[] = $array;
}
echo json_encode($response);
JavaScript:
function myfunction()
{
$.ajax({
url: 'myy.php',
dataType: 'json',
success: function() {
alert('success');
},
error: function() {
alert('failure');
}
});
}
Try this, You have missed db connection link in mysqli_query
$result = mysqli_query($con, "SELECT mycoat FROM $table");
instead of,
$result = mysqli_query("SELECT mycoat FROM $table");

php jquery iterate php array in success function

I have jquery pop form . It takes one input from the user ,mapping_key , Once the user enters the mapping key ,i make an ajax call to check if there is a user in the database with such a key.
This is my call .
Javascript:
$.ajax({
url : base_url+'ns/config/functions.php',
type: 'POST',
data : {"mapping_key":mapping_key} ,
success: function(response) {
alert(response)
}
});
PHP:
$sql = "select first_name,last_name,user_email,company_name from registered_users where mapping_key = '$mapping_key'";
$res = mysql_query($sql);
$num_rows = mysql_num_rows($res);
if($num_rows == 0)
{
echo $num_rows;
}
else{
while($result = mysql_fetch_assoc($res))
{
print_r($result);
}
}
Now i want to loop through the returned array and add those returned values for displaying in another popup form.
Would appreciate any advice or help.
In your php, echo a json_encoded array:
$result = array();
while($row = mysql_fetch_assoc($res)) {
$result[] = $row;
}
echo json_encode($result);
In your javascript, set the $.ajax dataType property to 'json', then you will be able to loop the returned array:
$.ajax({
url : base_url+'ns/config/functions.php',
type: 'POST',
data : {"mapping_key":mapping_key} ,
dataType : 'json',
success: function(response) {
var i;
for (i in response) {
alert(response[i].yourcolumn);
}
}
});
change
data : {"mapping_key":mapping_key} ,
to
data: "mapping_key=" + mapping_key,
You have to take the posted mapping_key:
$mapping_key = $_POST['mapping_key'];
$sql = "select first_name,last_name,user_email,company_name from registered_users
where mapping_key = '$mapping_key'";
or this:
$sql = "select first_name,last_name,user_email,company_name from registered_users
where mapping_key = $_POST['mapping_key']";

workout with the php returned array to jquery

my this php code is working for the $.ajax call which is below this code
$family = mysql_real_escape_string($_REQUEST['send_txt'], $link);
$query = "SELECT imgurl FROM images WHERE family='$family'";
//Query database
$result = mysql_query($query, $link);
//Output result, send back to ajax as var 'response'
$imgurl=array();
//$i=0;
if(mysql_num_rows($result) > 0){
//Fetch rows
while($row = mysql_fetch_array($result)){
$imgurl[]=$row['imgurl'];
}
}
echo $imgurl;
jquery code
$(document).ready(function() {
$('ul.sub_menu a').click(function() {
var txt = $(this).text();
$.ajax({
type: "POST",
url: "thegamer.php",
data:{send_txt: txt},
success: function(data){
$('#main-content').html(data);
}
});
});
});
it outputs just Array written at the #main-content div how to work with that array which are basically image paths
Why you create array from mysql result ? your code can be simpler like this:
<?php
$family = mysql_real_escape_string($_REQUEST['send_txt'], $link);
$query = "SELECT imgurl FROM images WHERE family='$family'";
//Query database
$result = mysql_query($query, $link);
//Output result, send back to ajax as var 'response'
if(mysql_num_rows($result) > 0)
{
//Fetch rows
while($row = mysql_fetch_array($result))
{
echo $row['imgurl'];
}
}
?>
Try you page from the browser directly. Using JSON can help here:
echo json_encode($imgurl);
and using getJSON instead of plain ajax:
$.getJSON('thegamer.php', {send_text:text}, function(data) { … });

Categories