Kamruddin Bivob Kamruddin Bivob - 1 year ago 45
Javascript Question

How to get multiple return value from php by javaScript / jquery?

This is my javaScript code :

$(document).ready(function() {
$('#IMDB').click(function() {
var MovieID = $('#MovieID').val();

$.post('action/action.php', { url: "http://api.themoviedb.org/3/movie/"+MovieID
+"?append_to_response=credits,images&api_key=myapikey" }, function(data) {

When I click the button I get imdb id from my input field which I inserted
then I get the actual result from php. this is my php code.

$url = $_POST['url'];
$url2 = file_get_contents($url);
$json = json_decode($url2, true); //This will convert it to an array
$title = $json['original_title'];
$imdb = $json['imdb_id'];

echo $title;
echo $imdb;

return true;


But I get result like this :

Batman: The Killing Jokett4853102

One is movie title and another is imdb id in same html tage. but I want to display my each result in each html tag like this :

$("#test").html(data); // result 1 -- movie title
$("#test2").html(data); // result 2 --- imdb id

Please help me how to get multiple value?

Answer Source

It would probably be easiest just to output the entire JSON structure and work with that in Javascript, but if you just want the title and id, change your individual echo calls to one with a hash:

header('Content-Type: application/json');
echo json_encode(array(
  'title' => $title,
  'id' => $imdb

Then you can reference them in your javascript using:

var id = data.id;
var title = data.title;