anevins Posted June 10, 2011 Share Posted June 10, 2011 I'm working on a contact form and I want the instructions to hide from view once the user submits successfully. I've currently implemented this through the form; as the form disappears once submitted but am having trouble using the instruction html elements. The variable $form_message holds the html elements I want to hide on successful submit: <?php $form_message = true; if ($form_message){ ?> <h1> Need to contact me? </h1> <p> Please fill out your details and press the submit button to email me. <span class="bold">All fields are required</span>, thank you.</p><br /> <?php } ?> This is the entire document: <div id="form"> <?php $form_message = true; if ($form_message){ ?> <h1> Need to contact me? </h1> <p> Please fill out your details and press the submit button to email me. <span class="bold">All fields are required</span>, thank you.</p><br /> <?php } include_once('php/functions.php'); $firstname = $_POST['first_name']; $lastname = $_POST['last_name']; $email = $_POST['email']; $reason = $_POST['reason']; $form = true; if(isset($_POST['submit'])){ if (empty($firstname)){ echo ' <script> $("span.fn_error").slideDown("slow", function()); </script> '; echo '<span class="error">Please enter your First name.<br /></span>'; } if (empty($lastname)){ echo '<span class="error">Please enter your Last name.<br /></span>'; } if (check_email_address($email)==false){ echo '<span class="error">Please enter a valid email address.<br /></span>'; } if (empty($reason)){ echo '<span class="error">Please enter your reason to contact me.<br /></span>'; } if (!empty($firstname) && !empty($lastname) && (check_email_address($email)) && !empty($reason)){ $form = false; $form_message = false; echo '<p><span class="green">Thank you </span>'. $firstname .' '. $lastname . ' for taking the time to contact me.<br />I will try to get back to you through your email <span class="bold">'.$email.' </span>as soon as possible.</p><br />'; echo '<p>In the mean time, feel free to look at my Flickr and Blogger feeds.</p><br />'; } } if ($form){ ?> <form id="contact" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table> <tr><td>First name:</td><td><input type="text" name="first_name" value="<?php echo $firstname; ?>"/></td></tr> <tr><td></td><td><?php echo $firstname_error; ?></td></tr> <tr><td>Last name: </td><td><input type="text" name="last_name" value="<?php echo $lastname; ?>"/></td></tr> <tr><td></td><td><?php echo $lastname_error; ?></td></tr> <tr><td>Email Address: </td><td><input type="text" name="email" value="<?php echo $email; ?>"/></td></tr> <tr><td></td><td><?php echo $email_error; ?></td></tr> <tr><td>Reason: </td><td><textarea rows="5" cols="16" name="reason" ><?php echo $reason; ?></textarea></td></tr> <tr><td></td><td><?php echo $reason_error; ?></td></tr> <tr><td> </td><td class="submit"><input type="submit" name="submit" id="submit" value="submit" /></td></tr> </table> </form> </div> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238963-cant-get-html-to-hide-through-php/ Share on other sites More sharing options...
ryanfilard Posted June 10, 2011 Share Posted June 10, 2011 I use this: <?php $form_message = true; if ($form_message) echo ' <h1> Need to contact me? </h1> <p> Please fill out your details and press the submit button to email me. <span class="bold">All fields are required</span>, thank you.</p><br /> '; ?> Quote Link to comment https://forums.phpfreaks.com/topic/238963-cant-get-html-to-hide-through-php/#findComment-1227865 Share on other sites More sharing options...
anevins Posted June 10, 2011 Author Share Posted June 10, 2011 Thanks for your help Ryan, unfortunately that gives the same problem. Quote Link to comment https://forums.phpfreaks.com/topic/238963-cant-get-html-to-hide-through-php/#findComment-1227870 Share on other sites More sharing options...
KevinM1 Posted June 10, 2011 Share Posted June 10, 2011 The HTML will always show because you're always setting $form_message as true. What you need to do is check if the form has been submitted first, then set $form_message accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/238963-cant-get-html-to-hide-through-php/#findComment-1227877 Share on other sites More sharing options...
anevins Posted June 10, 2011 Author Share Posted June 10, 2011 I thought that was what I was doing: if (!empty($firstname) && !empty($lastname) && (check_email_address($email)) && !empty($reason)){ $form = false; $form_message = false; } $form = false; works and this is the same technique I'm trying to do with $form_message. Quote Link to comment https://forums.phpfreaks.com/topic/238963-cant-get-html-to-hide-through-php/#findComment-1227879 Share on other sites More sharing options...
KevinM1 Posted June 10, 2011 Share Posted June 10, 2011 I thought that was what I was doing: if (!empty($firstname) && !empty($lastname) && (check_email_address($email)) && !empty($reason)){ $form = false; $form_message = false; } $form = false; works and this is the same technique I'm trying to do with $form_message. Remember, PHP scripts are executed from the top down. Having: $form_message = true; if ($form_message) { ?> <!-- html --> <?php } Will simply spit out the HTML where it is. Why does it work for $form? It works because you set $form as false before you check for it and try to output the form. Again, top-down execution. Quote Link to comment https://forums.phpfreaks.com/topic/238963-cant-get-html-to-hide-through-php/#findComment-1227882 Share on other sites More sharing options...
cyberRobot Posted June 10, 2011 Share Posted June 10, 2011 I would recommend that you don't use of $_SERVER['PHP_SELF'] in the form's action attribute. For more information on the security risks behind $_SERVER['PHP_SELF'], check out the following article: http://www.mc2design.com/blog/php_self-safe-alternatives Instead, you could use the actual page name or leave the action attribute blank. Quote Link to comment https://forums.phpfreaks.com/topic/238963-cant-get-html-to-hide-through-php/#findComment-1227888 Share on other sites More sharing options...
anevins Posted June 10, 2011 Author Share Posted June 10, 2011 Thank you Cyber Robot, I have taken your advice. Quote Link to comment https://forums.phpfreaks.com/topic/238963-cant-get-html-to-hide-through-php/#findComment-1227890 Share on other sites More sharing options...
anevins Posted June 10, 2011 Author Share Posted June 10, 2011 Nightslyer, I don't know how to circumvent this problem you pointed out. Could you please give me some advice to how to hide that html on successful form submit? Your help so far has been appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/238963-cant-get-html-to-hide-through-php/#findComment-1227891 Share on other sites More sharing options...
KevinM1 Posted June 10, 2011 Share Posted June 10, 2011 The best solution would be to have all your form processing within the following conditional: if (isset($_POST['submit')) { $form_message = false; // more form processing } if (isset($form_message) && $form_message == true) { // display HTML } Quote Link to comment https://forums.phpfreaks.com/topic/238963-cant-get-html-to-hide-through-php/#findComment-1227893 Share on other sites More sharing options...
anevins Posted June 10, 2011 Author Share Posted June 10, 2011 Thank you so much, I have solved this problem from your help Nightslyer. All I did was put the part I wanted to hide in with the other part of the form, that works. ... if ($form){ ?> <!-- This is the bit I just moved inside the if($form), which originally just held the form but now the instructions.--> <h1> Need to contact me? </h1> <p> Please fill out your details and press the submit button to email me. <span class="bold">All fields are required</span>, thank you.</p><br /> <form id="contact" method="post" action="contact.php"> <table> ... Quote Link to comment https://forums.phpfreaks.com/topic/238963-cant-get-html-to-hide-through-php/#findComment-1227904 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.