user1411837
user1411837

Reputation: 1564

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.

Upvotes: 0

Views: 149

Answers (3)

Jai
Jai

Reputation: 74738

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']";

Upvotes: 0

Damien Legros
Damien Legros

Reputation: 529

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

Upvotes: 2

thumber nirmal
thumber nirmal

Reputation: 1617

change

data : {"mapping_key":mapping_key} ,

to

data: "mapping_key=" + mapping_key,

Upvotes: 0

Related Questions