从PHP代码中插入JavaScript变量中的值

I am trying to test the following code for inserting the value from php code to my javascript variable x

tested the php code and its giving correct output but alert box in the javascript shows this -

date_sub(curdate(),interval 1 day) and activity=1 group by code having b > 1000"; $query = mysql_query($myquery); if ( ! $myquery ) { echo mysql_error(); die; } $data = array(); for ($x = 0; $x < mysql_num_rows($query); $x++) { $data[] = mysql_fetch_assoc($query); } //echo json_encode($data); echo ''; mysql_close($server); ?>

<html>
  <head>    
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Testing </title>
   
  
    <script type="text/javascript" src="jquery-1.9.1.min.js"></script> 
  </head>
  <body>
  <?php
    $username='user'; 
    $password='pass';   
    $host='xx.xx.xx.xx';
    $database='abc';
    $server = mysql_connect($host, $username, $password);
    $connection = mysql_select_db($database, $server);
    $myquery = 'select code a,sum(fee) b from xyz where date > date_sub(curdate(),interval 1 day) and activity=1 group by code having b > 1000';
    $query = mysql_query($myquery);
    if ( ! $myquery ) {
    echo mysql_error();
    die;
    }
    $data = array();
    for ($x = 0; $x < mysql_num_rows($query); $x++) {
    $data[] = mysql_fetch_assoc($query);    
    }
    //echo json_encode($data);
    echo '<input type="hidden" name="myPhpValue" value="'. json_encode($data) . '">';
    
    mysql_close($server);
    ?>
    
    <script type="text/javascript">
    

    function test(){

     var x = document.getElementById("myPhpValue").value;
     alert(x);
     
     
     
          
    }

    test();



    </script>
    

  </body>
</html>

</div>
echo '<input type="hidden" name="myPhpValue" value="'. json_encode($data) . '">';

Then:

var x = document.getElementById("myPhpValue").value;

you need to insert id="myPhpValue", because you used the "getElementById";

Add ID attribute in the html line

echo '<input type="hidden" name="myPhpValue" value="'. json_encode($data) . '">';

replace the above line by

echo '<input type="hidden" name="myPhpValue" id ="myPhpValue" value="'. json_encode($data) . '">';

You are inserting a value into html, not javascript code. Do it like that:

<script type="text/javascript">

function test(){
 var x = <?php echo json_encode($data); ?>;
 alert(x);  
}
test();

</script>

Server configuration problems

If You are getting php code on client-side (view-source to confirm), then the PHP engine is not working on the server. You should check that php is properly installed on the server and is set as a handler for php files in your web server. This depends on your web server and operating system.


Code problems

Problem #1: your output (echo) is creating an HTML element, not javascript. Hence you should escape the content for HTML - use htmlspecialchars instead of json_encode

Problem #2: you access the element with javascript document.getElementById but your actual element does not have an ID. Hence need to add the id attribute to your html input element.


Solution:

Stage 1: php outputs html - use htmlspecialchars and add id attribute

echo '<input type="hidden" name="myPhpValue" id="myPhpValue" value="'. htmlspecialchars($data) . '">';

Stage 2: javascript accesses html element (this is taken as-is from your code).

var x = document.getElementById("myPhpValue").value;

Side-note

You're using a deprecated mysql extension and should switch to PDO or mysqli instead.

There are numerous discussions both on SO and external resources on the matter.

Just a few: