0

here is my simple code

  $.ajax({
            url:'action.php',
            method: 'POST',
            data:{getcart:1},
            success:function(response){
                $('#getcart').html(response);//want to display $return_value (from action page)
                $('#getcart2').html(response);//want to display $return_value2 (from action page)
            }
        });

Here i am sending request to action.php

and if in action.php i have echo two variables separately for example.

$return_value = " //Some html here ";  echo $return_value;
$return_value2= "//some other html";  echo $return_value2;

So the question is in ajax i have function with argument response . how i will be able to receive these both variables from php and display it in different divs. i hope you guys help me. thanks.

Rakesh K
  • 1,290
  • 1
  • 16
  • 43

4 Answers4

4

Send the responses as JSON.

In PHP

$return = array( $return_value, $return_value2 );
echo json_encode($return);

In Javascript

var response = JSON.parse(response);
$("#getcart").html(response[0]);
$("#getcart2").html(response[1]);
spaceman
  • 825
  • 9
  • 11
  • @spaceman sir, in my code there is while loop and i need to echo $return_value inside the loop and $return_value2 outside the loop.....but here where should i echo json_encode($return) . if i echo it inside the loop not getting any data but if i echo it outside the loop ; not getting repeated data through while loop but getting $return_value2. – Rakesh K Aug 24 '17 at 13:29
  • Don't echo $return_value inside the loop, but save it into a variable instead. `while(...) { ... $return_value = 1; ... } $return_value2 = 2;` The only echo that needs to happen is `echo json_encode($return);` and it definitely should be outside the loop. – spaceman Aug 24 '17 at 13:59
  • and where to create an array? – Rakesh K Aug 24 '17 at 14:24
1

your could return a json from action

echo json_encode(array('cart' => $return_value, 'cart2' => $return_value2));

then in your js,

       $.ajax({
            url:'action.php',
            method: 'POST',
            data:{getcart:1},
            dataType: 'json',
            success:function(response){
                $('#getcart').html(response.cart1);//want to display $return_value (from action page)
                $('#getcart2').html(response.cart2);//want to display $return_value2 (from action page)
            }
        });
okante
  • 151
  • 6
0

You need to use json_encode in order to get multiple records or data.

action.php

<?php 
header('Content-Type: application/json');

$array = array(
               'var1'=> 'var value 1',
               'var2'=> 'var value 2'
// more variables to go
);
echo json_encode($array);
?>

and then read your variables in JS

dataType: 'json',
success:function(response){
    console.log(response.var1);
    console.log(response.var2);
    $('#getcart').html(response.var1);
    $('#getcart2').html(response.var2);
}
Noman
  • 4,088
  • 1
  • 21
  • 36
0

You can use json_encode to create an object like:

$array = [
    "return_value" => " //Some html here ",
    "return_value2" => "//some other html",
];
echo json_encode($array)

Then add dataType: 'json' to your ajax options. The response then will be an object like:

{
    "return_value" => " //Some html here ",
    "return_value2" => "//some other html",
}

Where you can access the specific values with e.g. response.return_value2 and thus separate them.

vellip
  • 78
  • 6