-2

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.

karel
  • 5,489
  • 46
  • 45
  • 50
Soniya Jain
  • 21
  • 1
  • 3
  • 2
    `“` < those curly quotes if that is what you're really using, are causing a parse error. – Funk Forty Niner Jul 23 '20 at 13:44
  • 1
    Replace the `“` with `"` and try again. If that's all this is, then there's a duplicate for this. – Funk Forty Niner Jul 23 '20 at 13:47
  • `'demo-page.php?&page=+document.getElementById('inputID').value'` < That won't execute `document.getElementById('inputID').value` as javascript. It will be read as literal text and will be a part of the URL. Even if it were to be executed as javascript, it would do it on page load, before the user have a chance to enter anything into the input. – M. Eriksson Jul 23 '20 at 13:50
  • I replaced but still giving error. echo 'Jump'; – Soniya Jain Jul 23 '20 at 13:52
  • 1
    `?&` < is a syntax error. The ampersand is for additional data; remove it. – Funk Forty Niner Jul 23 '20 at 13:53
  • @MagnusEriksson So what is the good practice sir? How to tackle this problem, kindly suggest. – Soniya Jain Jul 23 '20 at 13:53
  • Btw, your input has no value. Unsure what you want to do here and where the data is coming from. – Funk Forty Niner Jul 23 '20 at 13:55
  • I would add a click event on the link (using javascript), where I then get the value from the input and append it to the URL before redirecting the client to the URL. – M. Eriksson Jul 23 '20 at 13:55
  • You should go and have a good, thorough read of https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming first of all. The code you have shown clearly shows that you have not understood some very important basics yet. – CBroe Jul 23 '20 at 13:56
  • @Funk Forty Niner I have removed ?& but still giving me the error. The value is blank because user input the page number and when it click on the jump then the page redirect there. I think instead of href I have also used onclick function but failed. – Soniya Jain Jul 23 '20 at 13:58

1 Answers1

0

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!

David Rojo
  • 2,337
  • 1
  • 22
  • 44