inasiopo
inasiopo

Reputation: 55

Jquery mobile with php for email, php won't redirect to page

I have a html page with jquery mobile.

I am having a button that calling a php script to send an email.

HTML

       <form id="contact_form" action="contact-form.php" method="POST" enctype="multipart/form-data" data-ajax="false">
            <div class="row">
                <label for="name">Your name:</label><br />
                <input id="name" class="input" name="name" type="text" value="" size="30" /><br />
            </div>
            <div class="row">
                <label for="email">Your email:</label><br />
                <input id="email" class="input" name="email" type="text" value="" size="30" /><br />
            </div>
            <div class="row">
                <label for="message">Your message:</label><br />
                <textarea id="message" class="input" name="message" rows="7" cols="30"></textarea><br />
            </div>
            <input id="submit_button" type="submit" value="Send email" />
        </form>     

Php is sending email and is then supposed to redirect to main page, using window.location.href='http://www.xxxxx.com/main.html'

PHP:

<?php
 $action=$_REQUEST['action'];

     $name=$_POST['name'];
     $email=$_POST['email'];
$message=$_POST['message'];
$mail_to='[email protected]';

if (($name=="")||($email=="")||($message==""))
    {
    echo "All fields are required, please fill <a href=\"\">the form</a>     again.";
    }
else{
    $subject = ' New message ' . $field_name;
    $body_message = 'From: '.$name."\n";
    $body_message .= 'E-mail: '.$email."\n";
    $body_message .= 'Message: '.$message;
    $headers = "From: $cf_email\r\n";
    $headers .= "Reply-To: $cf_email\r\n";

    $mail_status = mail($mail_to, $subject, $body_message, $headers);
    mail($email,$subject,$body_message,$headers); 
    }

 if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
    alert('Thank you for the message. I will contact you shortly.');
    window.location.href = 'http://www.example.com/Portfolio.html';
</script>
 <?php
 }
 else { ?>
<script language="javascript" type="text/javascript">
    alert('Message failed.');
    window.location.href = 'http://www.example.com/Portfolio.html';
</script>
 <?php
 }?>

 ?>

It does not work!! The email is sent but it won't redirect.

I know it has to do with how the page is loaded with jquery mobile, but I can't figure out how to solve it.

Any help please???

Upvotes: 1

Views: 45

Answers (1)

Victor Luna
Victor Luna

Reputation: 1814

May I suggest using window.location.replace()this method will replace the current page with a new one in the current browser window. replace() removes the current page from the history, so it does not go back to the original page.

window.location.replace("http://www.example.com/Portfolio.html");

More information about the method:

Upvotes: 2

Related Questions