0

I don't know how to make a script for sending multiple variables from external php to javascript (jQuery) For exemple:

<?php
  $test = "tets"; -->this, I want something this, no echo
  $dock = "dock"; --
  echo $test; -->no, I don't want echo in PHP script
  echo $dock; -->no, I don't want echo in PHP script
?>

and JS

<script>
function(){
  $.post("url", {Some_Thing: "Some_Thing"}, function (data) {
  alert(data); --> no, I don't want echo in PHP script
  alert($test); --> this, I want something this, no echo
  alert($dock);
  }
}
</script>
3
  • Use // Note here for inline notes. Then if we test your code it won't screw up. Commented Jan 28, 2014 at 16:21
  • you can send json or serilize them or even just use , for that Commented Jan 28, 2014 at 16:21
  • i think this question answer your question stackoverflow.com/q/5004233/1723893 Commented Jan 28, 2014 at 16:24

2 Answers 2

1

Use a data structure, e.g:

<?php
   $data = array('test', 'dock');
   echo json_encode($data);

Then in your JS

$.post('url', {...}, function (data) {
    alert(data[0]);
}, 'json');
  ^^^^^^^^--- tell jquery you're expecting back some json
Sign up to request clarification or add additional context in comments.

Comments

0

you can just output your data in JSON format and then load the data to JavaScript using ajax like so:

<?

$arrayWithData = array('data1' => 123, 'data2' => 555);

echo json_encode($arrayWithData);

then call load the data using ajax:

$.ajax({
    'url' : 'php-data-script.php',
    'method' : 'get',
    'dataType' : 'json'
}).done(function(data) {
    // do whatever you want with the data
    console.log(data);
});

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.