-5

i see one example in which they pass javascript variable into php by like this:

<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>

i try one exaple like this:

<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>

but its not give output. there is any solution for this problem? how can i get value in php variable? i want "nis" in $ff variable.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
yash
  • 5
  • 3

3 Answers3

0

You can not pass Javascript variable to PHP variable as PHP is server side language and Javascript is client side. Please check this answer

How to pass JavaScript variables to PHP?

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

Community
  • 1
  • 1
Saurabh
  • 776
  • 1
  • 5
  • 15
-1
<script>
var abcd = "xyz";
</script>

<?php
echo "<script>document.write(abcd);</script>";
?>
-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>
Keyur Buch
  • 41
  • 4