如何在此PHP代码中捕获文本框值并将其附加到URL?

I managed to get my API lyrics code working. All I'm facing is a small problem: When a user enters a song name in a textbox and clicks the submit button, I catch the value via getElementById, and then how do I append it with the URL below?

Here's my code:

<?php
     //Catches the value of the Submit button: 
     $submit = isset($_POST['submit']);
     if($submit) {
?>
     <script type="text/javascript">
         var val1 = document.getElementById('val').value;
     </script>
<?php
    /* The below $res contains the URL where I wanna append the caught value. 
       Eg: http://webservices.lyrdb.com/lookup.php?q=Nothing Else Matters(Or 
       what the user searches for)&for=trackname&agent=agent
    */
      $res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q='+val1+' &for=trackname&agent=agent");
?>
<html>
   <form method="post" action="">
      Enter value: <input type="text" name="value" id="val" /><br/>
      <input type="submit" value="Submit" name="submit" />
   </form>
</html>

Could you please correct me as to where I'm making a mistake in this piece of code, highly appreciate all help in this forum! :)

As far as I understand your question, what you need to do is:

<?php
     //Catches the value of the Submit button: 
     $submit = isset($_POST['submit']);
     if($submit) {
     $val1 = $_POST['val'];    // TO store form passed value in "PHP" variable.
    /* The below $res contains the URL where I wanna append the caught value. 
       Eg: http://webservices.lyrdb.com/lookup.php?q=Nothing Else Matters(Or 
       what the user searches for)&for=trackname&agent=agent
    */
      $res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q=' . urlencode($val1) . ' &for=trackname&agent=agent");
   }   // This is to end the "if" statement
?>

and then change the form input field to:

      Enter value: <input type="text" name="val" id="val" /><br/>

I am not sure if POST accepts id values too!

Remove the javascript it's unnecessary. PHP runs at the server. Javascript at the client.

Then change your PHP code to this:

if(isset($_POST['value'])) {
    $res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q=". $_POST['value'] ." &for=trackname&agent=agent");
}