The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP Form Validation Redirect Issue
Discuss PHP Form Validation Redirect Issue in the PHP Development forum on Dev Shed. PHP Form Validation Redirect Issue PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 4th, 2013, 06:39 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
Time spent in forums: 3 h 51 m 56 sec
Reputation Power: 0
|
|
|
PHP Form Validation Redirect Issue
Hello,
I'm not sure what I'm doing wrong here. The goal here is to validate my form and when it's successful the email is sent and the user is redirected. However, when the PHP form is loaded, you're instantly redirected and the form is not loaded.
PHP Code:
<?php
$emailSubject = 'Blah Blah';
$webMaster = 'email address';
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$domain = $_POST['domain'];
$start_time = $_POST['start_time'];
$message = $_POST['message'];
$nameErr = "";
$emailErr = "";
$phoneErr = "";
$messageErr = "";
$body = <<<EOD
<b>Contact Info</b>
<hr>
Name: $name<br>
Phone: $phone<br>
Email: $email<br>
<br>
<b>Project Info</b>
<hr>
Domain Name Needed? $domain<br>
Project Start Time: $start_time<br>
Details: $message<br>
EOD;
if(isset($_POST['submit'])) {
if (empty($_POST["name"])) {
$nameErr = "This field is required.";
}
else {
$name = $_POST["name"];
}
if (empty($_POST["email"])) {
$emailErr = "This field is required.";
}
else if(filter_var($email, FILTER_VALIDATE_EMAIL) == FALSE){
$emailErr = "Please specify a valid email address.";
}
else {
$email = $_POST["email"];
}
if (empty($_POST["phone"])) {
$phoneErr = "This field is required.";
}
else if(!preg_match("/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/", $phone)){
$phoneErr = "Please specify a valid phone number.";
}
else {
$phone = $_POST["phone"];
}
if (empty($_POST["message"])) {
$messageErr = "This field is required.";
}
else {
$message = $_POST["message"];
}
}
mail($webMaster, $emailSubject, $body, $headers);
header( "Location: (URL)" );
?>
|

January 4th, 2013, 07:00 AM
|
|
|
|
Please edit your post and fix the tags ([ PHP ] not [PHPNET]). It is very hard to read but it appears to me that the mail and header calls are misplaced. They probably need to be inside the 'if' block. They currently appear to be executed whether the form is submitted or not.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

January 4th, 2013, 07:25 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
Time spent in forums: 3 h 51 m 56 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by gw1500se Please edit your post and fix the tags ([ PHP ] not [PHPNET]). It is very hard to read but it appears to me that the mail and header calls are misplaced. They probably need to be inside the 'if' block. They currently appear to be executed whether the form is submitted or not. |
Fixed the tags. Could you elaborate a bit on your response please. Thank you.
|

January 4th, 2013, 07:43 AM
|
|
|
|
Now that I can read your code more easily, it is behaving exactly as you programmed it. The first time the page is displayed, $_POST['Submit'] is not set so it skips all the code except for the 'mail' and 'header' calls. That is why it immediately displays the redirection page. You need to simply move the last '}' after those calls.
|

January 4th, 2013, 09:26 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
Time spent in forums: 3 h 51 m 56 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by gw1500se Now that I can read your code more easily, it is behaving exactly as you programmed it. The first time the page is displayed, $_POST['Submit'] is not set so it skips all the code except for the 'mail' and 'header' calls. That is why it immediately displays the redirection page. You need to simply move the last '}' after those calls. |
I moved
PHP Code:
mail($webMaster, $emailSubject, $body, $headers);
header( "Location: (URL)" );
into the last '}' and I still get the same results. It redirects as soon as I hit the page.
|

January 4th, 2013, 09:41 AM
|
|
|
|
Not possible if you did what I said. Re-post all your code as there must be something else you are not showing.
|

January 4th, 2013, 10:17 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
Time spent in forums: 3 h 51 m 56 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by gw1500se Not possible if you did what I said. Re-post all your code as there must be something else you are not showing. |
This is what I have...
PHP Code:
<?
$emailSubject = 'blah blah';
$webMaster = 'email address';
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$domain = $_POST['domain'];
$start_time = $_POST['start_time'];
$message = $_POST['message'];
$nameErr = "";
$emailErr = "";
$phoneErr = "";
$messageErr = "";
$body = <<<EOD
<b>Contact Info</b>
<hr>
Name: $name<br>
Phone: $phone<br>
Email: $email<br>
<br>
<b>Project Info</b>
<hr>
Domain Name Needed? $domain<br>
Project Start Time: $start_time<br>
Details: $message<br>
EOD;
if(isset($_POST['submit'])) {
if (empty($_POST["name"])) {
$nameErr = "This field is required.";
}
else {
$name = $_POST["name"];
}
if (empty($_POST["email"])) {
$emailErr = "This field is required.";
}
else if(filter_var($email, FILTER_VALIDATE_EMAIL) == FALSE){
$emailErr = "Please specify a valid email address.";
}
else {
$email = $_POST["email"];
}
if (empty($_POST["phone"])) {
$phoneErr = "This field is required.";
}
else if(!preg_match("/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/", $phone)){
$phoneErr = "Please specify a valid phone number.";
}
else {
$phone = $_POST["phone"];
}
if (empty($_POST["message"])) {
$messageErr = "This field is required.";
}
else {
$message = $_POST["message"];
}
mail($webMaster, $emailSubject, $body, $headers);
header( "Location: URL");
}
?>
|

January 4th, 2013, 10:32 AM
|
|
|
|
As far as I can tell that will do absolutely nothing. Since there is no form the code can never be executed.
|

January 4th, 2013, 10:58 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
Time spent in forums: 3 h 51 m 56 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by gw1500se As far as I can tell that will do absolutely nothing. Since there is no form the code can never be executed. |
Here's the form:
Code:
<?php
include('validate.php');
?>
<html>
<body>
<form id="form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<fieldset>
<legend>Contact Info</legend>
<label>Your Name<span class="red">*</span></label>
<input type="text" name="name" id="name" class="required letters" value="<?php echo htmlspecialchars($name); ?>"/>
<label class="error"><?php echo $nameErr; ?></label>
<label>Email<span class="red">*</span></label>
<input type="text" name="email" id="email" class="required email" value="<?php echo ($email); ?>" />
<label class="error"><?php echo $emailErr; ?></label>
<label>Phone<span class="red">*</span></label>
<input type="text" name="phone" id="phone" class="required phoneUS" value="<?php echo htmlspecialchars($phone); ?>"/>
<label class="error"><?php echo $phoneErr; ?></label>
</fieldset>
<fieldset>
<legend>Project Details</legend>
<label>Need a Domain Name?</label>
<input type="radio" id="radio" name="domain" value="No"/><span class="style1">No</span>
<input type="radio" id="radio" name="domain" value="Yes"/><span class="style1">Yes</span>
<input type="radio" id="radio" name="domain" value="Not Sure" /><span class="style1">Not Sure</span>
<label>If No, Current Domain Name?</label>
<input type="text" name="url" value="http://"/>
<label>Project Start Time</label>
<input type="radio" id="radio" name="start_time" value="1-2 Weeks"/><span class="style1">1-2 Weeks</span>
<input type="radio" id="radio" name="start_time" value="3-4 Weeks"/><span class="style1">3-4 Weeks</span>
<input type="radio" id="radio" name="start_time" value="1 Month+"/><span class="style1">1 Month+</span>
<input type="radio" id="radio" name="start_time" value="Not Sure" checked="checked"/><span class="style1">Not Sure</span>
<label>Project Description<span class="red">*</span></label>
<textarea name="message" id="message" class="required"><?php echo htmlspecialchars($message); ?></textarea>
<label class="error"><?php echo $messageErr; ?></label>
<label class="style"><span class="red">*</span> Required Fields</label>
</fieldset>
<input type="submit" id="submit" name="submit" class="submit" value="Submit" />
</form>
</body>
</html>
|

January 4th, 2013, 11:17 AM
|
|
|
Do I assume that 'validate.php' contains the code you posted? If so you need to move the 'if (isset' to surround the include and don't use it in the php file itself. In other words only execute the include if the form has been submitted and in that PHP file assume that it is only executed if the form has been submitted.
PHP Code:
<?php
if (isset($_POST['submit'])) {
include('validate.php');
}
?>
|

January 4th, 2013, 11:35 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
Time spent in forums: 3 h 51 m 56 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by gw1500se Do I assume that 'validate.php' contains the code you posted? If so you need to move the 'if (isset' to surround the include and don't use it in the php file itself. In other words only execute the include if the form has been submitted and in that PHP file assume that it is only executed if the form has been submitted.
PHP Code:
<?php
if (isset($_POST['submit'])) {
include('validate.php');
}
?>
|
Curious to know where I would place the redirect script.
|

January 4th, 2013, 12:09 PM
|
|
|
|
I thought it was already in 'validate.php'. If not then you have still not posted all the requisite code.
|

January 4th, 2013, 04:10 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
Time spent in forums: 3 h 51 m 56 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by gw1500se I thought it was already in 'validate.php'. If not then you have still not posted all the requisite code. |
This is my 'validate.php' and the redirect script is at the very bottom:
PHP Code:
<?php
$emailSubject = 'Blah Blah';
$webMaster = 'email address';
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$domain = $_POST['domain'];
$start_time = $_POST['start_time'];
$message = $_POST['message'];
$nameErr = "";
$emailErr = "";
$phoneErr = "";
$messageErr = "";
$body = <<<EOD
<b>Contact Info</b>
<hr>
Name: $name<br>
Phone: $phone<br>
Email: $email<br>
<br>
<b>Project Info</b>
<hr>
Domain Name Needed? $domain<br>
Project Start Time: $start_time<br>
Details: $message<br>
EOD;
if(isset($_POST['submit'])) {
if (empty($_POST["name"])) {
$nameErr = "This field is required.";
}
else {
$name = $_POST["name"];
}
if (empty($_POST["email"])) {
$emailErr = "This field is required.";
}
else if(filter_var($email, FILTER_VALIDATE_EMAIL) == FALSE){
$emailErr = "Please specify a valid email address.";
}
else {
$email = $_POST["email"];
}
if (empty($_POST["phone"])) {
$phoneErr = "This field is required.";
}
else if(!preg_match("/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/", $phone)){
$phoneErr = "Please specify a valid phone number.";
}
else {
$phone = $_POST["phone"];
}
if (empty($_POST["message"])) {
$messageErr = "This field is required.";
}
else {
$message = $_POST["message"];
}
}
mail($webMaster, $emailSubject, $body, $headers);
header( "Location: (URL)" );
?>
|

January 4th, 2013, 04:25 PM
|
|
|
First, if you put the 'if' I suggested around the include then you no longer need it in 'validate.php'.
PHP Code:
<?php
$emailSubject = 'Blah Blah';
$webMaster = 'email address';
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$domain = $_POST['domain'];
$start_time = $_POST['start_time'];
$message = $_POST['message'];
$nameErr = "";
$emailErr = "";
$phoneErr = "";
$messageErr = "";
$body = <<<EOD
<b>Contact Info</b>
<hr>
Name: $name<br>
Phone: $phone<br>
Email: $email<br>
<br>
<b>Project Info</b>
<hr>
Domain Name Needed? $domain<br>
Project Start Time: $start_time<br>
Details: $message<br>
EOD;
if (empty($_POST["name"])) {
$nameErr = "This field is required.";
}
else {
$name = $_POST["name"];
}
if (empty($_POST["email"])) {
$emailErr = "This field is required.";
}
else if(filter_var($email, FILTER_VALIDATE_EMAIL) == FALSE){
$emailErr = "Please specify a valid email address.";
}
else {
$email = $_POST["email"];
}
if (empty($_POST["phone"])) {
$phoneErr = "This field is required.";
}
else if(!preg_match("/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/", $phone)){
$phoneErr = "Please specify a valid phone number.";
}
else {
$phone = $_POST["phone"];
}
if (empty($_POST["message"])) {
$messageErr = "This field is required.";
}
else {
$message = $_POST["message"];
}
mail($webMaster, $emailSubject, $body, $headers);
header( "Location: (URL)" );
?>
Second, I don't understand your question about where to put the redirect. You already have it in 'validate.php'.
|

January 4th, 2013, 04:44 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
Time spent in forums: 3 h 51 m 56 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by gw1500se First, if you put the 'if' I suggested around the include then you no longer need it in 'validate.php'.
PHP Code:
<?php
$emailSubject = 'Blah Blah';
$webMaster = 'email address';
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$domain = $_POST['domain'];
$start_time = $_POST['start_time'];
$message = $_POST['message'];
$nameErr = "";
$emailErr = "";
$phoneErr = "";
$messageErr = "";
$body = <<<EOD
<b>Contact Info</b>
<hr>
Name: $name<br>
Phone: $phone<br>
Email: $email<br>
<br>
<b>Project Info</b>
<hr>
Domain Name Needed? $domain<br>
Project Start Time: $start_time<br>
Details: $message<br>
EOD;
if (empty($_POST["name"])) {
$nameErr = "This field is required.";
}
else {
$name = $_POST["name"];
}
if (empty($_POST["email"])) {
$emailErr = "This field is required.";
}
else if(filter_var($email, FILTER_VALIDATE_EMAIL) == FALSE){
$emailErr = "Please specify a valid email address.";
}
else {
$email = $_POST["email"];
}
if (empty($_POST["phone"])) {
$phoneErr = "This field is required.";
}
else if(!preg_match("/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/", $phone)){
$phoneErr = "Please specify a valid phone number.";
}
else {
$phone = $_POST["phone"];
}
if (empty($_POST["message"])) {
$messageErr = "This field is required.";
}
else {
$message = $_POST["message"];
}
mail($webMaster, $emailSubject, $body, $headers);
header( "Location: (URL)" );
?>
Second, I don't understand your question about where to put the redirect. You already have it in 'validate.php'. |
The problem originally was that when the script executes it redirects without performing the validation. I've tried numerous things and still no good.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|