Location: PHPKode > projects > Apartment Classfieds > apartment_classifieds_script/apartment_insert.php
<?php
require_once('class_apartment.php');
require_once('class_category.php');
require_once('class_database.php');
require_once('class_page.php');
require_once('globals.php');

session_start();

$categories_html = null;
$categories = null;
$page = null;
$db = null;
$row_count = null;
$apartment = null;

$page = new Page();
$page->append_title('Submission Form');
$db = new Database();
if (!$db->connect())
{
	die('Fatal error: could not connect to database.');
}

if (isset($_POST['submit']))
{
	if (!isset($_POST['title']) ||
		!isset($_POST['category_id']) ||
		!isset($_POST['location']) ||
		!isset($_POST['bathrooms']) ||
		!isset($_POST['rent']) ||
		!isset($_POST['lease_term']) ||
		!isset($_POST['required_credit_check']) ||
		!isset($_POST['required_reference_check']) ||
		!isset($_POST['damage_deposit']) ||
		!isset($_POST['description']) ||
		!isset($_POST['allowed_pets']) ||
		!isset($_POST['email']) ||
		!isset($_POST['phone']) ||
		!isset($_POST['image_verify']) ||
		!isset($_SESSION['key']))
	{
		die('invalid input (1)');
	}

	$apartment = new Apartment();
	
	if (!$apartment->set_title($_POST['title']) ||
		!$apartment->set_category_id($_POST['category_id']) ||
		!$apartment->set_location($_POST['location']) ||
		!$apartment->set_bathrooms($_POST['bathrooms']) ||
		!$apartment->set_rent($_POST['rent']) ||
		!$apartment->set_lease_term($_POST['lease_term']) ||
		!$apartment->set_required_credit_check($_POST['required_credit_check']) ||
		!$apartment->set_required_reference_check($_POST['required_reference_check']) ||
		!$apartment->set_damage_deposit($_POST['damage_deposit']) ||
		!$apartment->set_description($_POST['description']) ||
		!$apartment->set_allowed_pets($_POST['allowed_pets']) ||
		!$apartment->set_email($_POST['email']) ||
		!$apartment->set_phone($_POST['phone']))
	{
		die('invalid input (2)');
	}
	
	// check verification image
	if ($_SESSION['key'] != $_POST['image_verify'])
	{
		die('Incorrect verification image, please press the back button on your browser and try again');
	}
	else
	{
		// make them visit the input page again if successful
		$_SESSION['key'] = null;
	}
	
	$apartment->save($db->get_handle());
	
	$page->append_content('Your apartment information was saved, <a href="apartment_search.php?category_id='.$apartment->get_category_id().'&amp;offset=0">click here</a> to view it.');
}
else
{
	$row_count = 0;

	$categories_html = '<select name="category_id">';
	Category::get_categories($categories, $db->get_handle());
	
	for ($i = 0; $i < count($categories); $i++)
	{
		$categories_html .= '<option value="'.$categories[$i]->get_id().'">'.$categories[$i]->get_title().'</option>';
	}
	$categories_html .= '</select>';

	$page->append_content(
	'<h3>Please fill in the form below, all fields are required</h3>
	<form action="apartment_insert.php" method="POST" onSubmit="return check_form()">
		<table width="100%" class="data_table" cellpadding="0" cellspacing="0">
		<tr'.(Page::row_shade($row_count)).'>
		<td class="field_name">Title of your ad</td>
		<td><input type="text" id="title" name="title" size="50" maxlength="'.MAX_APT_TITLE_LENGTH.'" >	<div id="title_error" class="error_description"></div></td>
		</tr>
		
		<tr'.(Page::row_shade($row_count)).'>
		<td class="field_name">Full address</td>
		<td><input type="text" id="location" name="location" size="50" maxlength="'.MAX_APT_LOCATION_LENGTH.'"> <div id="location_error" class="error_description"></div></td>
		</tr>		

		<tr'.(Page::row_shade($row_count)).'>
		<td class="field_name">Type of rental</td>
		<td>'.$categories_html.'</td>
		</tr>

		<tr'.(Page::row_shade($row_count)).'>
		<td class="field_name">Credit check required</td>
		<td>
		<select id="credit_check_required" name="required_credit_check">
			<option value="0">No</option>
			<option value="1">Yes</option>
		</select>
		</td>
		</tr>
		
		<tr'.(Page::row_shade($row_count)).'>
		<td class="field_name">Reference check required</td>
		<td>
		<select name="required_reference_check">
			<option value="0">No</option>
			<option value="1">Yes</option>
		</select>
		</td>
		</tr>
		
		<tr'.(Page::row_shade($row_count)).'>
		<td class="field_name">Pets allowed</td>
		<td>
		<select name="allowed_pets">
			<option value="0">No</option>
			<option value="1">Yes</option>
		</select>
		</td>
		</tr>
		
		<tr'.(Page::row_shade($row_count)).'>
		<td class="field_name">Number of bathrooms</td>
		<td>
			<select name="bathrooms">
			<option value="1">One</option>
			<option value="2">Two</option>
			<option value="3">Three</option>
			<option value="4">Four Plus</option>
			</select>
		</td>
		</tr>
		
		<tr'.(Page::row_shade($row_count)).'>
		<td class="field_name">Monthly rent amount</td>
		<td>$<input type="text" id="rent" name="rent" size="4" maxlength="4"> <div id="rent_error" class="error_description"></div></td>
		</tr>
		
		<tr'.(Page::row_shade($row_count)).'>
		<td class="field_name">Lease term required in months (0 if no lease)</td>
		<td><input type="text" id="lease_term" name="lease_term" size="2" maxlength="2"> <div id="lease_term_error" class="error_description"></div></td>
		</tr>
		
		<tr'.(Page::row_shade($row_count)).'>
		<td class="field_name">Damage deposit amount (0 if no damage deposit)</td>
		<td>$<input type="text" id="damage_deposit" name="damage_deposit" size="4" maxlength="4"> <div id="damage_deposit_error" class="error_description"></div></td>
		</tr>
		
		<tr'.(Page::row_shade($row_count)).'>
		<td class="field_name">Email address</td>
		<td><input type="text" id="email" name="email" size="50" maxlength="'.MAX_APT_EMAIL_LENGTH.'"> <div id="email_error" class="error_description"></div></td>
		</tr>

		<tr'.(Page::row_shade($row_count)).'>
		<td class="field_name">Phone number</td>
		<td><input type="text" id="phone" name="phone" size="20" maxlength="'.MAX_APT_PHONE_LENGTH.'"><div id="phone_error" class="error_description"></div></td>
		</tr>
		
		<tr'.(Page::row_shade($row_count)).'>
		<td class="field_name">Verification image</td>
		<td>
		<img src="image_verify.php" height="88" width="150" alt="Please enable images"><br>
		<input type="text" id="image_verify" name="image_verify" size="8" maxlength="8"><div id="image_verify_error" class="error_description"></div></td>
		</tr>
		
		</table>
		<div align="center">
		<p>Detailed Description (distance to stores, included appliances, etc)</p>
		<textarea id="description" name="description" maxlength="'.MAX_APT_DESCRIPTION_LENGTH.'" rows="10" cols="80"></textarea><div id="description_error" class="error_description"></div>
		
		<p><a href="terms_and_conditions.php" target="_blank">Terms and Conditions</a> (Opens in a new window)</p>
		
		<p><input type="submit" value="I have read, understood and agree to the terms and conditons. Submit" name="submit"></p>
		</div>
	</form>	
	
	<script type="text/javascript">
		function check_form()
		{	
			var error = false;
			document.getElementById("title_error").innerHTML = "";
			document.getElementById("location_error").innerHTML = "";
			document.getElementById("rent_error").innerHTML = "";
			document.getElementById("lease_term_error").innerHTML = "";
			document.getElementById("damage_deposit_error").innerHTML = "";
			document.getElementById("email_error").innerHTML = "";
			document.getElementById("phone_error").innerHTML = "";
			document.getElementById("description_error").innerHTML = "";
			document.getElementById("image_verify_error").innerHTML = "";
			
			if (document.getElementById("title").value == "")
			{
				error = true;
				document.getElementById("title_error").innerHTML = "Please enter a title for your ad";
			}
			
			if (document.getElementById("location").value == "")
			{
				error = true;
				document.getElementById("location_error").innerHTML = "Please enter an address for your rental property";
			}
			
			if (isNaN(document.getElementById("rent").value) || document.getElementById("rent").value == "")
			{
				error = true;
				document.getElementById("rent_error").innerHTML = "Please enter a rent amount in dollars";
			}
						
			if (isNaN(document.getElementById("lease_term").value) || document.getElementById("lease_term").value == "")
			{
				error = true;
				document.getElementById("lease_term_error").innerHTML = "Please enter the required lease term in months";
			}
			
			if (isNaN(document.getElementById("damage_deposit").value) || document.getElementById("damage_deposit").value == "")
			{
				error = true;
				document.getElementById("damage_deposit_error").innerHTML = "Please enter the damage deposit amount";
			}
			
			if (document.getElementById("email").value == "")
			{
				error = true;
				document.getElementById("email_error").innerHTML = "Please enter an email address";
			}
			
			if (document.getElementById("phone").value == "")
			{
				error = true;
				document.getElementById("phone_error").innerHTML += "Please enter a phone number";
			}	
			
			if (document.getElementById("description").value == "")
			{
				error = true;
				document.getElementById("description_error").innerHTML += "Please enter a description";
			}	
			
			if (isNaN(document.getElementById("image_verify").value) || document.getElementById("image_verify").value == "")
			{
				error = true;
				document.getElementById("image_verify_error").innerHTML += "Please the numbers in the verification image";
			}
			
			if (error)
			{
				alert("There were errors with your submission, please see the fields in red for more information.");
				return false;
			}
			
			return true;
		}
	</script>');
}

$page->display();
?>
Return current item: Apartment Classfieds