tefuzz Posted March 10, 2009 Share Posted March 10, 2009 still new to PHP...i searched but i couldn't find exactly what i was hoping for. I have a simple contact form . it has 3 text inputs and 1 text area. All I am having it do is a simply email the results to someone. I want to validate the fields using php and then output any errors to the correct spot on the form...What I am having trouble deciding is when to do the error checking...I am running the site completely off my index.php (using page values like index.php?p=contact) I want to check all the fields for errors, and make the user aware of all the errors at once. I have seen examples of error checking functions and validation but I cant seem to figure out how to make them work the way I want... <?php if (!isset($_POST['sub'])) { // if the user hasn't submitted the form, display the page normally echo <<<_HTML <div id="contactUs"> <!-- beginning of the contact us page code --> <div class="Row"><!-- top row--> </div><!--top row--> <div id="leftCol"><!-- left column--> <div id="contactForm"> <!-- contact form --> <table width="400" border="0" cellpadding="0" cellspacing="0"> <form name="contact" class="myform" method="post" action="index.php?p=contact&a=submit"> <input type="hidden" name="sub" /> <!-- make sure form is posted --> <tr> <td width="150"> <label>Full Name <span class="small">first & last</span></label></td> <td width="250"><input name="name" type="text" id="name" tabindex="1" dir="ltr" lang="en" maxlength="65" /></td> </tr> <tr> <td><label>Email Address <span class="small">your@email.com</span></label></td> <td><input name="email" type="text" id="email" tabindex="2" dir="ltr" lang="en" maxlength="65" /></td> </tr> <tr> <td><label>Telephone <span class="small">xxx-xxx-xxxx</span></label></td> <td><input name="phone" type="text" id="phone" dir="ltr" lang="en" maxlength="12" /></td> </tr> <tr> <td valign="top"><label>Comments<span class="small">or questions?</span></label></td> <td><textarea name="comments" cols="30" rows="7" tabindex="4"></textarea></td> </tr> <tr><td> </td><td><button type="reset">Clear</button> <button type="submit">Submit</button></td> </tr> </form> </table> </div> <!--contact form --> </div><!-- left column --> <div id="rightCol"><!-- right column --> </div> <!-- right column --> <div class="Row"><!-- bottom row--> </div> <!--bottom row --> </div> <!--contact us page --> _HTML; } elseif (isset($post['sub'])) { $fname = $_POST['name']; $ftel = $_POST['phone']; /* set new variables with the information from the form */ $femail = $_POST['email']; /* in case one is invalid, all the good fields will be entered*/ $fcomment = $_POST['comments']; /* on the form so user doesn't have to retype them all */ /* THIS IS WHERE I WOULD VALIDATE AND OUTPUT */ thats as far as I got with the code...the top half is where I am checking to see if "sub" is set, which if it is not i want the form to display normally. Then under that, I am checking in case it is I set variables for my form fields so I can validate them. Now, how do I validate them and output individual errors for the fields which are incorrect? I have a form inside a table which is positioned by CSS on my page. So for example, if there was an error in the "Name" text field (validating using $fname), i would want it to output something like this in the area of the HTML where the name input is. <td width="150"> <label>Full Name <span class="small">first & last</span></label></td> <td width="250"><input name="name" type="text" id="name" tabindex="1" dir="ltr" lang="en" maxlength="65" /> <span class="smallError">* required</span></td> Am I explaining this right? I can see how i want it to go in my head, i just dont know if I am explaining it right... Quote Link to comment https://forums.phpfreaks.com/topic/148862-solved-contact-formshowing-the-errors-in-the-right-spot/ Share on other sites More sharing options...
WolfRage Posted March 11, 2009 Share Posted March 11, 2009 This should give you some ideas. http://www.phpfreaks.com/forums/index.php/topic,241273.msg1130388.html#msg1130388 Quote Link to comment https://forums.phpfreaks.com/topic/148862-solved-contact-formshowing-the-errors-in-the-right-spot/#findComment-781844 Share on other sites More sharing options...
tefuzz Posted March 11, 2009 Author Share Posted March 11, 2009 hmm...im gonna have to mess with this and see if i can get that to work. thanks for the link, not sure why i didnt check out that thread when i searched. Quote Link to comment https://forums.phpfreaks.com/topic/148862-solved-contact-formshowing-the-errors-in-the-right-spot/#findComment-782248 Share on other sites More sharing options...
tefuzz Posted March 11, 2009 Author Share Posted March 11, 2009 used a variation of the example...thanks! Quote Link to comment https://forums.phpfreaks.com/topic/148862-solved-contact-formshowing-the-errors-in-the-right-spot/#findComment-782343 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.