5

I am trying to use javascript to call a php script which then will return multiple variables back to my javascript so I can manipulate them.

This is my JS.

 $.ajax({ 
 url: 'test.php',
 data: { id : lastFileId },
 success: function(output) {
 alert(output);
 }
 });

my PHP

<?php
 $fileId = ($_GET['id']);
 $num1 = 1;
 $num2 = 2;
?>

From here, how can I return variables $num1 and $num2 so i can use them in my javascript. Is it possible?

also this is a very basic idea of what I have planned to do if I can achieve this.

asked Apr 2, 2014 at 19:26
1
  • 4
    return json (use json_encode in PHP) and add the parameter datatype: json to your ajax request. Commented Apr 2, 2014 at 19:27

5 Answers 5

24

You can return as many variables as you want with json_encode().

Try in your PHP:

<?php
echo json_encode(array($num1, $num2));
?>

You can add to that array , $num3, $num4, ... and so on.

In your JS, you can access each number as follows.

First, you will need this line of code to parse the encoded JSON string, in your success function.

var result = $.parseJSON(output);

That sets result as a JSON object. Now you can access all fields within result:

  • result[0] -- $num1 in PHP
  • result[1] -- $num2 in PHP
answered Apr 2, 2014 at 19:31
Sign up to request clarification or add additional context in comments.

3 Comments

When i do console.log(output[0]), it gives me ' [ '
It splits up each char as an element, this is not correct
My mistake, I forgot a line of code. You'll need var result = $.parseJSON(output);. See corrected post.
5

You can go for Json in PHP and javascript if you want array in response for a ajax request

PHP Code

<?php
 $fileId = isset($_GET['id'])?$_GET['id']:0;
 echo json_encode(array("field"=>$fileId,"num1"=>1,"num2"=>2));
?>

Js Code

jQuery.ajax({
 type: "GET",
 url: 'test.php',
 dataType: "json",
 success: function(response) {
 console.log(response);
 alert(response.num1);
 }
});
answered Apr 2, 2014 at 19:59

2 Comments

When I do resp.num1 i get "undefined" in my console
I'm getting the same "undefined"
1

convert json to object

jQuery.ajax({
 type: "GET",
 url: 'test.php',
 dataType: "json",
 success: function(response) {
 item=JSON.parse(response);
 console.log(item);
 alert(item.num1);
 }
});
tmthydvnprt
10.8k10 gold badges54 silver badges75 bronze badges
answered Jun 2, 2016 at 14:27

Comments

0

Use a simply string and explode(split) it further in ajax response. Here is PHP code

<?php
 $fileId = ($_GET['id']);
 echo $num1."|".$num2;
?>

Now split(explode) the response with JavaScript

$.ajax({ 
 url: 'test.php',
 data: { id : lastFileId },
 success: function(output) {
 var my_arr = output.split("|");
 console.log(my_arr[0] + "" + my_arr[1]);
 }
});
answered Mar 28, 2019 at 11:48

Comments

0

you can simply return it like that return ['num1'=>$num1,'num2' => $num2];

and also, you can access it as followed, respone.num1

answered Aug 9, 2019 at 9:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.