Appending GET parameters to URL from <form> action

雨燕双飞 提交于 2019-12-04 02:13:44

问题


So say I'm currently on index.php or index.php?p=about within my current web build.

I am trying to build a search form that will be displayed on most pages, but I want the form action to go to http://mywebsiteurl.com/?p=search&q=GETDATA, as my website's paging depends on the data passed to the 'p' attribute.

How would I append the search parameter to the URL in a static fashion, upon submission?


回答1:


Perhaps something like this:

  <form method="get" action="index.php">
     <input type="hidden" name="p" value="search" />
     <input type="text" name="search" value="" />
     <input type="submit" value="search" />
  </form>



回答2:


You can use a hidden field in your form to maintain the value of the p parameter:

<input type="hidden" 
       name="p" 
       value="<?= htmlentities($_GET['p'], ENT_QUOTES) ?>" />



回答3:


You should put the value of the parameter p inside a hidden form field inside the search form; something like:

<input type="hidden"
    name="p"
    value="<?php echo(htmlspecialchars($_REQUEST["p"])); ?>" />

It's not a good idea to put the parameter to the form action parameter; post requests are handled differently than GET requests, the values in a POST request aren't appended to the URL by ? and & as with GET; meaning that you wouldn't actually get the p parameter into the script handling the POST request from the form...

Also take care not to show the request parameter unreflected (hence the htmlspecialchars, thanks for the hint!), since malicious clients could try to inject code into your page (HTML injection / XSS).



来源:https://stackoverflow.com/questions/8476602/appending-get-parameters-to-url-from-form-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!