redirect to new page after form submitted and vali

2019-09-06 21:09发布

问题:

I have been trying to improve our contact forms with php validation, I can get the form to be validated and show errors when each field is not filled in correctly, success message appears when the form is completed and validated correctly, and an email is sent to me with the information from the form.

My problem is that I can not get the header('location'...) part of the code to work. Rather than just having "Form has been submitted successfully" appear underneath the form once submitted, I would like it to go to a "Thank You" page instead.

Here is my code:

This is this my form page:

<?php 
include('validate.php'); 
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>TEST</title>
<style>    
    input, textarea {font-size: 1em;}
    p.error {background: #ffd; color: red;}
    p.error:before {content: "Error:";}
    p.success {background: #ffd; color: green;}
    p.success:before {content: "Success:";}
    p.error, p.success {font-weight: bold;}
</style>
</head>
<body>
<h2>Please fill up the form below and submit.</h2>
<?=$error?>
<form action="html_form_to_validate.php" method="post">
<table>
<tr>
   <td>Name: </td>
<td>
<input type="text" name="name" placeholder="Name *(Required)" value="<?=@$name?>"/>    </td>
</tr>
<tr>
    <td>Company: </td>
<td><input type="text" name="company" placeholder="Company" value="<?=@$company?>"/>    </td>
</tr>
<tr>
<td>Hear: </td>
<td><input type="text" name="hear"  placeholder="How did you hear about us?" value="<?=@$hear?>"/></td>
</tr>
<tr>
<td>Email: </td>
<td><input type="text" name="email" value="<?=@$email?>"/></td>
</tr>
<tr>
<td>Phone: </td>
<td><input type="text" name="phone" value="<?=@$phone?>"/></td>
</tr>
<tr>
<td>Message: </td>
<td><textarea name="comments"><?=@$comments?></textarea></td>
</tr>
</table>            
<input type="submit" name="submit" value="Submit"/> <input type="reset" name="reset" value="Reset"/>
</form>

<?php
if (isset($_POST['submit']) && $error == '') { // if there is no error, then process further
echo "<p class='success'>Form has been submitted successfully.</p>"; // showing success message

$name=$_POST['name']; 
$company=$_POST['company']; 
$telephone=$_POST['telephone']; 
$email=$_POST['email']; 
$hear=$_POST['hear']; 
$comments=$_POST['comments']; 

$to="chris@example.com";
$subject="Request for information";
$from="sales@example.com";
$message="<html>
<body topmargin='0'>

<div align='center'>
<table border='0' width='736' id='table1' cellspacing='0' cellpadding='0'>
<tr>
<td height='129' bgcolor='#EDEDE9' width='736' valign='top' style='margin-left: 10'>
<table border='0' id='table2' cellspacing='10' cellpadding='15' width='726'>
<tr>
<td width='676' valign='top' bgcolor='#FFFFFF'>
<p align='left'>
<img border='0' src='http://www.example.com/images/logo.png' align='left' hspace='0'>    </p>
<p align='left'>
<br><br>
<b>
<font face='Verdana' color='#0078c1' style='font-size: 20pt'>
&nbsp;&nbsp;Request for information</font></b></p>
<p align='left'>&nbsp;</p>
</td>
</tr>
<tr>
<td width='676' valign='top' bgcolor='#FFFFFF'>
<p>
<font face='Verdana' size='2'>The following person has been on <a href='http://www.example.com'>
<font color='#0078c1'>www.example.com</font></a> and requesting information from our 'contact form'.</font></p>
<p>
<font face='Verdana' size='2'>Name: </font><font face='Verdana' size='2'><b>$name</b>    </font></p>
<p>
<font face='Verdana' size='2'>Company: </font><font face='Verdana' size='2'><b>$company</b></font></p>
<p>
<font face='Verdana' size='2'>Telephone: <font face='Verdana' size='2'><b>$telephone</b></font></p>
<p>
<font face='Verdana' size='2'>Email: <font face='Verdana' size='2'><b>$email</b></font></p>
<p>
<font face='Verdana' size='2'>Heard about us from: </font><font face='Verdana' size='2'><b>$hear</b></font></p>
<p>
<font face='Verdana' size='2'>Message: <font face='Verdana' size='2'><b>$comments</b></font></p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</body>
</html>

";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers  .= "From: $from\r\n"; 
@mail($to, $subject, $message, $headers);
header('Location: http://www.example.com/contact/thank-you.php'); 
}
?> 
</body>
</html>

And my validate.php file:

<?php

$error = ""; // Initialize error as blank

if (isset($_POST['submit'])) { // check if the form is submitted
#### removing extra white spaces & escaping harmful characters ####
$name             = trim($_POST['name']);
$company         = trim($_POST['company']);
$hear             = trim($_POST['hear']);
$email                 = $_POST['email'];
$phone                = $_POST['phone'];
$comments                = $_POST['comments'];

#### start validating input data ####
#####################################

# Validate Name #

    // if name is not 3-20 characters long, throw error
    if (strlen($name) < 3 OR strlen($name) > 20) {
        $error .= '<p class="error">Name should be within 3-20 characters long.</p>';
    }

# Validate Email #
    // if email is invalid, throw error
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { // you can also use regex to do same
        $error .= '<p class="error">Enter a valid email address.</p>';
    }

# Validate Phone #
    // if phone is invalid, throw error
    if (!ctype_digit($phone) OR strlen($phone) < 9) {
        $error .= '<p class="error">Enter a valid telephone number.</p>';
    }

# Validate Comments #
    if (strlen($comments)==0 OR strlen($comments)>240) {
        $error .= '<p class="error">Please enter your message less than 240 characters.</p>';
    }

#### end validating input data ####
#####################################
}

I am a novice and have used scripts from other places to do what I wanted to do - so be nice lol

Thanks

Chris

回答1:

In my PHP code I always use header("refresh:0;url=the_url_here"); instead of header('Location: url');.

Try it, maybe it works to you.



回答2:

I personally love this method...

echo '<meta http-equiv="refresh" content="0;URL=www.mylink.com" />';


回答3:

Validation and redirect needs to happen before any output to the browser, so you need to place header function before showing form.

Or if you must do redirect after form you can use javascript redirect instead of PHP.

<script>
    // just redirect
    window.location='http://your-site.dev/new-url.php'
    // or redirect after showing form
    setTimeout(function () {
        window.location.href = 'http://your-site.dev/new-url.php'; 
    }, 2000); //will call the function after 2 secs.
</script>