Reputation: 3327
I've got the following issue: I wrote a search function which results are being saved into an array. As i handle the response of that function with the jquery form plugin, i created an additional array which is filled with all of the arrays created by the search. Then, i want to parse that multi-array to my jQuery script as a JSON object. So far so good, but how do i make the multi-array accessable to the script? (Like multiarray.array1.property)
Here is my code so far:
<!DOCTYPE html> <html> <body> <div class="edit"> <h2>Editieren</h2> <form id="suchen" method="post"><input type="text" id="search" name="id"> <input type="submit" value="Senden"></form> </div> </html>
$('#suchen').ajaxForm({
url: "./php/search.php",
dataType: 'json',
success: function(data){
alert(data[0]) ;
},
clearForm: true
}) ;
Thank you in advance
Edit:
Example of JSON:
{
"id": "33",
"firma": "sls",
"strasse": "Industriegebiet Siebend",
"plz": "66663",
"ort": "Merzig",
"vorname": "",
"nachname": "Ruf",
"email": "[email protected] ",
"bemerkung": "",
"partner": "",
"kinder": "1",
"nation": "D",
"betreuer": "Adam",
"anrede": "Herr"
}
Upvotes: 1
Views: 339
Reputation: 4676
At your PHP
while($row = mysql_fetch_array($result)){
$article = array (
"id"=>$row['id'],
"firma"=>$row['firma'],
"strasse"=>$row['strasse'],
"plz"=>$row['plz'],
"ort"=>$row['ort'],
"vorname"=>$row['vorname'],
"nachname"=>$row['nachname'],
"email"=>$row['email'],
"bemerkung"=>$row['bemerkung'],
"partner"=>$row['partner'],
"kinder"=>$row['kinder'],
"nation"=>$row['nation'],
"betreuer"=>$row['betreuer'],
"anrede"=>$row['anrede'],
) ;
$hits[] = $article;
}
echo json_encode($hits) ;
At your jquery...
$('#suchen').ajaxForm({
url: "./php/search.php",
dataType: 'json',
success: function(data){
$.each(data, function(i, val){
console.log(val); /*alert don't work on arrays*/
alert(val.firma); /*you can alert some array key for example*/
});
},
clearForm: true
}) ;
I replace alert with console.log because val will be array... to access any of its keys just write val.keyname..
as .. alert(val.strasse);
Upvotes: 2
Reputation: 6702
parseJSON
helps you
var data = jQuery.parseJSON('{"name":"John"}');
alert( data.name );
Upvotes: 0