如何在提交表单元素后将网页重定向到不同的网页


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> 

上面的代码通过将单选按钮值提交到"splitter.php"可以很好地工作,但我需要的是首先将值提交到"splitter.php"。提交后,网页应该重定向到另一个网页(例如:"kothi.html")。有没有办法使用php或JQuery来做到这一点。请帮我解决这个问题。。。。

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

在此处查找手册

您希望使用PHP的header()函数重定向它,以发送Location标头(在glitter.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=我们的价值。我们的价值是"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'
    }
  });
});

在表单中添加一个"操作"怎么样?