1

I have a form in my html document and when press submit button, the form calls the file form.php. Now in the form.php, i want to redirect it back to my home page and reset the previous filled form. I have tried the code below but I got a blank page. The Google url is just default.

<?php
if ($_POST['submit']) {
    if (mail ($to, $subject, $body, $from)) { 
  // To redirect to home page
  header("Location:http://www.google.com");
    } else { 
        echo '<p>Oops! An error occurred. Try sending your message again.</p>'; 
    }
}
?>

Thanks for any help guys.

5 Answers5

2

You can try the javascript way of redirecting the page:

Example :

    $url='http://www.google.com';

    echo '<script>window.location = "'.$url.'";</script>';
    die;

It may help ...!!

Sharry India
  • 341
  • 1
  • 9
1

Probably you wrote some echo or print before this header(...). Try ob_start();.

For more info follow this

Community
  • 1
  • 1
Ayush choubey
  • 606
  • 6
  • 23
0

I didn't see your HTML, but have you named your submit button with the attribute name="submit" ?

Other case $_POST['submit'] will be empty and your script do nothing.

iag
  • 166
  • 4
0

Hi I have used the code which you have. I can able to redirect to google.Please check the below code..In my view I think the problem may be the mail function.check if the mail is coming or not..If mail comes then it would definetly redirects to the google page..

<?php
if ($_POST['submit']) 
 {
    $to="test@gmail.com";
    $subject="googletest";
    $body="test";
    if (mail ($to, $subject, $body)) 
    { 
        // To redirect to home page
        header("Location:http://www.google.com");
    } else { 
        echo '<p>An error occurred. Try sending your message again.</p>'; 
    }
}
?>
<form name="form" id="form" action="form.php" method="post">
<input type="submit" value="submit" name="submit">
</form>
Choco
  • 1,054
  • 1
  • 7
  • 20
  • Yeap thats my html too. I got the email when submit. It just won't redirect. But I've used echo ''; instead of header and it's fine now. Btw do you know how to make this topic solved? – Nawshad Ghannoo Oct 09 '14 at 07:06
0

Try this :

<?php
if ($_POST['submit']) {
   $to="abc@gmail.com";
   $subject="Mail";
   $body="redirect";
   $from="xyz@gmail.com";
    if (mail($to, $subject, $body, $from)) { 
        // To redirect to home page
        header("Location:http://www.google.com");
        exit();
    } else { 
        echo '<p>Oops! An error occurred. Try sending your message again.</p>'; 
    }
}
?>  

Its working on my local server.

Debug Diva
  • 26,058
  • 13
  • 70
  • 123