duanmao1319 2016-04-15 07:38
浏览 77

从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>
  • 写回答

4条回答 默认 最新

  • dongqing483174 2016-04-15 07:46
    关注
    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";

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用