Jump to content

[SOLVED] contact form...showing the errors in the right spot...


tefuzz

Recommended Posts

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">[email protected]</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...

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.