harleyquinn Posted July 8, 2007 Share Posted July 8, 2007 I really need some help, it's probably a very simple fix, but I just can't seem to wrap my head around php -- feeling very stupid right now. This is my problem: I have a simple Tell A Friend form and it works fine, but what I can't figure out is how to add some text above the form when a user initially goes to the page where the form lives and then change depending on their actions. Right now the errors show up just fine, its the text I want to show initally that I can't get to work. And if you look at my code below, if I added my desired text right above where the form tags are, it sort of works, but then my errors appear above my text, I want the text to be above the errors -- am I making any sense? So for instance, when one would first go to the page, it would say something like: <h1>Send this page to a Friend Form</h1> <p>some other text here</p> Form here Then if there is an error, the text would change to: <h1>Send this page to a Friend Form</h1> <p>Error(s) : </p> Form here But if there are no errors, it would just say: <h1>Thanks for telling a friend<h1> <p>some other text here</p> (no form) this is the code: <?php if (empty($params['emailsubject'])){ echo '<div class="formError">An email subject must be specified in order to use this plugin, using the format emailsubject="Your subject here"</div>'; return; }else{ $subject = $params['emailsubject']; $canned = $params['cannedtext']; } $style = true; // Use default styles if (FALSE == empty($params['style']) && $params['style'] === "false" ) $style = false; // Except if "false" given in params // Default styles $inputStyle = 'style="width:100%;border: 1px solid black; margin:0 0 5px 0;"'; // input boxes $taStyle = 'style="word-wrap:break-word; overflow-x:hidden; overflow-y:auto; width:100%; border: 1px solid black; margin:0 0 5px 0; font-size:.8em; font-family:arial,helvetica,sans-serif;"'; // TextArea boxes $formStyle = 'style="width:300px; line-height:90%;"'; // form $errorsStyle = 'style="color: white; background-color: red; font-weight: bold; border: 3px solid black; margin: 5px;"'; // Errors box (div) $labelStyle = 'style="display:block; font-size:.8em; font-family:arial,helvetica,sans-serif;"'; $buttonStyle = 'style="float:left; width:50%;"'; $fieldsetStyle = 'style="padding:12px; border:1px solid #999999; text-align:left;"'; $captchaStyle = 'style="margin-bottom:5px; text-align: center;"'; $errors=$to=$name=$email=$message = ''; $pageURL = "\r\n\r\nhttp"; if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } $message = $canned . $pageURL ; if($_SERVER['REQUEST_METHOD']=='POST'){ if (!empty($_POST['subject'])) $subject = tafsanitize($_POST['subject']); if (!empty($_POST['to'])) $to = tafsanitize($_POST['to']); if (!empty($_POST['name'])) $name = tafsanitize($_POST['name']); if (!empty($_POST['email'])) $email = tafsanitize($_POST['email']); if (!empty($_POST['canned'])) $canned = $_POST['canned']; if (!empty($_POST['message'])) $message = $_POST['message']; if (FALSE == empty($params['captcha']) && $params['captcha'] && isset($gCms->modules['Captcha'])) { if (!empty($_POST['captcha_resp'])) { $captcha_resp = $_POST['captcha_resp']; } } //Mail headers $extra = "From: $name <$email>\r\n"; $charset = isset($gCms->config['default_encoding']) && $gCms->config['default_encoding'] != '' ? $gCms->config['default_encoding'] : 'utf-8'; $extra .= "Content-Type: text/plain; charset=" . $charset . "\r\n"; if (empty($name)) $errors .= "\t\t<li>" . 'Please enter your Name' . "</li>\n"; if (empty($email)) $errors .= "\t\t<li>" . 'Please enter your Email Address' . "</li>\n"; elseif (!tafvalidEmail($email)) $errors .= "\t\t<li>" . 'Your Email Address is not valid' . "</li>\n"; if (empty($to)) $errors .= "\t\t<li>" . 'Please enter your Friend\'s Email Address' . "</li>\n"; elseif (!tafvalidtoEmail($to)) $errors .= "\t\t<li>" . 'Your Friend\'s Email Address is not valid' . "</li>\n"; if (empty($message)) $errors .= "\t\t<li>" . 'Please Enter a Message' . "</li>\n"; if (FALSE == empty($params['captcha']) && $params['captcha'] && isset($gCms->modules['Captcha'])) { if (empty($captcha_resp)) $errors .= "\t\t<li>" . 'Please enter the text in the image' . "</li>\n"; elseif (! ($captcha->checkCaptcha($captcha_resp))) $errors .= "\t\t<li>" . 'The text from the image was not entered correctly' . "</li>\n"; } if (!empty($errors)) { echo '<div class="formError" ' . (($style) ? $errorsStyle:'') . '>' . "\n"; echo '<p>Error(s) : </p>' . "\n"; echo "\t<ul>\n"; echo $errors; echo "\t</ul>\n"; echo "</div>"; } elseif (@mail($to, $subject, $message, $extra)) { echo '<div class="formMessage">Your message was successfully sent.</div>' . "\n"; return; } else { echo '<div class="formError" ' . (($style) ? $errorsStyle:'') . '>Sorry, the message was not sent. Please try again in a few moments.</div>' . "\n"; return; } } if (isset($_SERVER['REQUEST_URI'])) { $action = $_SERVER['REQUEST_URI']; } else { $action = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : ''; if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') { $action .= '?'.$_SERVER['QUERY_STRING']; } } ?> <!-- TELLAFRIEND_FORM --> <form action="<?php echo $action ?>" class="tellafriendform" method="post" <?php echo ($style) ? $formStyle:''; ?>> <fieldset <?php echo ($style) ? $fieldsetStyle:''; ?>> <label for="name" <?php echo ($style) ? $labelStyle:''; ?> >Your name :</label> <input type="text" id="name" name="name" value="<?php echo htmlspecialchars($name); ?>" <?php echo ($style) ? $inputStyle:''; ?>/> <label for="email" <?php echo ($style) ? $labelStyle:''; ?> >Your email address : </label> <input type="text" id="email" name="email" value="<?php echo htmlspecialchars($email); ?>" <?php echo ($style) ? $inputStyle:''; ?>/> <label for="to" <?php echo ($style) ? $labelStyle:''; ?> >Your friend's email address : </label> <input type="text" id="to" name="to" value="<?php echo htmlspecialchars($to); ?>" <?php echo ($style) ? $inputStyle:''; ?>/> <label for="message" <?php echo ($style) ? $labelStyle:''; ?> >Message : </label> <textarea id="message" name="message" rows="5" cols="48" <?php echo ($style) ? $taStyle:''; ?>><?php echo $message; ?></textarea> <?php if (FALSE == empty($params['captcha']) && $params['captcha'] && isset($gCms->modules['Captcha'])) { ?> <label for="captcha_resp" <?php echo ($style) ? $labelStyle:''; ?> >Enter the text from the image below : </label> <input type="text" id="captcha_resp" name="captcha_resp" value="" <?php echo ($style) ? $inputStyle:''; ?>/> <?php echo "<div $captchaStyle>" . $captcha->getCaptcha() . '</div>'; } ?> <input type="submit" class="button" value="Send!" <?php echo ($style) ? $buttonStyle: ''; ?> /> <input type="reset" class="button" value="Reset" <?php echo ($style) ? $buttonStyle: ''; ?> /> </fieldset> </form> <!-- END of TELLAFRIEND_FORM --> Can someone please help? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 8, 2007 Share Posted July 8, 2007 Instead of echo'ing the error to the browser, store it in a variable instead. Then where you want the error to appear show the contents of the variable. Something like this to store the error: $err='This is an error message'; Then to show the error: <?=$err?> Which is exactly the same as <?php echo $err; ?> Quote Link to comment Share on other sites More sharing options...
harleyquinn Posted July 8, 2007 Author Share Posted July 8, 2007 That was a speedy reply! Thanks. My errors are displaying ok. Its the initial text one sees when they first get to the page that I want to show which would then be replaced by the errors or thank you message depending on the submit results. Any other ideas? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 8, 2007 Share Posted July 8, 2007 At the very start of the script assign $err your intro message. Next woukld be your checking routines giving $err an error message if something went wrong. Next you'd simply dump the contents of $err to the browser as described above. Result? When the script is run for the first time you'll be setting your $err variable to your intro message. As no submit has been clicked and no checks are being made on the data no error messages will beshown. Once the user clicks a submit button the errors (if generated) would overwrite the defualt intro message. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 8, 2007 Share Posted July 8, 2007 Example: <?php $msg='Please enter your name'; if ($_POST['subsend']) { $name=$_POST['name']; if (empty($name)) { $msg='You did not enter anything'; } else { //everything is ok } } ?> <html> <body> <?=$msg?><br /><br /> <form action="" method="post"> Name: <input type="text" name="name" /> </form> </body> </html> Should explain it a bit better. Quote Link to comment Share on other sites More sharing options...
harleyquinn Posted July 8, 2007 Author Share Posted July 8, 2007 I tried your suggestion, but it still kept that intro text below the error messages - I want it to go above the error message. Here's a link to my test page to show exactly what's happening: http://www.ecosimplify.com/form/. Just hit the send button and you will see what I mean. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.