0

API の歌詞コードを動作させることができました。私が直面しているのは小さな問題です: ユーザーがテキストボックスに曲名を入力して送信ボタンをクリックすると、getElementById を介して値を取得し、それを以下の URL に追加するにはどうすればよいですか?

これが私のコードです:

<?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>

このコードのどこが間違っているかを教えてください。このフォーラムのすべての助けに感謝します! :)

4

2 に答える 2

0

私があなたの質問を理解している限り、あなたがする必要があるのは次のとおりです。

<?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
?>

次に、フォーム入力フィールドを次のように変更します。

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

POST も値を受け入れるかどうかはわかりませidん!

于 2012-04-17T16:14:51.640 に答える
-1

不要な JavaScript を削除します。PHP はサーバーで実行されます。クライアントでの Javascript

次に、PHP コードを次のように変更します。

if(isset($_POST['value'])) {
    $res = file_get_contents("http://webservices.lyrdb.com/lookup.php?q=". $_POST['value'] ." &for=trackname&agent=agent");
}
于 2012-04-17T16:16:10.257 に答える