Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Actually this code is wrote in index.php file but now i want to pass this javascript array value to external js file.

<?PHP 
  $qry2 = mysql_query("SELECT table_no FROM table_info");
  while($res2 = mysql_fetch_array($qry2))
  {
    static $i = 0;
    $i++;
    $reg_table[$i] = $res2['table_no']; 
  }
?>


<script>
  var bookedSeats = new Array();
  <?php foreach($reg_table as $key => $val)
  { ?>
    bookedSeats.push('<?php echo $val; ?>');
  <?php }?>
</script>

I want the bookedSeats variable to be in the external table.js file.

share|improve this question
2  
just include table.js after and reference bookedSeats var – David Nguyen Apr 2 '14 at 14:04
    
Also, why are you referencing an external script munna.js, then proceeding to write code inline? That seems odd – mituw16 Apr 2 '14 at 14:05

You have jQuery tag in there, so I am going to give you this... use Ajax:

test.php

<?php

// Handle Ajax Request
if (isset($_GET['loadData']))
{
    // Query your db here, im building dummy data
    $results = array();
    for ($i = 0; $i < 10; $i++) {
        $results[] = 'Data '. $i;
    }

    // Return Data
    exit(json_encode($results));
}

?>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">

// Load Data Via Ajax
$.getJSON("test.php?loadData", function(bookedSeats)
{
    console.log(bookedSeats);
});

</script>

When the page loads, we get the result like this:

enter image description here

share|improve this answer
    
You could include a brief explanation of json and how to parse it also. – user1537415 Apr 2 '14 at 15:02
<script>
  var bookedSeats = new Array();
  <?php foreach($reg_table as $key => $val)
  { ?>
    bookedSeats.push('<?php echo $val; ?>');
  <?php }?>
</script>

This can be greatly simplified and made immune to XSS:

<script>
  var bookedSeats = <?php echo json_encode(array_values($reg_table)); ?>;
</script>

You can now refer to bookedSeats in your external .js file, but only if that file is being run after this inline script has been placed. In other words, putting:

<script src="external.js"></script>

after the <script>...<?php ... ?>...</script> is okay, but putting it before is only okay if you are deferring its execution - it's just safer to put it after ;)

share|improve this answer

I have an alternative solution for you eventually even if Latheesan Kanes's one is right.

Mine is just given as a trivial exemple if you have access to a constructor in your Javascript object in the table.js file.

    <script>
  var bookedSeats = new Array();
person=new Object();
  <?php foreach($reg_table as $key => $val)
  { 
// here a javascript object
?>

person.firstname="John"; // of course replace firstname and John by your informations
  <?php }?>


    // then call table.js constructor
var objectInTableDotJS = new YourConstructor(person); // now in table.js you need to make modification :)

share|improve this answer
    
In Table.js you have all your code in Javascript but the content of the variables comes from the php file so it's quite dynamic – AMS Apr 2 '14 at 14:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.