dongtuojuan8998 2017-07-29 03:56
浏览 15
已采纳

如何从用户读取并将其放在地址栏中?

I am creating a web application in PHP. I need a task to be done that is I want to take an input from a user in a search form and the term user have given must be added to address for a specific task.

For example: User is given an input such as obama now I want it should be added to address bar URL such as www.somedomain.com/something/obama.

<html>
   <head>
   <title>Test Page</title>
   <link rel="stylesheet" href="style.css">
   </head>
   <body>

   <form class="form-wrapper" method="POST" 
   action="http://www.somedomainname.com/">
   <input type="text" id="search" placeholder="Search for..." required >
   <input  class="form-control" type="submit" value="go" id="submit">
   </form>

   </body> 
</html>

Now I want that what user give as input then it must redirect to www.somedomainname.com/searchterm.

  • 写回答

2条回答 默认 最新

  • doufang8282 2017-07-29 04:26
    关注

    1)Your input for search does not have a name. So the post will not work.

    2)Your form action call the php page handling the form.

    3)Use header() to redirect

    Try something like this.

    <?php 
    
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
       $name = trim( $_POST['name'] );
       $name = htmlentities( $name, ENT_QUOTES | ENT_HTML5, $encoding = 'UTF-8' );
    
       header('Location:http://somedomain.com'.$name);
    
     }
    ?>
    
    <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
     <div>
       <label for="name">Text Input:</label>
       <input type="text" name="name" id="name" value="" tabindex="1" />
      </div>
     <div>
     <input type="submit" value="Submit" />
     </div>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多