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**:
- Client (Webbrowser) sends a request
- 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.