0

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.

3 Answers 3

2

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);
        }                      
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

change

data : {"mapping_key":mapping_key} ,

to

data: "mapping_key=" + mapping_key,

Comments

0

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.