Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep 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($emailFILTER_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)" );

    ?>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,501
    Rep Power
    594
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,501
    Rep Power
    594
    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.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,501
    Rep Power
    594
    Not possible if you did what I said. Re-post all your code as there must be something else you are not showing.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    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($emailFILTER_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");
    }
    ?>
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,501
    Rep Power
    594
    As far as I can tell that will do absolutely nothing. Since there is no form the code can never be executed.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    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>
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,501
    Rep Power
    594
    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');
    }
    ?>
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    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.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,501
    Rep Power
    594
    I thought it was already in 'validate.php'. If not then you have still not posted all the requisite code.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    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($emailFILTER_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)" ); 

    ?>
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,501
    Rep Power
    594
    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($emailFILTER_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'.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    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($emailFILTER_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.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo