I am trying to use a jquery getJSON get information for my table from a php file, and its not working. Can someone give me a few pointers as to where I am going wrong:
Javascript:
$(document).ready(function(){
$("input#autofill").click(function()
{
$.get("last_year_ISBN.php",
function(data){
var isbns = $.parseJSON(data); //creates a javascript object.
$.each(isbns, function(){
var elements = document.getElementById("booklist").getElementsByTagName("input");
for (var i=0, i<elements.length; i++)
{
elements[i] = this;
}
});
},
"json");
});
});
HTML/php
<?php
$file = fopen("SGS_Fall_Booklist.csv", "r");
$entry= fgetcsv($file);
echo $entry[0] . $_GET["program"] . $entry[1] . $GET["school"] . $entry[2] . $GET["term"] . "<br>";
//COMMENTED OUT FOR TESTINGecho "<input type='submit' name='checkall' value='AutoFill (last years ISBN's)'></input><br>";
//if ($entry[0] == $_GET["program"] && $entry[1] ==$GET["school"] && $entry[2] == $GET["term"])
//{
echo "<table id='booklist'><tr>
<th>Edit</th>
<th class='coursename'>" . $entry[6] . "</th>
<th class='startdate'>" . $entry[7] . "</th>
<th class='booktitle'>" . $entry[17]. "</th>
<th class='author'>" . $entry[18]. "</th>
<th class='isbn'>" . $entry[16]. "</th>
</tr>";
while(! feof($file))
{
$entry = fgetcsv($file);
echo "<tr>
<td><input type='checkbox'></input></td>
<td class='coursename'>" . $entry[6] . "</td>
<td class='startdate'>" . $entry[7] . "</td>
<td class='booktitle'>" . $entry[17]. "</td>
<td class='author'>" . $entry[18]. "</td>
<td class='isbn'><input class='ISBN_number' type='text' value='";
if(isset($_GET["checkall"]))
{
echo $entry[16];
}
echo "'size='13' maxlength='13'></input></td>
</tr>";
}
//}
echo "</table>";
fclose($file);
?>
external php file
<?php
$file = fopen("SGS_Fall_Booklist.csv", "r");
$entry = fgetcsv($file); //discard the first title line
$i=0;
while(! feof($file))
{
$entry = fgetcsv($file);
$isbns=[$i] = $entry;
$i++;
}
fclose($file);
echo json_encode($isbns);
?>
I have changed my code as hyperslug suggested and it still doesn’t work. The alert(data) does return the whole json statement. What am I missing?
$(document).ready(function(){
$("input#autofill").click(function()
{
$.get("last_year_ISBN.php",
function(data){
alert(data);
var isbns = $.parseJSON(data); //creates a javascript object.
var elements = $('#booklist .ISBN_number');
$.each(isbns, function(index, obj){
//var elements = document.getElementById("booklist").getElementsByTagName("input");
//for (var i=0, i<elements.length; i++)
//{
//elements[i].value = this;
//}
$(elements[index]).val(obj);
});
},
"json");
});
});
I finally figured out my problem! I was using json_encode on the php side which sends the request back as json. The .get call was reading it in AS JSON (note the last parameter) and automatically turning it into a javascript object. Therefore, of course my parseJSON line wouldn’t work, I was parsing a javascript object, not json text!!
Your jQuery
.eachloop gives you an index and a reference to each object inisbns. Ifisbnsis a simple array and has exactly the same number of elements as yourbooklisthas rows,should fill your ISBN
<input>fields.If
isbnsis actually an array of objects, you’ll have to reference that value by name: