Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    46
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Display error/thanks DIV after form submitted

    I have my errors and thank u msgs in a div below my form, it has padding that makes the bottom of my site look strange since there is nothing there so I put display:none on it. How do I enable it after the form is clicked? heres my form

    PHP Code:
    <?php
                                    
    // To better handle Validation errors we need to put the fields into an array for easier handling
                                    
    $FIELDS = array("Your Name" => "name""Your Email" => "email""Your Message" => "txt""Anti-Spam" => "question");
                                    
    ?>
                                      <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>#contactform">
                                    <?php
                                    
    // Get question and answer for Anti-spam
                                    
    $rand rand(0,count($QUESTIONS)-1);    // randomize question being asked
                                    
    list($q$a) = $QUESTIONS[$rand];        // split question and answer up into seperate variables
                                    
    $_SESSION['answer'] = $a;                // Answer for the question gets stored in a SESSION
                        
                                    
    foreach($FIELDS as $fieldname => $name) {
                                    
    $style "";            // Used for changing background color of input field .
                                
                                    // if there are errors we want to change the style of the input boxes to inform user 
                                    
    if(isset($errors) && $errors[$name] == 1$style "style=\"border: 1px solid #d25e55;\"";
                                
                                    echo 
    "<div class=\"grp\">";
                                    switch(
    $name) {
                                    case 
    'txt':
                                    
    // need a textarea instead of an input box
                                    
    echo "<label for=\"{$name}\">{$fieldname}</label><br />\n";
                                    echo 
    "<textarea id=\"{$name}\" {$style} name=\"{$name}\" class=\"\" rows=\"\" cols=\"\" onfocus=\"this.style.backgroundColor='#3c3c3f'\" ".
                                    
    "onblur=\"this.style.backgroundColor='#2d2d30'\" >";
                                    if(isset(
    $_POST[$name])) echo $_POST[$name];
                                    echo 
    "</textarea>\n";
                                    break;
                                    case 
    'question':
                                        
                                    echo 
    "<label for=\"{$name}\">{$q}</label><br />\n";
                                    echo 
    "<input class=\"\" {$style} id=\"{$name}\" type=\"text\" name=\"{$name}\" onfocus=\"this.style.backgroundColor='#3c3c3f'\" ".
                                    
    "onblur=\"this.style.backgroundColor='#2d2d30'\" />";
                                    break;
                                        
                                    default:
                                    echo 
    "<label for=\"{$name}\">{$fieldname}</label><br />\n";
                                    echo 
    "<input class=\"\" {$style} id=\"{$name}\" type=\"text\" name=\"{$name}\" onfocus=\"this.style.backgroundColor='#3c3c3f'\" ".
                                    
    "onblur=\"this.style.backgroundColor='#2d2d30'\" value=\"";
                                    if(isset(
    $_POST[$name])) echo $_POST[$name];
                                    echo 
    "\" />\n";
                                    break;
                                    }
                                    echo 
    "</div>";
                                    }
                                    
    ?>
                            <div class="grp2">
                                <input class="formsubmit" type="submit" value="Submit" name="submit" />
                                
                            </div>
                                </form>
                        </div>
                    </div>
                    <div id="ereport">
                                <p>
                                <?php
                                
    // If errors found display DIV block
                                
    if(isset($errMsg)) echo $errMsg;
                        
                                
    // If form successfully submitted, thank the user.
                                
    if(isset($_GET['thankyou'])) {
                                echo 
    $THANKYOU;
                                }
                                
    ?>
                                </p>
                        </div>
    what would I change the display to so it works? and is this where I would do it?

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,306
    Thanks
    4
    Thanked 205 Times in 202 Posts
    Put your div inside your PHP if test so it only gets used if there's an error.

    Code:
    <?php
    // If errors found display DIV block
    if (isset($errMsg)) {
    ?>
    <div id="ereport">
    <p>
    <?php echo $errMsg; ?>
    </p>
    </div>
    <?php
    }
    // If form successfully submitted, thank the user.
    if(isset($_GET['thankyou'])) {
    echo $THANKYOU;
    }
    ?>
    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    JaymeNYC (07-22-2008)

  • #3
    New Coder
    Join Date
    Apr 2008
    Posts
    46
    Thanks
    3
    Thanked 0 Times in 0 Posts
    thx! didnt think id get a response that fast it works perfectly


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •