Echo JavaScript variable in PHP
I want to jump the page based on input in php using javascript.
I am trying this but failed.
<?php
echo '<input type="number" name=???inputID??? id=???inputID??? ></input>';
// Here I am failed not able to get the input value and jump to the page
echo "<a href = 'demo-page.php?&page=+document.getElementById('inputID').value'>Jump</a>";
?>
Kindly suggest what I am doing wrong.
Also is this approach jump to the page is right or wrong?
Any idea or suggestion would be welcome.
Answer
Solution:
If you want to get the value of a field when you click the link, then you need to execute a javascript code when the onclick event fires in the link. The href property does not fires javascript.
So the solution would be:
echo '<a href="#" onClick="window.location = \'demo-page.php?&page=\' + document.getElementById(\'inputID\').value + \'">Jump</a>';
This will result in the following html code:
<a href="#" onClick="window.location = 'demo-page.php?&page=' + document.getElementById('inputID').value + '">Jump</a>
Be carefull with quotes!
Source