Jump to content

FORM


user4fun

Recommended Posts

Okay, i took this code of the internet and i repeated itover and over for the fields that i need. Unfortunatly they are listed vertically and i need to change that, but every time i do. It stops working.I need to have it.Name: textbox Email: textbox Phone:textboxAddress: textboxCity: textbox State: textbox Zipcode: textbox.When a field is found missing, it would still get highlighted.If possible, at least get me a jump start and i will finish the rest.Thank you very much for any help you can give me. (here is my vertically listed code)

<?php $arrErrors = array();if (!empty($_POST['Submit'])) {      if ($_POST['name']=='')        $arrErrors['name'] = 'Please provide your name.';    if ($_POST['email']=='')        $arrErrors['email'] = 'A valid email address is required.';    if ($_POST['phone']=='')        $arrErrors['phone'] = 'Please provide your phone number.';    if ($_POST['Maddress']=='')        $arrErrors['Maddress'] = 'Please provide your address.';    if ($_POST['Mcity']=='')        $arrErrors['Mcity'] = 'Please provide your city.';    if ($_POST['Mstate']=='')        $arrErrors['Mstate'] = 'Please provide your state.';    if ($_POST['Mzip']=='')        $arrErrors['Mzip'] = 'Please provide your postal code.';    if (count($arrErrors) == 0) {       } else {       $strError = '<div class="formerror"><p><img src="/image/triangle_error.png" width="16" height="16" hspace="5" alt="">Please check the following and try again:</p><ul>';            foreach ($arrErrors as $error) {            $strError .= "<li>$error</li>";        }        $strError .= '</ul></div>';    }}?><style>label {  width: 80px;  text-align: right;  float: left;}.formerror {  border: 1px solid red;  background-color : #FFCCCC;  width: auto;  padding: 5px 0;}.errortext {  padding-left: 80px;  font: bold smaller sans-serif;}</style><div align="center">  <center><table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="80%">  <tr>    <td width="100%">   <?php echo $strError; ?><form method="post" action="<?php echo $PHP_SELF; ?>"><p><p<?php if (!empty($arrErrors['name'])) echo ' class="formerror"'; ?>>    <label for="name">Name:</label>    <input name="name" type="text" id="name" value="<?php echo $_POST['name'] ?>" size="20">    <?php if (!empty($arrErrors['name'])) echo '<img src="/image/triangle_error.png" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['name'].'</span>'; ?></p></p><p<?php if (!empty($arrErrors['email'])) echo ' class="formerror"'; ?>>    <label for="email">Email:</label>    <input name="email" type="text" id="email" value="<?php echo $_POST['email'] ?>" size="20">    <?php if (!empty($arrErrors['email'])) echo '<img src="/image/triangle_error.png" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['email'].'</span>'; ?></p><p<?php if (!empty($arrErrors['phone'])) echo ' class="formerror"'; ?>>    <label for="phone">Phone:</label>    <input name="phone" type="text" id="phone" value="<?php echo $_POST['phone'] ?>" size="20">    <?php if (!empty($arrErrors['phone'])) echo '<img src="/image/triangle_error.png" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['phone'].'</span>'; ?><p<?php if (!empty($arrErrors['Maddress'])) echo ' class="formerror"'; ?>>    <label for="Maddress">Address:</label>    <input name="Maddress" type="text" id="Maddress" value="<?php echo $_POST['Maddress'] ?>" size="20">    <?php if (!empty($arrErrors['Maddress'])) echo '<img src="/image/triangle_error.png" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['Maddress'].'</span>'; ?></p><p<?php if (!empty($arrErrors['Mcity'])) echo ' class="formerror"'; ?>>    <label for="Mcity">City:</label>    <input name="Mcity" type="text" id="Mcity" value="<?php echo $_POST['Mcity'] ?>" size="20">    <?php if (!empty($arrErrors['Mcity'])) echo '<img src="/image/triangle_error.png" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['Mcity'].'</span>'; ?></p><p<?php if (!empty($arrErrors['Mstate'])) echo ' class="formerror"'; ?>>    <label for="Mstate">State:</label>    <input name="Mstate" type="text" id="Mstate" value="<?php echo $_POST['Mstate'] ?>" size="20">    <?php if (!empty($arrErrors['Mstate'])) echo '<img src="/image/triangle_error.png" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['Mstate'].'</span>'; ?></p><p<?php if (!empty($arrErrors['Mzip'])) echo ' class="formerror"'; ?>>    <label for="Mzip">Zip Code:</label>    <input name="Mzip" type="text" id="Mzip" value="<?php echo $_POST['Mzip'] ?>" size="20">    <?php if (!empty($arrErrors['Mzip'])) echo '<img src="/image/triangle_error.png" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['Mzip'].'</span>'; ?></p></p><p align="center">    </p></td>  </tr></table>  </center></div>

Link to comment
Share on other sites

Complaints that I've "solved" in my copy:

  • It doesn't have a </form>.
  • For some odd reason, <p<?php if (!empty($arrErrors['phone'])) echo ' class="formerror"'; ?>> isn't ended until after all the other <p class="formerror">s.
  • <p<?php if (!empty($arrErrors['name'])) echo ' class="formerror"'; ?>> is (seemingly) unnecessarily nested in a plain <p>.
  • The <td> contains the <form>; if it is to comply with your model, the <form> will need to contain a <table> with several <tr>s and <td>s (or three CSS-positioned <div>s, but that could get complicated).
  • Each field is in a <p>, which prevents it from being in line with the others.
  • When the <?php ifs evaluate to false, extra tabs still show. (Yes, I'm very picky.)
  • There's no submit button.
  • The labels don't stay in line with the fields.
  • Your validation doesn't handle a completely empty form.
  • The errors contort the page.

Link to comment
Share on other sites

My only problem with it is the red lines over the empty cells.

<html>	<head>		<title>Titled Document</title>		<style>			label {				text-align: right;			}						.formfield, .formerror {				width: auto;				text-align: right;			}			.formerror {				border: 1px solid red;				background-color : #FFCCCC;			}						span {				white-space: nowrap;			}						.errorlist {				border: 1px solid red;				background-color : #FFCCCC;				width: auto;			}						.errortext {				padding: 0px;				font: bold smaller sans-serif;			}		</style>	</head>	<body><?php	define('EOL', "\n");	$errorSign = '/image/triangle_error.png';	$arrErrors = array();	if (!empty($_POST['Submit'])) {		if ($_POST['name']=='')			$arrErrors['name'] = 'Please provide your name.';		if ($_POST['email']=='')			$arrErrors['email'] = 'A valid email address is required.';		if ($_POST['phone']=='')			$arrErrors['phone'] = 'Please provide your phone number.';		if ($_POST['Maddress']=='')			$arrErrors['Maddress'] = 'Please provide your address.';		if ($_POST['Mcity']=='')			$arrErrors['Mcity'] = 'Please provide your city.';		if ($_POST['Mstate']=='')			$arrErrors['Mstate'] = 'Please provide your state.';		if ($_POST['Mzip']=='')			$arrErrors['Mzip'] = 'Please provide your postal code.';		if (count($arrErrors) != 0) {			$strError = '				<div class="errorlist"><p><img src="' . $errorSign . '" width="16" height="16" hspace="5" alt="">' .						'Please check the following and try again:</p><ul>';			foreach ($arrErrors as $error) {				$strError .= "<li>$error</li>";			}			$strError .= '</ul></div>';		}	}?>		<div align="center">			<center><?php echo $strError; ?>				<form method="post" action="<?php echo $PHP_SELF; ?>">					<table cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="80%">						<tr>							<td<?php if (!empty($arrErrors['name'])) echo ' class="formerror"'; else echo ' class="formfield"'; ?>>								<label for="name">Name:</label>								<input name="name" type="text" id="name" value="<?php echo $_POST['name'] ?>" size="20"><?php if (!empty($arrErrors['name'])) echo '								<img src="' . $errorSign . '" width="16" height="16" hspace="5" alt=""><span class="errortext">'.$arrErrors['name'].'</span>'.EOL; ?>							</td>							<td<?php if (!empty($arrErrors['email'])) echo ' class="formerror"'; else echo ' class="formfield"' ?>>								<label for="email">Email:</label>								<input name="email" type="text" id="email" value="<?php echo $_POST['email'] ?>" size="20"><?php if (!empty($arrErrors['email'])) echo '								<img src="' . $errorSign . '" width="16" height="16" hspace="5" alt=""><span class="errortext">'.$arrErrors['email'].'</span>'.EOL; ?>							</td>							<td<?php if (!empty($arrErrors['phone'])) echo ' class="formerror"'; else echo ' class="formfield"' ?>>								<label for="phone">Phone:</label>								<input name="phone" type="text" id="phone" value="<?php echo $_POST['phone'] ?>" size="20"><?php if (!empty($arrErrors['phone'])) echo '								<img src="' . $errorSign . '" width="16" height="16" hspace="5" alt=""><span class="errortext">'.$arrErrors['phone'].'</span>'.EOL; ?>							</td>						</tr>						<tr>							<td<?php if (!empty($arrErrors['Maddress'])) echo ' class="formerror"'; else echo ' class="formfield"' ?>>								<label for="Maddress">Address:</label>								<input name="Maddress" type="text" id="Maddress" value="<?php echo $_POST['Maddress'] ?>" size="20"><?php if (!empty($arrErrors['Maddress'])) echo '							<img src="' . $errorSign . '" width="16" height="16" hspace="5" alt=""><span class="errortext">'.$arrErrors['Maddress'].'</span>'.EOL; ?>							</td>						</tr>						<tr>							<td <?php if (!empty($arrErrors['Mcity'])) echo ' class="formerror"'; else echo ' class="formfield"' ?>>								<label for="Mcity">City:</label>								<input name="Mcity" type="text" id="Mcity" value="<?php echo $_POST['Mcity'] ?>" size="20"><?php if (!empty($arrErrors['Mcity'])) echo '								<img src="' . $errorSign . '" width="16" height="16" hspace="5" alt=""><span class="errortext">'.$arrErrors['Mcity'].'</span>'.EOL; ?>							</td>							<td<?php if (!empty($arrErrors['Mstate'])) echo ' class="formerror"'; else echo ' class="formfield"' ?>>								<label for="Mstate">State:</label>								<input name="Mstate" type="text" id="Mstate" value="<?php echo $_POST['Mstate'] ?>" size="20"><?php if (!empty($arrErrors['Mstate'])) echo '								<img src="' . $errorSign . '" width="16" height="16" hspace="5" alt=""><span class="errortext">'.$arrErrors['Mstate'].'</span>'.EOL; ?>							</td>							<td<?php if (!empty($arrErrors['Mzip'])) echo ' class="formerror"'; else echo ' class="formfield"' ?>>								<label for="Mzip">Zip Code:</label>								<input name="Mzip" type="text" id="Mzip" value="<?php echo $_POST['Mzip'] ?>" size="20"><?php if (!empty($arrErrors['Mzip'])) echo '								<img src="' . $errorSign . '" width="16" height="16" hspace="5" alt=""><span class="errortext">'.$arrErrors['Mzip'].'</span>'.EOL; ?>							</td>						</tr>						<tr>							<td colspan="3" align="center">								<input name="Submit" type="submit" value="Submit">							</td>						</tr>					</table>				</form>			</center>		</div>	</body></html>

Link to comment
Share on other sites

There was no submit button because the form is the top section of a long form. The first few fields are required and the rest are not.Thank you so much for both your input.Iwill try the code left at posting 2

The labels don't stay in line with the fields.Your validation doesn't handle a completely empty form.The errors contort the page.
I will have to work on that, any help would be appreciated.
Link to comment
Share on other sites

Both posts were mine; I edited the first until I was done, then posted the second to make sure you read it. :) (If you had read the first while I was editing it, and I had edited the code in, you wouldn't have been notified of the code being posted.)Note that I solved those complaints in the posted code (except the one about red lines).

Link to comment
Share on other sites

I was advised to change that section to include what would happen when all the fields are completed, my question is the thank you message will be reaplced with a code that would email me the information ( already done) I am not sure how to stop the rest of the code to happen.Should i put in a redirect page ( like a thank you apge)?

if (count($arrErrors) == 0) 				{				echo "thanksyou";				}				else 				{		$strError = '<div class="errorlist"><p><img src="' . $errorSign . '" width="16" height="16" hspace="5" alt="">' .'Please check the following and try again:</p><ul>';		foreach ($arrErrors as $error)					   {				$strError .= "<li>$error</li>";			}			$strError .= '</ul></div>';		}	}?>

Link to comment
Share on other sites

No, it won't resubmit form information, when the browser sends the request for the next page it doesn't send all the data in the other request. You can either use the session or the querystring to get the data to the other page.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...