简体   繁体   中英

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". After submission, the webpage should be redirected to another web page (ex: "kothi.html"). Is there a way to do this by using PHP or JQuery. Please help me fix this problem....

You can use 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).

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

<?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. First, we'll grab the form and we'll stop the default action with 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. Our value is the value of 'val', which is whatever is our form submitted. 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. 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'
    }
  });
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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