简体   繁体   English

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

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

I am designing a webpage where I need to use radio buttons as form elements. 我正在设计一个网页,我需要在其中使用单选按钮作为表单元素。 Please check the code below... 请检查下面的代码...

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

The above code works fine by submitting the radio button values to "glitter.php", but what I need is for values to first be submitted to "glitter.php". 上面的代码通过将单选按钮值提交到“ glitter.php”可以正常工作,但是我需要的是首先将值提交到“ glitter.php”。 After submission, the webpage should be redirected to another web page (ex: "kothi.html"). 提交后,该网页应重定向到另一个网页(例如:“ kothi.html”)。 Is there a way to do this by using PHP or JQuery. 有没有一种方法可以通过使用PHP或JQuery来实现。 Please help me fix this problem.... 请帮助我解决此问题。

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

Find the manual here 在这里找到手册

You want to redirect it using PHP's header() function, to send a Location header (on glitter.php, after all the form processing). 您想使用PHP的header()函数重定向它,以发送一个Location头(在所有表单处理之后,在spark.php上)。

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

Also, if you want your radio buttons to actually work, you need to give them all the same name (otherwise they won't group) 另外,如果您希望单选按钮真正起作用,则需要给它们起相同的名称(否则它们将不会分组)

<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 page // 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");

OR 要么

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

would both work. 都可以。

We'll use Ajax to do this. 我们将使用Ajax来做到这一点。 First, we'll grab the form and we'll stop the default action with e.preventDefault();. 首先,我们将获取表单,并使用e.preventDefault();停止默认操作。 Then we'll grab the value of the form. 然后,我们将获取表单的值。 with jQuery's robust $.ajax method, we set the url to our glitter.php file, our data is sent as a query string, so, 'mysite.com/?val=OurValue. 使用jQuery强大的$ .ajax方法,我们将url设置为我们的glitter.php文件,我们的数据作为查询字符串发送,因此,'mysite.com /?val = OurValue。 Our value is the value of 'val', which is whatever is our form submitted. 我们的价值就是“ val”的价值,这就是我们提交的表格。 The success function allows us to direct the page only after the form has been successfully submitted. 成功功能使我们仅在成功提交表单后才可以引导页面。 top.location.href allows us to perform a standard redirect to wherever we want, locally, or externally. top.location.href允许我们执行标准重定向到本地或外部所需的任何位置。 I left 'response' in the success function so you can dictate what to do and when to do it. 我在成功功能中保留了“响应”,因此您可以决定做什么和何时进行。

Hope this helps. 希望这可以帮助。

$("#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