Jump to content


Photo

Quick question


  • Please log in to reply
2 replies to this topic

#1 spires

spires
  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts

Posted 23 August 2006 - 09:13 PM

Hi,

Just wondering if there is a way of shortening my code,
I've been storing strings into an array that will be call up for form validation.
Only, when your from has over ten input fields, the code gets a bit long.

Thanks

$arrErrors = array();

if (!empty($_POST['submit'])) {
	if ($_POST['first_name'] == "")
		$arrErrors['first_name'] = 'Add First Name';
	if ($_POST['last_name'] == "")
		$arrErrors['last_name'] = 'Add Last Name';
	if ($_POST['username']== "")
		$arrErrors['username'] = "Add Username";
	if ($_POST['password']== "")
		$arrErrors['password'] = "Add Password";
	if ($_POST['company_name']== "")
		$arrErrors['company_name'] = "Add Company Name";
	if ($_POST['country']== "")
		$arrErrors['country'] = "Add Country";
	if ($_POST['location']== "")
		$arrErrors['location'] = "Add Location";
	if ($_POST['postcode']== "")
		$arrErrors['postcode'] = "Add Postode";
	if ($_POST['email']== "") {
		$arrErrors['email'] = 'Add Email';
	} else {
		if (email_validation($_POST['email'])==true) {
		$arrErrors['email'] = 'Email Incorrect Format';
		}
	}
	if ($_POST['url']== "")
		$arrErrors['url'] = "Add Url";


	
	if (count($arrErrors) == 0) {

		$first_name=addslashes($_POST['first_name']);
		$last_name=addslashes($_POST['last_name']);
		$username=addslashes($_POST['username']);
		$password=addslashes($_POST['password']);
		$company_name=addslashes($_POST['company_name']);
		$country=addslashes($_POST['country']);
		$location=addslashes($_POST['location']);
		$postcode=addslashes($_POST['postcode']);
		$email=addslashes($_POST['email']);
		$url=addslashes($_POST['url']);
		$date = date("Y-m-d > H:i:s");
		$type = $_POST['type'];
		$cat = $_POST['cat'];
		
	$query2 = "SELECT username FROM user_info WHERE username = '$username'";
	$result2 = mysql_query($query2) or die ("query 2 failed");
	$count = mysql_num_rows($result2);
	
	if ($count == 0) {
		$sql = mysql_query("INSERT INTO user_info (first_name, last_name, username, password, company_name, country, location, postcode, email, url, date, type, cat, misc)
							VALUES ('$first_name', '$last_name', '$username', '$password', '$company_name', '$country', '$location', '$postcode', '$email', '$url', '$date', '$type', '$cat', 'NULL')") or die ("could not insert");
		if ($sql) {
		$_POST = array();
			$ok = 'Your Details have been updated,<br>
			An email has been sent to you with your login details.';
		$link = '<a href="http://www.theeventorganiser.com/login.php">login</a>';
		$to      = 	$email;
		$subject = 	'Hi '.$first_name.', The Event Organiser Would like to thank you';
		$message = 	'Hi '.$first_name.'<br>
						Thank you for Signing up to he Event Organiser<br><br>
						Here is you username and password<br>
						USERNAME = '.$username.'<br>
						PASSWORD = '.$password.'<br><br>
						Please login here<br>'.$link.'';
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

		mail($to, $subject, $message, $headers);
		
		} else {
			$notOk = "Sorry, there was an Error";
		}
	} else {
	$userTaken = 'Sorry, The username has already been taken. <br> Please select another one.';
	}
	
	} else {
		if (empty($first_name) || empty($last_name) || empty($username) || empty($password) || empty($company_name) || empty($country) || empty($location) || empty($postcode) || empty($email) || empty($url)) {
		foreach ($arrErrors as $error) {
		
		$strError .= '<div class="error">';
			$strError .= "<li>$error</li>";
		}
		$strError .= '</div>';
	} 
}

} 

?>

How to make over $600 a day from AdSense? Discover EXACTLY how he does it in these videos:
http://www.adsense-online.com

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 23 August 2006 - 09:16 PM

If you are checking the data for validity, which you should, then there is no easy way to condense your code.

#3 spires

spires
  • Members
  • PipPipPip
  • Advanced Member
  • 492 posts

Posted 23 August 2006 - 09:27 PM

OK thanks.

Just thought id check.
How to make over $600 a day from AdSense? Discover EXACTLY how he does it in these videos:
http://www.adsense-online.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users