繁体   English   中英

提交表单元素后如何将网页重定向到其他网页

[英]How to Redirect webpage to different webpage after submitting form elements

我正在设计一个网页,我需要在其中使用单选按钮作为表单元素。 请检查下面的代码...

<form action="glitter.php" method="post">
<input type="radio" name="font" value="fonts/darkcrystaloutline.ttf"/>
<input type="radio" name="font1" value="fonts/darkcrystalout.ttf"/> 
<input type="radio" name="font2" value="fonts/darkcrystalout2.ttf"/> 
<input type="submit">
</form> 

上面的代码通过将单选按钮值提交到“ glitter.php”可以正常工作,但是我需要的是首先将值提交到“ glitter.php”。 提交后,该网页应重定向到另一个网页(例如:“ kothi.html”)。 有没有一种方法可以通过使用PHP或JQuery来实现。 请帮助我解决此问题。

您可以使用header("location: http://domainname/path/to/kothi.html");

在这里找到手册

您想使用PHP的header()函数重定向它,以发送一个Location头(在所有表单处理之后,在spark.php上)。

header("Location: /kothi.html"); //Assuming kothi.html is 
                                 //found on the root of the website.
die();                           //Stop processing

另外,如果您希望单选按钮真正起作用,则需要给它们起相同的名称(否则它们将不会分组)

<form action="glitter.php" method="post">
    <input type="radio" name="font" value="fonts/darkcrystaloutline.ttf"/>
    <input type="radio" name="font" value="fonts/darkcrystalout.ttf"/>
    <input type="radio" name="font" value="fonts/darkcrystalout2.ttf"/>
    <input type="submit">
</form> 

// Glitter.php页面

<?php
if(isset($_POST['font']))
{
// Do you stuff then redirect
header("location: yourPage.html");
// adding exit() thanks to MrJ

exit();
}
?>
header("location: http://domainname/path/to/kothi.html");

要么

header("location: ./kothi.html");

都可以。

我们将使用Ajax来做到这一点。 首先,我们将获取表单,并使用e.preventDefault();停止默认操作。 然后,我们将获取表单的值。 使用jQuery强大的$ .ajax方法,我们将url设置为我们的glitter.php文件,我们的数据作为查询字符串发送,因此,'mysite.com /?val = OurValue。 我们的价值就是“ val”的价值,这就是我们提交的表格。 成功功能使我们仅在成功提交表单后才可以引导页面。 top.location.href允许我们执行标准重定向到本地或外部所需的任何位置。 我在成功功能中保留了“响应”,因此您可以决定做什么和何时进行。

希望这可以帮助。

$("#my_form").submit(function(e){
  e.preventDefault();
  val = $(this).val();
  $.ajax({
    url: 'glitter.php',
    data: 'val='+val,
    success: function(data, response){
      top.location.href = 'http://www.google.com'
    }
  });
});

如何在表单中添加“操作”?

暂无
暂无

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

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