Jump to content

PHP Booking Enquiry Form, Better way of accoplishing this?


Recommended Posts

This form was the first piece of PHP I had played with and as you can see it's Messy.

 

This form appears on every page of a hostel website which I am developing an example of the use can be found here: :Hairy Lemon.

 

What I have done is simply copy and paste the form, and use php_self. I'd rather link to a file but how do I do this and still post the errors on the same page and above the form?

 

I also have another php_self form which is a simple contact form which has its issues with the booking form.

 

I'm could use: <?php include("*.php"); ?> but this is just a better way to copy the form around and not a real answer.

 

 

However I'd like the Website to Validate to W3C.

 

What have I done here that is a definite no when using PHP?

How can I link to the form and still identify the correct errors?

 

I do appreciate all responses, and you will have my thanks :P

 

<?php

			function VerifyForm(&$values, &$errors)
			{
    				// Do all necessary form verification

	if (!ereg('.*@.*\..{2,4}', $values['email']))
       			$errors['email'] = 'Email address invalid<br />';

	if (strlen($values['visitor']) == 0)
        	$errors['visitor'] = 'Name required<br />';	

	if (strlen($values['phone']) == 0)
		$errors['phone'] = 'Mobile Number invalid<br />';

				if (strlen($values['month']) == 0)
        $errors['month'] = 'Month required<br />';


				if (strlen($values['date']) == 0)
        $errors['date'] = 'Date required<br />';

				if (strlen($values['arrivial']) == 0)
        $errors['arrivial'] = 'Arrivial Time required<br />';

			if (strlen($values['nights']) == 0)
        $errors['nights'] = 'Nights required<br />';


				if (strlen($values['beds']) == 0)
        $errors['beds'] = 'Beds required<br />';

	    return (count($errors) == 0);
					}

function DisplayForm($values, $errors)
{
    ?>
  
  
  <?php
    if (count($errors) > 0)
        echo "Form Errors";
    		?>
  
  
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" >
               <div id="error">
			<?= $errors['visitor'] ?><?= $errors['email'] ?><?= $errors['phone'] ?><?= $errors['month'] ?>
               <?= $errors['date'] ?><?= $errors['arrivial'] ?><?= $errors['nights'] ?><?= $errors['beds'] ?></div>

			<label for="visitor">Name</label>
			<input id="visitor" name="visitor" value="<?= htmlentities($values['visitor']) ?>"/><br />

			<label for="e-mail">E-mail</label>
                <input id="email" name="email" value="<?= htmlentities($values['email']) ?>"/>
                <br  />
    
    <label for="phone">Mobile</label>
    <input id="phone" name="phone" value="<?= htmlentities($values['phone']) ?>"/>
    <br  />
    
<label for="month">Arrival</label>
			<select name="month" size="0" value="1<?= htmlentities($values['month']) ?>">
  					<option value=""></option>
  					<option value="January">Jan </option>  				
                	<option value="Febuary">Feb </option>
  					<option value="March">Mar </option>
  					<option value="April">Apr </option>
  					<option value="May">May </option>
				<option value="June">Jun </option>
					<option value="July">Jul </option>
  					<option value="August">Aug </option>
  					<option value="September">Sep </option>
  					<option value="October">Oct </option>
  					<option value="November">Nov </option>
				<option value="December">Dec </option>
			</select>
                               	
                <select name="date" size="0" value="<?= htmlentities($values['date']) ?>">
  					<option value=""></option>
  					<option value="1st">1</option>
				    <option value="2nd">2</option>
  					<option value="3rd">3</option>
  					<option value="4th">4</option>
					<option value="5th">5</option>
  					<option value="6th">6</option>
  					<option value="7th">7</option>
  					<option value="8th">8</option>
  					<option value="9th">9</option>
  					<option value="10th">10</option>
  					<option value="11th">11</option>
  					<option value="12th">12</option>
  					<option value="13th">13</option>
  					<option value="14th">14</option>
  					<option value="15th">15</option>
					<option value="16th">16</option>
  					<option value="17th">17</option>
  					<option value="18th">18</option>
  					<option value="19th">19</option>
  					<option value="20th">20</option>
  					<option value="21st">21</option>
  					<option value="22nd">22</option>
  					<option value="23rd">23</option>
  					<option value="24th">24</option>
  					<option value="25th">25</option>
  					<option value="26th">26</option>
  					<option value="27th">27</option>
  					<option value="28th">28</option>
  					<option value="29th">29</option>
  					<option value="30th">30</option>
  					<option value="31st">31</option>
			</select>
                <br />
                
                <label for="arrivial" > Time</label>
                <select name="arrivial" size="0" value="<?= htmlentities($values['arrivial']) ?>">
  					<option value=""></option>
  					<option value="01:00">1am</option>
				    <option value="02:00">2am</option>
  					<option value="03:00">3am</option>
  					<option value="04:00">4am</option>
					<option value="05:00">5am</option>
  					<option value="06:00">6am</option>
  					<option value="07:00">7am</option>
  					<option value="08:00">8am</option>
  					<option value="09:00">9am</option>
  					<option value="10:00">10am</option>
  					<option value="11:00">11am</option>
  					<option value="12:00">12pm</option>
  					<option value="13:00">1pm</option>
  					<option value="14:00">2pm</option>
  					<option value="15:00">3pm</option>
					<option value="16:00">4pm</option>
  					<option value="17:00">5pm</option>
  					<option value="18:00">6pm</option>
  					<option value="19:00">7pm</option>
  					<option value="20:00">8pm</option>
  					<option value="21:00">9pm</option>
  					<option value="22:00">10pm</option>
  					<option value="23:00">11pm</option>
  					<option value="00:00">12am</option>
                </select>
                <br />
                
			<label for="nights" >Nights</label>
			<select name="nights" size="0" value="<?= htmlentities($values['nights']) ?>">
				<option value=""></option>
				<option value=" 1 ">1 </option>
				<option value=" 2 ">2 </option>
				<option value=" 3">3 </option>
				<option value=" 4">4 </option>
				<option value=" 5 ">5 </option>
				<option value=" 6 ">6 </option>
				<option value=" 7">7 </option>
			</select>
                

                Beds
              <select name="beds" size="0" value="<?= htmlentities($values['beds']) ?>">
				<option value=""></option>
				<option value=" 1 ">1 </option>
				<option value=" 2 ">2 </option>
				<option value=" 3">3 </option>
				<option value=" 4">4 </option>
				<option value=" 5 ">5 </option>
				<option value=" 6 ">6 </option>
			</select>
                
                
			<br />
                <label for="submit"></label>
		<input type="submit" value="submit" />
			<br />

		</form>
            
            <?php
}



function ProcessForm($values)
{

	$subject="Booking Enquiry in Saranda for {$values['date']} {$values['month']}, {$values['beds']} beds & {$values['nights']} nights";
	$message="Booking enquiry from: {$values['visitor']}

	Booking Enquiry in Saranda

		Arrival Date: 	  {$values['date']} {$values['month']}
		Time: 			  {$values['arrivial']}
		Number of nights: {$values['nights']}
		Number of beds:	  {$values['beds']}

	respond to {$values['email']}
	respond to {$values['phone']}";


		mail("[email protected]", $subject, $message, "From: \"{$values['email']}\" <{$values['email']}>");
    
    			// Thank you link:
    			echo "Thank You {$values['visitor']}. Your booking enquiry has been submitted - we will get back to you as soon as possible ";
}
			if ($_SERVER['REQUEST_METHOD'] == 'POST')
		{
    		$formValues = $_POST;
    		$formErrors = array();
    
    		if (!VerifyForm($formValues, $formErrors))
        		DisplayForm($formValues, $formErrors);
    			else
        		ProcessForm($formValues);
			}
		else
    			DisplayForm(null, null);
		?>

There are two ways you can have the form processing script in a separate file. 

 

The first way is to write the form processing script as if the form is in the same file as it, and just include the form processing script in any form page script.  You could then continue to use php_self as the form action on your form.  If you choose this method, you don't have to worry about data persistence (sending error messages and form vars back to your form).

 

The second way is to have the form action point to the form processing script.  If the form is not validated (blank fields, whatever), the processing script would redirect back to the form.  Your processing script would know which form page to redirect back to with $_SERVER['HTTP_REFERER'] or by passing the page name to it via a hidden form field or a session variable.  The processing script would pass the error messages and form vars back to the form page via session vars.

 

The second way involves more coding.  One way is not necessarily better than the other; just depends on your setup, overall. 

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.