1

I am using a button on my form. I am trying to redirect to another page on button click after successful submission of user info. Every thing works properly except redirection. Here is the code I tried.

Html code:

<form role="form" id="contact-form" method="get">
  <div class="form-group row">
      <input type="email" id="email" name="email" placeholder="Enter your email" required="required" class="form-control input-lg" />
      <input type="text" id="address" name="address" placeholder="Enter your address" required="required" class="form-control input-lg" />
      <button type="submit" class="btn btn-t-primary">Show Me Now!</button>
  </div>
</form>

Javascript code:

function contact() {
  $("#contact-us-form").submit(function (e) {
  e.preventDefault();
  e.stopImmediatePropagation();
  form = $(this);
  data = $(this).serialize();
  $.post("contact.php", data, function(response){
      form.trigger('reset');
  });
}

Php code:

<?php 
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
if (isset($_POST['email'])) {
    $email = $_POST['email'];
    $address= $_POST['address'];
    $subject = "Message from: ".$email;
    $content = "Email: " . $email."\n"
            . "Address: " . $address;
    $headers ='Reply-To: ' . $email . "\r\n";
    mail('[email protected]', $subject ,$content, $headers );
    header("Location:https://www.example.com");
    echo 1;
}else {
    echo 0;
}

?>

5
  • This example code has nothing to do with javascript. Commented Jul 31, 2016 at 23:33
  • What's really the problem ! What happens when you submit the form ? Commented Jul 31, 2016 at 23:37
  • Don't echo anything after issuing a redirect header. In fact, the only thing after it should be an immediate exit; to prevent any further code from executing. Commented Jul 31, 2016 at 23:46
  • When I click the button, It's send the info at email but not redirecting to any where. Commented Jul 31, 2016 at 23:55
  • Unrelated to the question itself, but if this would ever be production code, please properly check your user inputs to prevent a mail header injection. Commented Aug 1, 2016 at 0:23

7 Answers 7

2

You can't redirect an ajax request from PHP, instead use this:

$(function() {

    $("#contact-form").submit(function(e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        form = $(this);
        data = $(this).serialize();
        $.post("contact.php", data, function(response) {
            form.trigger('reset');
            //Redirection
            window.location.href = "https://www.google.com";
        });
    });

});

Just remove this header("Location:https://www.example.com"); from your PHP.

I hope this will help you.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way to do this using php?
@WaseemBarcha yes, you can but you have to send data using the classic way (without ajax), header(...) will redirect the page.
1

Maybe you have to write the entire url when you are setting the header:

header("Location: http://www.google.com");

Comments

0

This isn't actually necessary.

Inside of the HTML form tag, add where you want to post the data to inside of the action attribute.

<form role="form"  id="contact-form" method="get" action="postpage.php"> <!-- notice the action attribute !-->

2 Comments

In the question they want to redirect to google.com, so this won't work in this case since Google isn't going to be page handling the post.
@Soviut They want to redirect to Google once they have mailed something to someone on the next page. They don't want Google as such to handle the post.
0

Try this!

echo '<meta http-equiv=REFRESH CONTENT=0;url=./yourDestination.php>';

As we can see in the code, the page would refresh to the url you declared.

"Content = 0" means that it'll refresh after 0 seconds You could modify the number if you want!

Comments

0

I wonder why you have set form method="get", but used $_POST variable under php code. Here's another version using POST method, try if it helps.

<?php
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
if (isset($_POST['email'])) {
    $email = $_POST['email'];
    $address= $_POST['address'];
    $subject = "Message from: ".$email;
    $content = "Email: " . $email."\n"
            . "Address: " . $address;
    $headers ='Reply-To: ' . $email . "\r\n";
    mail('[email protected]', $subject ,$content, $headers );
    header("location: https://google.com");
    exit;
}
?>

<form role="form" id="contact-form" method="post">
  <div class="form-group row">
      <input type="email" id="email" name="email" placeholder="Enter your email" required="required" class="form-control input-lg" />
      <input type="text" id="address" name="address" placeholder="Enter your address" required="required" class="form-control input-lg" />
      <button type="submit" class="btn btn-t-primary">Show Me Now!</button>
  </div>
</form>

2 Comments

It is still possible that blank email is sent (as no $_POST variables are set). You could actually check your inbox to verify this. BTW, is [email protected] your real email address? :)
Oh just noticed that you have also used Javascript to post the form, so it is setting the POST vars. In my example however, I have placed both the form and php code into a single file so no Javascript is needed for submission of the form. Please adapt the one that best works for you.
0

You need to set the absolute address, including the protocol (https://) in your header location if you want to redirect to a new site.

header("Location: https://www.google.com")

Without the protocol, the location is assumed to be relative and will be appended to the existing domain.

Additionally, you should not echo anything after the headers.

9 Comments

I think that's what the OP already did ! I'm curious why it's not working !
Updated my answer to reflect the question.
Yes! but even with header("Location:www.google.com") it still redirect to http://op-domain/www.google.com !
Right, because it's missing the protocol like I've mentioned in my answer.
I don't think this is the real problem because it's easy to notice that ! Instead, I think there is some sort of output before the header, especially from the mail function .
|
0

First you have to write PHP code that handles Ajax Request:

if (isset($_POST['ajax_request'])) {
    // write code;
    echo "http://www.example.com"; // redirect url
}

Javascript:

 jQuery(document).ready(function($) {
    $.post('/path/to/file.php', {"ajax_request": true}, function(data, textStatus, xhr) {
        window.location.href = data; // http://www.example.com
    });
});

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.