doubi4435 2012-04-17 16:06
浏览 37

如何在此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! :)

  • 写回答

2条回答 默认 最新

  • douzhuolong9886 2012-04-17 16:14
    关注

    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!

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)