<input type="hidden" id="hidden_sum_input" name="sum_input" value="">
<script> var sum = $('#hidden_sum_input').val();
<?php $totsum = ?>
</script>
how will i take the value from var sum and assign it to the php variable $totsum?
var sum_of_pass = 0;
sum_of_pass += no_of_adult;
sum_of_pass += no_of_children;
$("#hidden_sum_input").val(sum_of_pass);
i am getting the sum correctly in this variable sum_of_pass which i am keeping inside a hidden input now i want to use the value inside input tag as a php variable and do condition checks.
Thanks in advance!
2
Answers
PHP is run server-side. JavaScript is run client-side in the browser of the user requesting the page. By the time the JavaScript is executed, there is no access to PHP on the server whatsoever. Please read this article with details about client-side vs server-side coding.
What happens in a nutshell is this:
In your case, PHP will write the JS code into the page, so it can be executed when the page is rendered in your browser. By that time, the PHP part in your JS snippet does no longer exist. It was executed on the server already. It created a variable $result that contained a SQL query string. You didn’t use it, so when the page is send back to your browser, it’s gone. Have a look at the sourcecode when the page is rendered in your browser. You will see that there is nothing at the position you put the PHP code.
The only way to do what you are looking to do is either:
with the values you want to be insert into the database.
According to me, you can write the PHP code in another file and can access the input values of HTML using name or value attributes.
Hence, try writing PHP code in separate file and call that file in HTML.
Happy Coding!!!