简体   繁体   English

使用 ACTION 和 POST 方法在搜索表单中使用值重定向到 URL

[英]Redirect to URL with value in a search form using ACTION and POST method

I have a search form, where user enter a keyword:我有一个搜索表单,用户在其中输入关键字:

<form method="post" action="domain.com/gallery/<?php echo $_POST["something"]; ?>" >
<input type="text" name="something" required/>
<input type="submit"/>
</form>

<?php
if (isset($_POST["something"])) {
echo $_POST["something"];
}
?>

I want go to url "domain.com/gallery/keyword" after form submission.我想在提交表单后转到 url "domain.com/gallery/keyword"。

The problem is that I can't, with the code above it redirects to "domain.com/gallery/", without keyword value...问题是我不能,上面的代码重定向到“domain.com/gallery/”,没有关键字值......

I'm using POST and ACTION, because "isset($_POST["something"])" is not working with other methods like onsubmit, eg "onsubmit="window.location = 'domain.com/gallery/' + something.value;我正在使用 POST 和 ACTION,因为“isset($_POST["something"])” 不适用于 onsubmit 等其他方法,例如“onsubmit="window.location = 'domain.com/gallery/' + something。价值; return false;""返回假;""

Try this尝试这个

<form method="post" action="{samePage}.php" >
<input type="text" name="something" required/>
<input type="submit"/>
</form>

<?php
if (isset($_POST["something"])) {
$url="domain.com/gallery/".<?php echo $_POST["something"]; 
header("Location: $url")?>
}
?>

I think you have to change the value of action attribute.我认为您必须更改 action 属性的值。 You can doing it with javascript:你可以用javascript来做:

<form method="post" action="domain.com/gallery/" onSubmit="changeTarget(this)">

The javascript: javascript:

function changeTarget(currentForm)
{
   //read initial attribute value
   actionAttribute = currentForm.getAttribute('action');
   //read form field value
   enteredKeyword =  currentForm.omething.value;
   //Concantinate both values and set it as new target
   currentForm.setAtrribute('action') = actionAttribute + enteredKeyword;
   
   //Nessacary return statement
   return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM