1

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:

  • [HTML / JS]
<!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"
}
1
  • 1
    Could you provide a sample of the generated JSON? Nov 8, 2012 at 12:51

2 Answers 2

2

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);

1
  • Thank you, but it seems i am doing something wrong. Accessing a keyname won't work, it says undefined when i try it as you wrote above. Also, i do not understand how to switch between the arrays. Nov 8, 2012 at 13:21
0

parseJSON helps you

var data = jQuery.parseJSON('{"name":"John"}');
alert( data.name );
1
  • why don't you use JSON.parse Nov 8, 2012 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.