Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-22-2008, 08:14 PM   PM User | #1
JaymeNYC
New Coder

 
Join Date: Apr 2008
Posts: 46
Thanks: 3
Thanked 0 Times in 0 Posts
JaymeNYC is an unknown quantity at this point
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?
JaymeNYC is offline   Reply With Quote
Old 07-22-2008, 08:23 PM   PM User | #2
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,227
Thanks: 2
Thanked 189 Times in 187 Posts
djm0219 is on a distinguished road
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
djm0219 is offline   Reply With Quote
Users who have thanked djm0219 for this post:
JaymeNYC (07-22-2008)
Old 07-22-2008, 08:26 PM   PM User | #3
JaymeNYC
New Coder

 
Join Date: Apr 2008
Posts: 46
Thanks: 3
Thanked 0 Times in 0 Posts
JaymeNYC is an unknown quantity at this point
thx! didnt think id get a response that fast it works perfectly
JaymeNYC is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:29 AM.


Advertisement
Log in to turn off these ads.