How do I get all the filenames in the php file with jQuery getjson?
Json encoded data from PHP
[{"Filename":"941-2"},{"Filename":"941"}]
My code:
$.getJSON('json.php',{id: id, ajax: 'true'}, function(j){
$('#filename').append(j[0].Filename);
})
Do a simple forloop on j.
For example:
for (var i = 0; i < j.length; i++)
$('#filename').append(j[i].FileName);
Related
So i am using 2 apis of php that echo's value in json encode
one api contains all the data and the other api has the same data but only where one of the value is true
e.g
data from api contains 50 rows
data from api 2 contains 20 rows because that api shows same data but where a value is Yes
i cannot use join in tables as they are from different sources
so i was trying to join them using jQuery
with my code only the first value changes but not the rest
ive tried the following code but it will only change the first value
$.get("customapi.php", {
data: 'get_data'
}, function(response) {
$("#autovisit tbody").html("");
for (var i = 0; i < response.length; i++) {
html = "<tr>";
html += `
<td><b>${i+1}</b></td>
<td>${response[i]['name']}</td>
<td>${response[i]['p_name']}</td>
<td>${response[i]['p_type']}</td>
<td>${response[i]['a_date']}</td>
<td>${response[i]['l_date']}</td>
<td>${response[i]['r_date']}</td>
<td>${response[i]['d_name']}</td>
<td id="get_visit" class="text-center">NO</td>
<td id="get_invoice" class="text-center">NO</td>`;
$("#autovisit tbody").append(html);
$.get("customapi2.php", {
data: 'get_data'
}, function(result) {
for (var i = 0; i < result.length; i++)
$("#get_visit").text(result[i]['VISITED']);
$("#get_invoice").text(result[i]['INVOICED']);
}, 'JSON');
}
}, 'JSON');
in the above api both have the same data in the following values
{response[i]['name']}
{response[i]['p_name']}
{response[i]['p_type']}
{response[i]['a_date']}
{response[i]['l_date']}
{response[i]['r_date']}
{response[i]['d_name']}
the rest two
that is visit and invoice is comming from the other api and i need to change that text but with this code when the first name is true it only changes that not the others
You can use the following code to update your column values.
What you will be doing here is assigning a specific id to a data cell and then using that id to update data cell based on values from second api call.
This is done assuming that name is unique in response
$.get(
"customapi.php",
{
data: "get_data"
},
function(response) {
$("#autovisit tbody").html("");
for (var i = 0; i < response.length; i++) {
html = "<tr>";
html += `
<td><b>${i + 1}</b></td>
<td>${response[i]["name"]}</td>
<td>${response[i]["p_name"]}</td>
<td>${response[i]["p_type"]}</td>
<td>${response[i]["a_date"]}</td>
<td>${response[i]["l_date"]}</td>
<td>${response[i]["r_date"]}</td>
<td>${response[i]["d_name"]}</td>
<td id="visit-`${response[i]['name']}`" class="text-center">NO</td>
<td id="invoice-`${response[i]['name']}`" class="text-center">NO</td>`;
$("#autovisit tbody").append(html);
}
$.get(
"customapi2.php",
{
data: "get_data"
},
function(result) {
for (var i = 0; i < result.length; i++)
$("#visit-`${result[i]['name']}`").text(result[i]["VISITED"]);
$("#invoice-`${result[i]['name']}`").text(result[i]["INVOICED"]);
},
"JSON"
);
},
"JSON"
);
Please not that your 2nd API call should be outside the for loop, otherwise it will have too many unnecessary requests
You need UNIQUE IDs!
There cannot be more than one get_visit and one get_invoice so change the ID of the cells to some unique you also can know in the second code
An actual ID is best, so change the uniqueID combo below to an database ID if you have it
var uniqueID;
for (var i = 0; i < response.length; i++) {
uniqueID = response[i]['name'] + "_" + response[i]['p_name']; // or whatever is unique
html = "<tr>";
html += `
<td><b>${i+1}</b></td>
<td>${response[i]['name']}</td>
<td>${response[i]['p_name']}</td>
<td>${response[i]['p_type']}</td>
<td>${response[i]['a_date']}</td>
<td>${response[i]['l_date']}</td>
<td>${response[i]['r_date']}</td>
<td>${response[i]['d_name']}</td>
<td id="${uniqueID}_visit" class="text-center">NO</td>
<td id="${uniqueID}_invoice" class="text-center">NO</td>`;
}
html += "</tr>";
...
for (var i = 0; i < result.length; i++)
uniqueID = result[i]['name'] + "_" + result[i]['p_name']; // or whatever is unique from above
$(`#${uniqueID}_visit`).text(result[i]['VISITED']);
$(`#${uniqueID}_invoice`).text(result[i]['INVOICED']);
}
Got an issue today, I can't pick any info of my JSON array.
Example of a row :
[{"0":"84","id_account":"84","1":"1500","count_soleillos":"1500","2":"2018-06-26 10:19:43","date_purchase":"2018-06-26 10:19:43","3":"Doe","name_account":"Doe","4":"Jhon","nickname_account":"Jhon","5":"standard","type_offer":"standard"},
And here is my javascript
$.ajax({
type: 'POST',
url: example/example.php,
success: function (data) {
var jsonData = JSON.parse(data);
var container = $('liste-offer');
for (var i = 0; i <= jsonData.length; i++) {
var counter = jsonData[i];
Info : when I do jsonData.length, it returns 8 rows (exact). When I do
jsonData[0] or anything else, it returns undefined
Hope you could help me :)
You could use < jsonData.length; instead of <= jsonData.length; because you are indexing into the object and that has 8 properties instead of 9. Then <= will match 0 till 8 inclusive and jsonData[8]; does not exist and will give you undefined.
You could update your code to:
for (var i = 0; i < jsonData.length; i++) {
Try to access any key by below way:
jsonData[0]['id_account'] or jsonData[0].id_account
For exact solution please update your question with complete JSON and with your JavaScript method
Try this, it's working for me
for (var i = 0; i < jsonData.length; i++) {
alert(jsonData[i].id_account);
var counter = jsonData[i];
}
[{"id":"1","name":"Bangalore"},{"id":"3","name":"Mysore"}]
I tried using the below code but its not giving me any output
var obj=jQuery.parseJSON(response); // now obj is a json object
alet(obj.id);
$("#state").html("<option value='"+ obj.id +"'>'"+ obj.name +"'</option>");
$("#state").next().next().html("<li rel='"+ obj.id +"'>"+ obj.name +"</li>");
Your JSON is a JSON array containing two objects. Therefore, you need to read it as an array first, before accessing the id:
alert(obj[0].id);
To loop all, you will write something like this:
for(var i = 0; i < obj.length; i++){
console.log("ID: " + obj[i].id);
};
Loop through array
var response=jQuery.parseJSON(response);
var li='';
for(var i=0;i<response.length;i++){
li=li+ "<option value='"+ response[i].id +"'>'"+ response[i].name +"'</option>";
}
$("#state").html(li);
I have a PHP file which only return an array with the drivers and a url:
{"drivers":[{"marco":[0],"luigi":[123],"Joan":[2444],"George":[25]}, {"marco":[23],"luigi":[3],"Joan":[244],"George":[234]}],"url":"google.es"}
Is the json correctly structured?
And I'm trying to get the result using jQuery and AJAX by this way:
$.getJSON('calculate.php&someparams=123', function(data) {
alert("url - " + data.url);
var arr = data.drivers;
for (var i = 0; i < arr.length; i++) {
alert(arr[i] + " - " + arr[i][0]);
}
});
I see the first alert() with the url, but the second one does not works...
What am I doing wrong?
If you need more info let me know and I'll edit the post.
The drivers is not an array, it is an object, you use $.each to iterate through the object elements.
$.getJSON('calculate.php&someparams=123', function(data) {
$.each(data.drivers, function(key, value){
$.each(value, function(key, value){
console.log(key, value);
});
})
});
That's an object, not an array. It has named properties, not numerical indexes.
You need a for in loop to loop over the properties.
drivers is an object. Not an array.
How about this?
var json_string = '{"drivers":{"marco":[0],"luigi":[123],"Joan":[2444],"George":[25]},"url":"google.es"}';
var obj = jQuery.parseJSON(json_string);
alert(obj.url);
I echo JSON data in PHP like this.
echo json_encode($row,JSON_PRETTY_PRINT);
Then I use ajax in jquery and do for loop to loop the json data.
for(var i=0; i<data.length; i++) {
var img = data[i].url;
}
Ok, now here my problem.
I have a get_thumb(parem1, param2) php function and I want to use this function to
get the thumbnail image. How can I select the json url in php?
Like this
Edited
get_thumb($row[i]['url'], param2);