0

I need to pass a value from javascript to php. How can i achieve this since one runs on client side and other on server? or any other alternatives for this to happen? I am a newbie in programming, so correct and suggest me if i am wrong.

<html> 
    <head>
       <SCRIPT language="JavaScript"> 

          var q1=Math.floor(Math.random()*11) 
          <?php 
             $ff =  q1; 
          ?>
       </SCRIPT> 

    </head> 

    <body> 
       <?php 
          echo $ff ; 
       ?> 
    </body> 
</html> 
6
  • PHP runs on the server to create the page. It's all done by the time Javascript runs on the client. Commented Jan 12, 2014 at 7:10
  • If you need to pass data from Javascript to PHP, you have to submit a form or use AJAX. Commented Jan 12, 2014 at 7:11
  • Is my code wrong. If not, why am nt i able to pass the value from JS to php? Commented Jan 12, 2014 at 7:12
  • You can do this via an ajax call. Did you want that code? Commented Jan 12, 2014 at 7:14
  • Yes that could a problem solver for me Commented Jan 12, 2014 at 7:16

5 Answers 5

3

It is not possible since PHP code (server-side) is run/parsed before javascript code (client side). So what you are doing is not possible although reverse is possible. You can also use:

  • Ajax
  • Hidden form field
  • Query string variable (via url)

To send javascipt variable value to php.

Sign up to request clarification or add additional context in comments.

1 Comment

So , its not at all possible to pass the javascript variable to PHP. Can i store value of Q1 in a database(sql)? If so please suggest how?
2

PHP is server side language, that is used to render output in HTML, so you can assign PHP variable in JavaScript variable not JavaScript to php, you can send JavaScript variable to Server by AJAX

Try this way

<SCRIPT language="JavaScript"> 
var q1=Math.floor(Math.random()*11) 
function sendAJAX(q1)
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
         alert('posted value' + xmlhttp.responseText);
    }
  }
xmlhttp.open("GET","http://yourdomain.com/?q=" + q1 ,true);
xmlhttp.send();
}
</SCRIPT>

And in PHP code

<?php 
$ff = $_GET['q'];
echo $ff;
?>

6 Comments

Can yo suggest how to achieve this for my example with a code
Thanks , I m just trying this. BTW i am running on a localhost(localhost/php2js) , How do i modify this xmlhttp.open("GET","yourdomain.com/?q" + q1 ,true);
@Girish_Jangid Is there something i am missing from the expected result, Please correct me if i am wrong jsfiddle.net/Va8d4
this couldn't implement in JSFIDDLE, please don't bear up and read more about AJAX working
Its not working.. I am getting error undefined q in php
|
1

The HTML file:

<script src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
<script>
var q1=Math.floor(Math.random()*11);
$(document).ready(function(){
    $.ajax({url:"ajax.php",data: {"q1":q1}}).done(function(data){console.log(data)});
});
</script>

The PHP file: ajax.php

<?php
$ff = filter_input(INPUT_GET, 'q1', FILTER_VALIDATE_INT);
echo $ff;
?>

References:

jquery

PHP

5 Comments

Is there something i am missing to achieve the expected result I am nt able to pass the variable. Please find the attached files dropbox.com/sh/9gfrgo1j8ojs95f/HvbMrnFxFT
from the looks of this, you dont need PHP at all. You could achieve this with java script. Your server will not read the php code inside a HTML file. Did you just want to write out the value of q1 inside the body tag in your HTML file?
I actually want the value of q1 from JS to store in database. Am i doing it correct, send JS variable to PHP and from PHP to mysql?
Yes, is this just a concept? If all you wanted to store in a db was a random number, PHP could do this without JS using the rand() function.
This is just a concept, all i am looking is to find the connectivity to pass my variable from JS to store in SQL
1
you can use like this.

<html> 
    <head>
      <script type="text/javascript">
          var q1="hello"; 

          <?php 
             $ff ="<script>document.write(q1)</script>";             
          ?>
       </script>
   </head>
<body>
<?php echo $ff; ?>
 </body> 
</html> 

1 Comment

This would result in $ff being string "<script>document.write(q1)</script>", but if you echo it on the page script will execute and you will see "Hello" on the page. But if you insert $ff to DB "<script>document.write(q1)</script>" will be inserted instead of "hello"
0
you can use like this.

<html> 
    <head>
      <script type="text/javascript">
          var q1="hello"; 

          <?php 
             $ff ="<script>document.write(q1)</script>";             
          ?>
       </script>
   </head>
<body>
<?php echo $ff; ?>
 </body> 
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.