简体   繁体   中英

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM