【问题标题】:Redirect to URL with value in a search form using ACTION and POST method使用 ACTION 和 POST 方法重定向到搜索表单中具有值的 URL
【发布时间】:2020-10-02 09:07:02
【问题描述】:

我有一个搜索表单,用户在其中输入关键字:

<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"];
}
?>

我想在提交表单后转到 url "domain.com/gallery/keyword"。

问题是我不能,上面的代码重定向到“domain.com/gallery/”,没有关键字值...

我正在使用 POST 和 ACTION,因为 "isset($_POST["something"])" 不适用于其他方法,例如 onsubmit,例如“onsubmit="window.location = 'domain.com/gallery/' + something.value;返回假;""

【问题讨论】:

  • 将您的表单提交到同一页面,并使用 $_POST 获取数据,然后执行redirect
  • 谢谢,这个解决方案就像一个魅力!祝你有美好的一天。
  • 很高兴看到您通过我的评论解决了这个问题! ;)

标签: javascript php html jquery


【解决方案1】:

试试这个

<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")?>
}
?>

【讨论】:

    【解决方案2】:

    我认为您必须更改 action 属性的值。你可以用javascript来做:

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

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      相关资源
      最近更新 更多