Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP SELF : 2 FORMS ONE PAGE.

Featured Replies

Here is the code, this is my first time using PHP, I assume it has something to do with two PHP-SELF posts on one page, I've used this so I can post the errors on the same page heres the code of the whole page:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--aaronbrand design for HairyLemonhostel.com-->
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Hairy Lemon Hostel : Albania : Saranda</title>
<meta name="author" content="Aaron Evans" />

  <meta name="date" content="2009-01-01" />
<meta http-equiv="content-language" content="en" />
<meta name="keywords" content="hostel, hostels, albania, accomadation, travel, saranda, tiriana, butrint, hairy-lemon, hairy lemon, cheap hostel, backpacker hostel, youth hostel" />
<meta name="description" content="Hairy Lemon Hostels Traveler Accomadation.  Hairy Lemon Hostels are developed by travelers for travelers." />

<link rel="stylesheet" type="text/css" href="saranda.css" />
<link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />

<script type="text/javascript" src="js/prototype.js">
</script>
<script type="text/javascript" src="js/scriptaculous.js?load=effects,builder">
</script>
<script type="text/javascript" src="js/lightbox.js">
</script></head>
<body>

<div id="outer">

<div id="topbg"></div>

<div id="inner">
  <div id="header"></div>

	<!-- menu start -->
		<div id="orangecontainer">
			<ul id="navorange">
					   <li class="c1">Our hostels :</li>
						<li><a href="/saranda/">Saranda</a></li>
						<li><a href="/tirana/">Tirana</a></li>
						<li><a href="/borshi/">Borshi</a></li>
						<li class="c1">Interested:</li>
						<li><a href="faq.php">FAQ</a></li>
						<li><a href="jobs.php">Jobs</a></li>
						<li></li>
			</ul>
</div>

			<!--<li class="c1">Our hostels :</li>
				<li class="c1">Interested:</li>-->

	<!-- menu end -->

	<!-- content start -->

	<div id="content">	
<div class="picturebar"><a href="photos/contact2.jpg" rel="lightbox[contact]" title="Albanina from corfu by Ken Hircock 
<a href="http://ncane.com/7lc<a href=""><img src="photos/thumbs/contact2.jpg" alt="albainafromcorfu" /></a><a href="photos/contact2.jpg "></a></a><a href="photos/contact.jpg" rel="lightbox[contact]" title="Annette your wonderfull host and guests enjoying an evening at hairy lemon"><img src="photos/thumbs/contact.jpg" alt="hairy lemon party" /></a><a href="photos/contact3.jpg" rel="lightbox[contact]" title="Confused? why not get in touch"><img src="photos/thumbs/contact3.jpg" alt="hairy lemon party" /></a></div>
	<div>
	<div class="content" >
	<h1>Contact</h1>

	 <p>Hairy Lemon Hostels</p>
	 <p>13 street name</p>
	 <p>appartment 6, saranda</p>
	 <p>Albania</p>
	 <p>+355 69 355 9317</p>
	 <p><a href="mailto:saranda@hairylemonhostel.com">saranda@hairylemonhostel.com</a></strong></p>
	 <p>you can contact us the email above by using the form below:</p>




	 <?php

			function VerifyForm(&$value, &$error)
			{
				// Do all necessary form verification

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

		if (strlen($value['name']) == 0)
	$error['name'] = 'Name required<br />';	

		if (empty($value['message']))
	$error['message'] = 'Message required<br />';	

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



function DisplayForm($value, $error)
{
?>

<?php
if (count($error) > 0)
	echo "Form Errors";
		?>


		   <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" >
		   <div id="error">
			<?= $error['name'] ?><?= $error['emailadd'] ?><?= $error['message'] ?></div>

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

			<label for="emailadd">E-mail:</label>
			<input id="emailadd" name="emailadd" value="<?= htmlentities($value['emailadd']) ?>"/><br />

			<label for="message">Message:</label>
			<textarea name="message" rows="5" cols="20" value="<?= htmlentities($value['message']) ?>" ></textarea>
<br />

			<br />
			<label for="submit"></label>
		<input type="submit" value="submit" />
			<br />
		</form>
<?php
}

function ProcessForm($value)
{

	$subject="{$value['name']} has sent a message to the hostel";
	$message="A {$value['name']} has said:

	Message: {$value['message']}

	respond to {$value['emailadd']}";


		mail("aaronevans@inbox.com", $subject, $message, "From: \"{$value['emailadd']}\" <{$value['emailadd']}>");

			// Thank you link:
			echo "Thank You {$value['name']}, your email has been sent and we will get back to you as soon as possible";
}
			if ($_SERVER['REQUEST_METHOD'] == 'POST')
		{
		$formValue = $_POST;
		$formError = array();

		if (!VerifyForm($formValue, $formError))
			DisplayForm($formValue, $formError);
			else
			ProcessForm($formValue);
			}
		else
			DisplayForm(null, null);
		?>

	</div>			
	  </div>
	  <!-- content end -->
	</div>
	<div id="sidebar">


		<!-- sidebar start -->
  <div class="content">
	<h1>Booking Enquiry:</h1>

				  <div class="content">
 <?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['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['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="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">11pm</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 for {$values['date']} {$values['month']}, {$values['beds']} beds & {$values['nights']} nights";
	$message="Booking enquiry from: {$values['visitor']}

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

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


		mail("aaronevans@inbox.com", $subject, $message, "From: \"{$values['email']}\" <{$values['email']}>");

			// Thank you link:
			echo "Thank You {$values['visitor']}. Your booking enquiry has been submited - 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);
		?>
				  </div>

										  <div class="outer1">	
<div id="sidebarmenu">
	<ul>
				<li><a href="index.php">Home</a></li>
				<li><a href="reservastions.php">Reservations</a></li>
				<li><a href="directions.php">Map & Directions</a></li>
				<li><a href="facilities.php">Facilities</a></li>
				<li><a href="prices.php">Prices</a></li>
				<li><a href="contact.php">Contact</a></li>
				<li><a href="todo.php">Things to do: Saranda</a></li>
				<li><a href="guestbook.php">Guest book</a></li>
				<li><a href="photos.php">Photo gallery</a></li>
</ul>
</div>
</div>
	  </div>

		<!-- sidebar end -->
  </div>

<!-- footer start -->
	<div id=closer></div>
  <div id="footer">

		©  <a href="http://www.hairylemonhostel.com">hairylemonhostel.com</a> | <a href="credits.php">Credits</a> |</div>

  <!-- footer end -->
</div>
</div>

</body>
</html>

 

Any ideas?

Here is the code, this is my first time using PHP, I assume it has something to do with two PHP-SELF posts on one page, I've used this so I can post the errors on the same page heres the code of the whole page: ... Any ideas?

You haven't actually said what's wrong here, what it is that isn't working! You surely can't expect someone to read or parse all that XHTML and PHP in their head, or run the example, and tell you what's wrong and how to fix it? So please tell us what needs fixing!

 

Secondly, putting all that PHP and HTML together is a really bad idea... it's cluttered, not readable, and therefore hard to maintain. You might not even be able to determine (easily at least) where the error is with all that stuff in one long file. You'd be far better splitting out the HTML into a single HTML-only page, and putting the PHP separately as one or more processing scripts. Then have your forms submit to those processing scripts, redirecting back to the HTML if necessary. A better way altogether is to use a templating scheme (such as Smarty) so you can keep the HTML in templates, and display the HTML from your PHP along with populated data using simple one-line method calls. I'd suggest jiggling the script around to break out the HTML from the PHP before trying to debug the problem further.

The error is 'Cannot redeclare verifyform()'. This is because you've made two functions with the same name. Just rename one.

  • Author

I know i need to tidy it up a bit, I have them in the html because if the form has errors or a user hasn't filled in all the info i want them to post on the same page. is there a way to do this if using php from its own file? I put all the code in to demonstrate that there are two forms on one page, they both work fine on there own.

 

PEA i tried changing the function names but it still doesn't work, I need to get myself a php debugger.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.