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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.