0

I have one php file where I have a number input field. I can grab the value of my input field with jQuery and store it in a javascript variable, but now I want to pass this javascript variable in to a php variable. How easily can I achieve it ?

<input id="custom_price" type="number" class="custom_price" name="custom_price" value="10" min="10" max="1000">  

<script type="text/javascript">
     jQuery("#custom_price").live("change", function () {
        var user_amount = jQuery('#custom_price').val();    
     });         
</script>
<?php $final_value = *How can I pass user_amount here* ?> 
Saif Islam
  • 191
  • 1
  • 5
  • 17

5 Answers5

3

You can't. You need to send that variable to server using GET or POST method to save in a PHP variable.

Alok Patel
  • 7,842
  • 5
  • 31
  • 47
1

PHP is confusing, because two (or three) different scopes are running within the same sourcecode file.

Your sample has plain PHP:

<?php $final_value = *How can I pass user_amount here* ?>

Plain HTML:

<input id="custom_price" type="number" class="custom_price" name="custom_price" value="10" min="10" max="1000">  

<script type="text/javascript">
</script>

and plain JavaScript:

 jQuery("#custom_price").live("change", function () {
    var user_amount = jQuery('#custom_price').val();    
 });         

HTML and JavaScript run on the client (web-browser) side while PHP runs on the server side.

One step back:

A HTTP access has two basic different steps**:

  1. Client (Webbrowser) sends a request
  2. Server sends a response

That's all, no more magic, nothing inbetween: One question, one answer. Further communication requires additional HTTP requests (and responses).

Whenever a browser sends a request to yourscript.php, that request is received, all arguments are parsed and your script is starting. All PHP code sections are processed and everything else plain unknown static data.

In the end, the static data plus the dynamic output from your script are send back to the browser as one package. End of communication.

Each HTML page may contain one (or more) forms which tell the browser that additional information (arguments) should be included in the next request:

<form action="myscript2.php">
    <input type="hidden" name="foo" value="bar">
    <input type="submit" name="button" value="Send request">
</form>

A click on the submit button creates a completely new request, adds the values of the fields named foo and button and sends (submits) everything to the server.

Again the target PHP script is running and could output something, but not on the previously delivered web page***.

** Some techniques are working more or less slightly different, like continuation requests or websockets, but they're way behind the scope of this question.

*** JavaScript/Ajax could be used to manipulate a web page without reloading it, but thats again just a request, response and Javascript source processing the response on client side.

Sebastian
  • 2,472
  • 1
  • 18
  • 31
1

When a user visits the site, the PHP code is executed on the server and then sent to to the user. Javascript, on the other hand, is sent to the user and then executed on that user's machine. In a basic definition, this illustrates the difference between 'front-end' and 'back-end' development.

That being said. You CAN use PHP to insert values into javascript code, as that can happen on the server and then be executed by the user's machine. However, you CANNOT pass a javascript value to a PHP variable (within the same script/page) because the PHP has already been executed on the server by the time it makes it to your web browser.

As was mentioned by others, the solution is to send the (javascript) data to a PHP script on your server via a network call. You could utilize jQuery.ajax() to do this in your javascript, then your PHP script will need to accept the data and do something with it.

Josh
  • 403
  • 7
  • 12
1

Indeed , there is a way to embed Js Inside PHP code , but , is it applicable or not , this is another issue

see the following simple code :

<input id="custom_price" type="number" class="custom_price" name="custom_price" value="10" min="10" max="1000"> 

<?php       $final_value ="<script type=\"text/javascript\">
        var e = document.getElementById('custom_price').value;document.write(e); </script>";

echo $final_value;  ?>
Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18
0

First remember php is run on server side and javascript is run in client side. So to pass the variable to the server/php you need to send it via http request. You could for example use ajax:

$.post('file.php', { 'varname': 'varvalue'});

Also you will need to capture the parameter on you server side:

#file.php
<?php
$value = $_POST["varname"];
?>