0

I have a javascript file pet.js. I want to pass a value of variable in test.php. But i can't.

my pet.js is like

$('#pmWorkOrderDetailsPage').live('pageshow', function(event) {
var id = getUrlVars()["id"];
$.get("test.php", { test1: id } );
$.getJSON('pmworkorderdetails.php?id='+id, displaypmWODetails);
});

function displaypmWODetails(data) {
 ..............code..........
 }

My test.php is like

<?php
$ms = $_GET["test1"];
echo $ms;
?>

But it is not working. I tried with Ajax and post method.

It will be best if I can store the variable value on the session in test.php.

Thanks in advance for any help.

7
  • You have an echo statement in test.php, but you're not using a callback to handle that data in pet.js. What exactly do you want to do with the output of test.php? Commented Sep 14, 2012 at 13:00
  • What do you mean by 'not working'? Do you get any errors in your console? Is 'id' filled correctly before you do the ajax call (log it in your console)? Commented Sep 14, 2012 at 13:00
  • try echo json_encode($ms);. Also, you may need to add a json header Commented Sep 14, 2012 at 13:02
  • Try some more using xmlhttprequest. So many people have made it. Search for "xhr javascript example". Commented Sep 14, 2012 at 13:12
  • Where's your callback for the first get request? You're making the request and passing in a parameter, but there is no function being called once it returns with something. Also bear in mind that it will be asynchronous, so if getjson relies on the data that comes back from it, you'll need to do that in the callback. Commented Sep 14, 2012 at 13:16

1 Answer 1

0

1 do not use getUrlVars() it can make site vulnerable to xss

$('#pmWorkOrderDetailsPage').live('click', function(event) {
var id;// get id
$.get("test.php?id="+id,function(data){

var result=$.parseJSON(data);
 alert(result["content"])
 });

 })

test.php

<?php

 $id=$_GET['id'];
 $data=array();
  $data=array("content"=>$id);
 echo json_encode($data);
?>
3
  • so would you plz tell me whats the error..? try to alert(new); Commented Sep 14, 2012 at 17:22
  • It is working 50%(lol).alert is showing with accurate id value.but in test.php showing {"content":null}.If it is possible to get in test.php!! Commented Sep 14, 2012 at 19:02
  • how it could be possible that alert shows the right id and you don't have id in test.php ? Commented Sep 15, 2012 at 5:19

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.