Jump to content

Quick question


spires

Recommended Posts

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

[code]
$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>';
}
}

}

?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/18477-quick-question/
Share on other sites

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.