-6

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 it's giving the correct output but the 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>
xxx
  • 1,153
  • 1
  • 11
  • 23
cap
  • 179
  • 7
  • you're inserting a value into html, not javascript code. so use `htmlspecialchars` instead of `json_encode` – poncha Apr 15 '16 at 07:40
  • i am inserting the value in a javascript variable.. and i tried a ssuggested, it isnt working – cap Apr 15 '16 at 08:59

4 Answers4

1
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";

Ben Rhys-Lewis
  • 3,118
  • 8
  • 34
  • 45
zzZZ
  • 26
  • 1
0

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) . '">';
0

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>
Danmoreng
  • 2,367
  • 1
  • 19
  • 32
0

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:

Community
  • 1
  • 1
poncha
  • 7,726
  • 2
  • 34
  • 38