如何将javascript变量存储在php中?

时间:2016-07-19 11:12:19

标签: javascript php

我看到一个例子,他们将javascript变量传递给php,如下所示:

<html>
<head>
<script type="text/javascript">
    var q = "hello";
    <?php
      $ff ="<script>document.write(q1)</script>";
   ?>
</script>
</head>
<body>
<?php echo $ff; ?>
<input type="hidden" value="nis" id="val">
</body>
</html>

我尝试这样的一个例子:

<html>
<head>
<script type="text/javascript">
    var q = $("#val").val();
    <?php
      $ff ="<script>document.write(q)</script>";
   ?>
</script>
</head>
<body>
<?php echo $ff; ?>
<input type="hidden" value="nis" id="val">
</body>
</html>

但它没有给出输出。这个问题有什么解决方案吗?我如何在php变量中获得价值?我想在$ ff变量中使用“nis”。

3 个答案:

答案 0 :(得分:0)

你不能将Javascript变量传递给PHP变量,因为PHP是服务器端语言而Javascript是客户端。 请检查此答案

How to pass JavaScript variables to PHP?

What is the difference between client-side and server-side programming?

答案 1 :(得分:-1)

<script>
var abcd = "xyz";
</script>

<?php
echo "<script>document.write(abcd);</script>";
?>

答案 2 :(得分:-1)

Pl. try this code   
 <html>
    <head>
    <script type="text/javascript">
       var q=document.getElementById("val").value;
        //document.write(q);

    </script>
    </head>
    <body>



    <input type="hidden" value="nis" id="val"></body>

    <script type="text/javascript">
       var q=document.getElementById("val").value;
        document.write(q);

    </script>
     <?php
          echo $ff="<script type='text/javascript'>
                    document.write(q)</script>";
       ?>
    <?php echo $ff; ?>
    </html>