Page 1 of 1

Form check & Email Validation

Posted: Thu Jul 03, 2003 12:18 pm
by visionary
Hi all the PHP heads,

I know its basic but this is doing my head in a bit.

Any help please!
****************************************************

___________
HTML STUFF
___________

Code: Select all

<?php
   if ($submit){
	if (!$email || BLAH || !$eCheck) 					    
	{
	$error = "Required Fields Missing (*)!";
	$error1 = "(*)";
	}
	else {
	echo "<fieldset><legend>Application Recieved</legend><div align="center"><span class="thanx"><br />Thank you for 

your application.<br />We will contact you shortly.<br /><br /></span></div></fieldset><br />";
	}
  }
	if (!$submit || $error) {
		echo "<div align="center"><span class="red">$error</span></div>";			
?>
____
Form stuff
____
//////////////////////////////////////////////////////////////////////
// Problem lies below somewhere I guess!
// On submit if email is varified
// It doesn't assign the $eCheck variable, until next submit
// Hence you have to enter submit twice
// I am stumped! And been at it for far too long- Any ideas!
// All I wanted to do was validate the form, check the email and submit the page
//////////////////////////////////////////////////////////////////////

Code: Select all

<?php 
		 if ($email!="" && (!eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $email))) { echo 

"Invalid E-Mail address!"; 
		 } 
		 if ($email!="" && (eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $email))) { 

$eCheck = "correct"; 
		 	} 
		 ?>
</span>
<br /><br /></span>
</div>
</fieldset>
<br /><br />
<input name="eCheck" type="hidden" id="eCheck" value="

Code: Select all

<?php echo $eCheck ?>
" />
___________________
REST OF FORM STUFF
___________________

REST IF THE HTML
___________________

Any help greatly appreciated, cheers

V :?:

Posted: Thu Jul 03, 2003 12:35 pm
by m3rajk
ok. i get the feeling i'm misisng something that would be obious if i saw the whole thing... because from what i see ther's two issues: 1: you're outputting html before you know which html to output
2: your not checking if something is set correctly

there could be more.
i'm actually in a similar boat to you, only my issue is that html isn't passing a variable to php.
i don't knwo if it'd help you or not, but you can look at what i have by checking this thread, viewtopic.php?t=10271
do you have any messengers? i might be able to be of more help in a real-time chat

full code!

Posted: Thu Jul 03, 2003 1:19 pm
by visionary
Here we go then, jumping in and out to make the page do as I want and show aht is necessary when it is so and then to thank once done.


HTML PAGE DESIGN just nav and image stuff
______________________________________

Code: Select all

<?php
   if ($submit)&#123;
	if (!$borrow || !$over || !$property_value || !$outstanding_mortgage || !$applicant1_name || !$address || !$town_city || !$postcode || !$home_tel || !$work_tel || !$mob_tel || !$time_call || !$call_my || !$email || !$app1_am || !$app1_earn || !$eCheck) 					    
	&#123;
	$error = "Required Fields Missing (*)!";
	$error1 = "(*)";
	&#125;
	else &#123;
	echo "<fieldset><legend>Application Recieved</legend><div align="center"><span class="thanx"><br />Thank you for your application.<br />We will contact you shortly.<br /><br /></span></div></fieldset><br />";

// Mail() will go here!

	&#125;
  &#125;
	if (!$submit || $error) &#123;
		echo "<div align="center"><span class="red">$error</span></div>";			
?>
<div style="width: 400px; background-color: #fff; padding: 5px; margin: 0px auto";>
<form action="

Code: Select all

<?php echo $PHP_SELF ?>
" method="GET" name="loan_application" id="loan_application">
<fieldset>
<legend>Loan Details</legend>
<div class="row">
<span class="label">I wish to borrow:</span>
<span class="formw">
<input name="borrow" type="text" id="borrow" value="

Code: Select all

<?php echo $borrow ?>
" /><span class="red">

Code: Select all

<?php if (!$borrow) &#123; echo $error1; &#125; ?
></span>
</span> </div>
<div class="row">
<span class="label">Over:</span>
<span class="formw">

Code: Select all

<?php if (!$over) &#123; $over="Please Select";&#125; ?
>
<select name="over" class="formw" id="over">
<option value="

Code: Select all

<?php echo $over ?>
" SELECTED>

Code: Select all

<?php echo $over ?>
</option>
<option value="3 Years">3 Years</option>
<option value="4 Years">4 Years</option>
<option value="5 Years">5 Years</option>
<option value="6 Years">6 Years</option>
<option value="7 Years">7 Years</option>
<option value="8 Years">8 Years</option>
<option value="9 Years">9 Years</option>
<option value="10 Years">10 Years</option>
<option value="11 Years">11 Years</option>
<option value="12 Years">12 Years</option>
<option value="13 Years">13 Years</option>
<option value="14 Years">14 Years</option>
<option value="15 Years">15 Years</option>
<option value="16 Years">16 Years</option>
<option value="17 Years">17 Years</option>
<option value="18 Years">18 Years</option>
<option value="19 Years">19 Years</option>
<option value="20 Years">20 Years</option>
<option value="21 Years">21 Years</option>
<option value="22 Years">22 Years</option>
<option value="23 Years">23 Years</option>
<option value="24 Years">24 Years</option>
<option value="25 Years">25 Years</option>
</select><span class="red">

Code: Select all

<?php if ($over=="Please Select") &#123; echo $error1; &#125; ?>
</span>
</span> </div>
<div class="row">
<span class="label">Approx. value of my property:</span>
<span class="formw">
<input name="property_value" type="text" id="property_value" value="

Code: Select all

<?php echo $property_value ?>
" /><span class="red">

Code: Select all

<?php if (!$property_value) &#123; echo $error1; &#125; ?>
</span>
</span> </div>
<div class="row">
<span class="label">Approx. Mortgage currently outstanding:</span>
<span class="formw">
<input name="outstanding_mortgage" type="text" id="outstanding_mortgage" value="

Code: Select all

<?php echo $outstanding_mortgage ?>
" /><span class="red">

Code: Select all

<?php if (!$outstanding_mortgage) &#123; echo $error1; &#125; ?>
</span>
<br />
<br /></span>
</div>
</fieldset>
<br /><br />
<fieldset>
<legend>Applicant Details</legend>
<div class="row">
<span class="label">Applicant 1 (Full Name):</span>
<span class="formw">
<input name="applicant1_name" type="text" id="applicant1_name" value="

Code: Select all

<?php echo $applicant1_name ?>
" /><span class="red">

Code: Select all

<?php if (!$applicant1_name) &#123; echo $error1; &#125; ?>
</span>
</span> </div>
<div class="row">
<span class="label">Applicant 2 (Full Name):</span>
<span class="formw">
<input name="applicant2_name" type="text" id="applicant2_name" value="

Code: Select all

<?php echo $applicant2_name ?>
" />
<br /><br /></span>
</div>
<div class="row">
<span class="label">Address:</span>
<span class="formw">
<textarea name="address" class="formw" id="address">

Code: Select all

<?php echo $address ?>
</textarea>
<span class="red">

Code: Select all

<?php if (!$address) &#123; echo $error1; &#125; ?>
</span>
</span> </div>
<div class="row">
<span class="label">Town / City:</span>
<span class="formw">
<input name="town_city" type="text" id="town_city" value="

Code: Select all

<?php echo $town_city ?>
" /><span class="red">

Code: Select all

<?php if (!$town_city) &#123; echo $error1; &#125; ?>
</span>
</span>
</div>
<div class="row">
<span class="label">Postcode:</span>
<span class="formw">
<input name="postcode" type="text" id="postcode" value="<?php echo $postcode ?>" /><span class="red"><?php if (!$postcode) { echo $error1; } ?></span>
</span>
</div>
<div class="row"> <span class="label">Home Telephone:</span> <span class="formw">
<input name="home_tel" type="text" id="home_tel" value="

Code: Select all

<?php echo $home_tel ?>
" /><span class="red"><?php if (!$home_tel) { echo $error1; } ?></span>
</span>
</div>
<div class="row"> <span class="label">Work Telephone:</span> <span class="formw">
<input name="work_tel" type="text" id="work_tel" value="

Code: Select all

<?php echo $work_tel ?>
"/><span class="red">

Code: Select all

<?php if (!$work_tel) &#123; echo $error1; &#125; ?>
</span>
</span>
</div>
<div class="row">
<span class="label">Mobile Telephone:</span>
<span class="formw">
<input name="mob_tel" type="text" id="mob_tel" value="

Code: Select all

<?php echo $mob_tel ?>
" /><span class="red">

Code: Select all

<?php if (!$mob_tel) &#123; echo $error1; &#125; ?>
</span>
</span>
</div>
<div class="row">
<span class="label">Best Time to Call:</span>
<span class="formw">

Code: Select all

<?php if (!$time_call) &#123; $time_call="Please Select";&#125; ?>
<select name="time_call" class="formw" id="time_call">
<option value="

Code: Select all

<?php echo $time_call ?>
" SELECTED>

Code: Select all

<?php echo $time_call ?>
</option>
<option value="Morning">Morning</option>
<option value="Afternoon">Afternoon</option>
<option value="Early Evening">Early Evening</option>
</select><span class="red">

Code: Select all

<?php if ($time_call=="Please Select") &#123; echo $error1; &#125; ?>
</span>
</span>
</div>
<div class="row">
<span class="label">On my:</span>
<span class="formw">

Code: Select all

<?php if (!$call_my) &#123; $call_my="Please Select";&#125; ?>
<select name="call_my" class="formw" id="call_my">
<option value="<?php echo $call_my ?>" SELECTED>

Code: Select all

<?php echo $call_my ?>
</option>
<option value="Home Telephone">Home Telephone</option>
<option value="Work Telephone">Work Telephone</option>
<option value="Mobile Telephone">Mobile Telephone</option>
</select><span class="red">

Code: Select all

<?php if ($call_my=="Please Select") &#123; echo $error1; &#125; ?>
</span>
</span>
</div>
<div class="row">
<span class="label">Email Address:</span>
<span class="formw">
<input name="email" type="text" id="email" value="

Code: Select all

<?php echo $email ?>
" /><span class="red">

Code: Select all

<?php if (!$email) &#123; echo $error1; &#125; ?>
</span></span>
<span class="label">&nbsp;</span><span class="formw">
<span class="red">

Code: Select all

<?php 
		 if ($email!="" && (!eregi("^&#1111;a-z0-9]+(&#1111;-_\.]?&#1111;a-z0-9])+@&#1111;a-z0-9]+(&#1111;-_\.]?&#1111;a-z0-9])+\.&#1111;a-z]&#123;2,4&#125;", $email))) &#123; echo "Invalid E-Mail address!"; 
		 &#125; 
		 if ($email!="" && (eregi("^&#1111;a-z0-9]+(&#1111;-_\.]?&#1111;a-z0-9])+@&#1111;a-z0-9]+(&#1111;-_\.]?&#1111;a-z0-9])+\.&#1111;a-z]&#123;2,4&#125;", $email))) &#123; $eCheck = "correct"; 
		 	&#125; 
		 ?>
</span>
<br /><br /></span>
</div>
</fieldset>
<br /><br />
<input name="eCheck" type="hidden" id="eCheck" value="

Code: Select all

<?php echo $eCheck ?>
" />

// Testing

Code: Select all

<?php

echo "<pre>";
print_r($_GET);
echo "</pre>\n";

?>


<fieldset>
<legend>First Applicant's Employment Details</legend>
<div class="row">
<span class="label">I am:</span>
<span class="formw">

Code: Select all

<?php if (!$app1_am) &#123; $app1_am="Please Select";&#125; ?>
<select name="app1_am" class="formw" id="app1_am">
<option value="

Code: Select all

<?php echo $app1_am ?>
" SELECTED>

Code: Select all

<?php echo $app1_am ?>
</option>
<option value="full time">Employed full time</option>
<option value="part time">Employed part time</option>
<option value="self">Self Employed</option>
<option value="retired">Retired</option>
<option value="unemployed">Unemployed</option>
<option value="pensioner">A pensioner</option>
</select><span class="red">

Code: Select all

<?php if ($app1_am=="Please Select") &#123; echo $error1; &#125; ?>
</span>
</span>
</div>
<div class="row">
<span class="label">I earn:</span>
<span class="formw">
<input name="app1_earn" type="text" id="app1_earn" value="

Code: Select all

<?php echo $app1_earn ?>
" /><span class="red">

Code: Select all

<?php if (!$app1_earn) &#123; echo $error1; &#125; ?>
</span>
<br /><br /></span>
</div>
</fieldset>
<br /><br />
<fieldset>
<legend>Second Applicant's Employment Details</legend>
<div class="row">
<span class="label">I am:</span>
<span class="formw">

Code: Select all

<?php if (!$app2_am) &#123; $app2_am="Please Select";&#125; ?>
<select name="app2_am" class="formw" id="app2_am">
<option value="

Code: Select all

<?php echo $app2_am ?>
" SELECTED>

Code: Select all

<?php echo $app2_am ?>
</option>
<option value="full time">Employed full time</option>
<option value="part time">Employed part time</option>
<option value="self">Self Employed</option>
<option value="retired">Retired</option>
<option value="unemployed">Unemployed</option>
<option value="pensioner">A pensioner</option>
</select>
</span>
</div>
<div class="row">
<span class="label">I earn:</span>
<span class="formw">
<input name="app2_earn" type="text" id="app2_earn" value="

Code: Select all

<?php echo $app2_earn ?>
" />
<br /><br /></span>
</div>
</fieldset>
<br /><br />
<fieldset>
<legend>Submit Application</legend>
<div class="row">
<span class="formw">
<input type="submit" name="submit" value="submit" />
<input name="reset" type="reset" id="reset" value="reset" />
<br /><br />
</span>
</div>
</fieldset>
<div class="spacer">
&nbsp;
</div>
</form>
</div>

Code: Select all

<?php
&#125;
?>
______________
HTML TILL CLOSE


Hope this can make it clearer, thanks for the replies so far and hope one of you guys can help me with this conundrum (from my point of view anyways)

Cheers

V :?

Posted: Thu Jul 03, 2003 2:36 pm
by m3rajk
try starting the file like this...

Code: Select all

<?php
extract($_post);

$errors=array(); $err;

if(isset$submit){
  /* check for validity , if something's not valid, add to the error array, and set $err to true */

  if(!($err)){
   /* render post submission page */
  }else{
  /* render errors page */
  }
}else{ 
  /*render first visit page */
}
?>
another thing you cando is break the rendering into sections...

Code: Select all

<?php
/* stuff from above only rendering is done by calling functions */

function pagehead(){
?>
<!-- html for the begining of the page -->
<?php
}

function error_messages($errors){
?>
<!-- html for errors with a <?php for($i=0;$i<count$errors;$i++){ echo $error[$i]; } ?> -->
<?php
}
function page_1st_visit($db stuff passed){
?>
<!-- html for page -->
<?php
}

function thank_you($anything you need to pass)(
?>
<!-- html for page -->
<?php
}

function end_page(){
?>
<!-- html for page -->
<?php
}
?>

got it!!

Posted: Fri Jul 04, 2003 6:58 am
by visionary
Solution was the conditionals, what looking for and where!

Code below to see.

Cheers all for considerations and sugguestions.

<?php
// Check if form Submitted
if (isset($_POST['submit'])){
// Check if form field are completed
if (!$borrow || !$over || !$property_value || !$outstanding_mortgage || !$applicant1_name || !$address || !$town_city || !$postcode || !$home_tel || !$work_tel || !$mob_tel || !$time_call || !$call_my || !$email || !$app1_am || !$app1_earn)
{
$error = "Required Fields Missing (*)!";
$error1 = "(*)";
}
// Check if email exists and Varify its syntax
if (isset($_POST['email']) && (eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $_POST['email'])) && !$error && !$error2)
{
echo "<fieldset><legend>Application Recieved</legend><div align=\"center\"><span class=\"thanx\"><br />Thank you for your application.<br />We will contact you shortly.<br /><br /></span></div></fieldset><br />";
}
// There is an Email AND not valid
elseif (($_POST['email']) && !(eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $_POST['email'])))
{
$error2 = "Invalid E-Mail address!";
}
}
// Check not submited or ruturned error MSG
if (!$submit || $error)
{
echo "<div align=\"center\"><span class=\"red\">$error</span></div>";
?>

FORM HERE

<?php
}
?>

Posted: Fri Jul 04, 2003 2:07 pm
by m3rajk
one last thing i noticed... your select boxed have a range that you want displayed, it's much easier to adjust that range if you use php. it's also much smaller to code and looks nicer. instead of

Code: Select all

&lt;select name="over" class="formw" id="over"&gt; 
&lt;option value=" Code: 
&lt;?php echo $over ?&gt; 
" SELECTED&gt; Code: 
&lt;?php echo $over ?&gt; 
&lt;/option&gt; 
&lt;option value="3 Years"&gt;3 Years&lt;/option&gt; 
&lt;option value="4 Years"&gt;4 Years&lt;/option&gt; 
&lt;option value="5 Years"&gt;5 Years&lt;/option&gt; 
&lt;option value="6 Years"&gt;6 Years&lt;/option&gt; 
&lt;option value="7 Years"&gt;7 Years&lt;/option&gt; 
&lt;option value="8 Years"&gt;8 Years&lt;/option&gt; 
&lt;option value="9 Years"&gt;9 Years&lt;/option&gt; 
&lt;option value="10 Years"&gt;10 Years&lt;/option&gt; 
&lt;option value="11 Years"&gt;11 Years&lt;/option&gt; 
&lt;option value="12 Years"&gt;12 Years&lt;/option&gt; 
&lt;option value="13 Years"&gt;13 Years&lt;/option&gt; 
&lt;option value="14 Years"&gt;14 Years&lt;/option&gt; 
&lt;option value="15 Years"&gt;15 Years&lt;/option&gt; 
&lt;option value="16 Years"&gt;16 Years&lt;/option&gt; 
&lt;option value="17 Years"&gt;17 Years&lt;/option&gt; 
&lt;option value="18 Years"&gt;18 Years&lt;/option&gt; 
&lt;option value="19 Years"&gt;19 Years&lt;/option&gt; 
&lt;option value="20 Years"&gt;20 Years&lt;/option&gt; 
&lt;option value="21 Years"&gt;21 Years&lt;/option&gt; 
&lt;option value="22 Years"&gt;22 Years&lt;/option&gt; 
&lt;option value="23 Years"&gt;23 Years&lt;/option&gt; 
&lt;option value="24 Years"&gt;24 Years&lt;/option&gt; 
&lt;option value="25 Years"&gt;25 Years&lt;/option&gt; 
&lt;/select&gt;
try

Code: Select all

?><select name="over" class="formw" id="over">
<option value="Code: <?php echo $over ?>" SELECTED> Code: <?php echo $over ?> </option> 
<?php 
for($i=3;$i<26;$i++){
  echo "<option value="$i Years">$i Years</option>"; 
} ?>
</select>
it took me 4 lines to do what you did in 23. i can also make it change simply by changing the start value of $i and end value of $i.
your code will take much more editing to adjust.

when you're dealing with number ranges, use php. an example can be seen in a form i made....
in php (note this is part of a larger function that prints out the form and uses a table for alignment)

Code: Select all

<tr><td><a name="#dob" href="#dob" onClick="window.open('faq.php?seek=dob', 'faq', 'width=500,height=250,scrollbars=yes');">Date Of Birth</a></td><td>
		<select name="month" size="1"><option value="1">January</option><option value="2">February</option><option value="3">March</option><option value="4">April</option><option value="5">May</option><option value="6">June</option><option value="7">July</option><option value="8">August</option><option value="9">September</option><option value="10">October</option><option value="11">November</option><option value="12">December</option></select><select name="day" size="1"> <?php for($i=1;$i<32;$i++){ echo "<option value="$i">$i</option>"; } ?></select><select name="year" size="1"><?php for($i=1900;$i<1989;$i++){ echo "<option value="$i">$i</option>"; } ?></select></td></tr>
	    
	    <tr><td>Height</td><td><select name="feet" size="1"><?php for($i=3;$i<9;$i++){ echo "<option value="$i">$i</option>"; } ?></select> feet <select name="inches"><?php for($i=0;$i<12;$i++){ echo "<option value="$i">$i</option>"; } ?></select> inches</td></tr>
and the html output it creates:

Code: Select all

&lt;tr&gt;&lt;td&gt;&lt;a name="#dob" href="#dob" onClick="window.open('faq.php?seek=dob', 'faq', 'width=500,height=250,scrollbars=yes');"&gt;Date Of Birth&lt;/a&gt;&lt;/td&gt;&lt;td&gt;
		&lt;select name="month" size="1"&gt;&lt;option value="1"&gt;January&lt;/option&gt;&lt;option value="2"&gt;February&lt;/option&gt;&lt;option value="3"&gt;March&lt;/option&gt;&lt;option value="4"&gt;April&lt;/option&gt;&lt;option value="5"&gt;May&lt;/option&gt;&lt;option value="6"&gt;June&lt;/option&gt;&lt;option value="7"&gt;July&lt;/option&gt;&lt;option value="8"&gt;August&lt;/option&gt;&lt;option value="9"&gt;September&lt;/option&gt;&lt;option value="10"&gt;October&lt;/option&gt;&lt;option value="11"&gt;November&lt;/option&gt;&lt;option value="12"&gt;December&lt;/option&gt;&lt;/select&gt;&lt;select name="day" size="1"&gt; &lt;option value="1"&gt;1&lt;/option&gt;&lt;option value="2"&gt;2&lt;/option&gt;&lt;option value="3"&gt;3&lt;/option&gt;&lt;option value="4"&gt;4&lt;/option&gt;&lt;option value="5"&gt;5&lt;/option&gt;&lt;option value="6"&gt;6&lt;/option&gt;&lt;option value="7"&gt;7&lt;/option&gt;&lt;option value="8"&gt;8&lt;/option&gt;&lt;option value="9"&gt;9&lt;/option&gt;&lt;option value="10"&gt;10&lt;/option&gt;&lt;option value="11"&gt;11&lt;/option&gt;&lt;option value="12"&gt;12&lt;/option&gt;&lt;option value="13"&gt;13&lt;/option&gt;&lt;option value="14"&gt;14&lt;/option&gt;&lt;option value="15"&gt;15&lt;/option&gt;&lt;option value="16"&gt;16&lt;/option&gt;&lt;option value="17"&gt;17&lt;/option&gt;&lt;option value="18"&gt;18&lt;/option&gt;&lt;option value="19"&gt;19&lt;/option&gt;&lt;option value="20"&gt;20&lt;/option&gt;&lt;option value="21"&gt;21&lt;/option&gt;&lt;option value="22"&gt;22&lt;/option&gt;&lt;option value="23"&gt;23&lt;/option&gt;&lt;option value="24"&gt;24&lt;/option&gt;&lt;option value="25"&gt;25&lt;/option&gt;&lt;option value="26"&gt;26&lt;/option&gt;&lt;option value="27"&gt;27&lt;/option&gt;&lt;option value="28"&gt;28&lt;/option&gt;&lt;option value="29"&gt;29&lt;/option&gt;&lt;option value="30"&gt;30&lt;/option&gt;&lt;option value="31"&gt;31&lt;/option&gt;&lt;/select&gt;&lt;select name="year" size="1"&gt;&lt;option value="1900"&gt;1900&lt;/option&gt;&lt;option value="1901"&gt;1901&lt;/option&gt;&lt;option value="1902"&gt;1902&lt;/option&gt;&lt;option value="1903"&gt;1903&lt;/option&gt;&lt;option value="1904"&gt;1904&lt;/option&gt;&lt;option value="1905"&gt;1905&lt;/option&gt;&lt;option value="1906"&gt;1906&lt;/option&gt;&lt;option value="1907"&gt;1907&lt;/option&gt;&lt;option value="1908"&gt;1908&lt;/option&gt;&lt;option value="1909"&gt;1909&lt;/option&gt;&lt;option value="1910"&gt;1910&lt;/option&gt;&lt;option value="1911"&gt;1911&lt;/option&gt;&lt;option value="1912"&gt;1912&lt;/option&gt;&lt;option value="1913"&gt;1913&lt;/option&gt;&lt;option value="1914"&gt;1914&lt;/option&gt;&lt;option value="1915"&gt;1915&lt;/option&gt;&lt;option value="1916"&gt;1916&lt;/option&gt;&lt;option value="1917"&gt;1917&lt;/option&gt;&lt;option value="1918"&gt;1918&lt;/option&gt;&lt;option value="1919"&gt;1919&lt;/option&gt;&lt;option value="1920"&gt;1920&lt;/option&gt;&lt;option value="1921"&gt;1921&lt;/option&gt;&lt;option value="1922"&gt;1922&lt;/option&gt;&lt;option value="1923"&gt;1923&lt;/option&gt;&lt;option value="1924"&gt;1924&lt;/option&gt;&lt;option value="1925"&gt;1925&lt;/option&gt;&lt;option value="1926"&gt;1926&lt;/option&gt;&lt;option value="1927"&gt;1927&lt;/option&gt;&lt;option value="1928"&gt;1928&lt;/option&gt;&lt;option value="1929"&gt;1929&lt;/option&gt;&lt;option value="1930"&gt;1930&lt;/option&gt;&lt;option value="1931"&gt;1931&lt;/option&gt;&lt;option value="1932"&gt;1932&lt;/option&gt;&lt;option value="1933"&gt;1933&lt;/option&gt;&lt;option value="1934"&gt;1934&lt;/option&gt;&lt;option value="1935"&gt;1935&lt;/option&gt;&lt;option value="1936"&gt;1936&lt;/option&gt;&lt;option value="1937"&gt;1937&lt;/option&gt;&lt;option value="1938"&gt;1938&lt;/option&gt;&lt;option value="1939"&gt;1939&lt;/option&gt;&lt;option value="1940"&gt;1940&lt;/option&gt;&lt;option value="1941"&gt;1941&lt;/option&gt;&lt;option value="1942"&gt;1942&lt;/option&gt;&lt;option value="1943"&gt;1943&lt;/option&gt;&lt;option value="1944"&gt;1944&lt;/option&gt;&lt;option value="1945"&gt;1945&lt;/option&gt;&lt;option value="1946"&gt;1946&lt;/option&gt;&lt;option value="1947"&gt;1947&lt;/option&gt;&lt;option value="1948"&gt;1948&lt;/option&gt;&lt;option value="1949"&gt;1949&lt;/option&gt;&lt;option value="1950"&gt;1950&lt;/option&gt;&lt;option value="1951"&gt;1951&lt;/option&gt;&lt;option value="1952"&gt;1952&lt;/option&gt;&lt;option value="1953"&gt;1953&lt;/option&gt;&lt;option value="1954"&gt;1954&lt;/option&gt;&lt;option value="1955"&gt;1955&lt;/option&gt;&lt;option value="1956"&gt;1956&lt;/option&gt;&lt;option value="1957"&gt;1957&lt;/option&gt;&lt;option value="1958"&gt;1958&lt;/option&gt;&lt;option value="1959"&gt;1959&lt;/option&gt;&lt;option value="1960"&gt;1960&lt;/option&gt;&lt;option value="1961"&gt;1961&lt;/option&gt;&lt;option value="1962"&gt;1962&lt;/option&gt;&lt;option value="1963"&gt;1963&lt;/option&gt;&lt;option value="1964"&gt;1964&lt;/option&gt;&lt;option value="1965"&gt;1965&lt;/option&gt;&lt;option value="1966"&gt;1966&lt;/option&gt;&lt;option value="1967"&gt;1967&lt;/option&gt;&lt;option value="1968"&gt;1968&lt;/option&gt;&lt;option value="1969"&gt;1969&lt;/option&gt;&lt;option value="1970"&gt;1970&lt;/option&gt;&lt;option value="1971"&gt;1971&lt;/option&gt;&lt;option value="1972"&gt;1972&lt;/option&gt;&lt;option value="1973"&gt;1973&lt;/option&gt;&lt;option value="1974"&gt;1974&lt;/option&gt;&lt;option value="1975"&gt;1975&lt;/option&gt;&lt;option value="1976"&gt;1976&lt;/option&gt;&lt;option value="1977"&gt;1977&lt;/option&gt;&lt;option value="1978"&gt;1978&lt;/option&gt;&lt;option value="1979"&gt;1979&lt;/option&gt;&lt;option value="1980"&gt;1980&lt;/option&gt;&lt;option value="1981"&gt;1981&lt;/option&gt;&lt;option value="1982"&gt;1982&lt;/option&gt;&lt;option value="1983"&gt;1983&lt;/option&gt;&lt;option value="1984"&gt;1984&lt;/option&gt;&lt;option value="1985"&gt;1985&lt;/option&gt;&lt;option value="1986"&gt;1986&lt;/option&gt;&lt;option value="1987"&gt;1987&lt;/option&gt;&lt;option value="1988"&gt;1988&lt;/option&gt;&lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt;&lt;td&gt;Height&lt;/td&gt;&lt;td&gt;&lt;select name="feet" size="1"&gt;&lt;option value="3"&gt;3&lt;/option&gt;&lt;option value="4"&gt;4&lt;/option&gt;&lt;option value="5"&gt;5&lt;/option&gt;&lt;option value="6"&gt;6&lt;/option&gt;&lt;option value="7"&gt;7&lt;/option&gt;&lt;option value="8"&gt;8&lt;/option&gt;&lt;/select&gt; feet &lt;select name="inches"&gt;&lt;option value="0"&gt;0&lt;/option&gt;&lt;option value="1"&gt;1&lt;/option&gt;&lt;option value="2"&gt;2&lt;/option&gt;&lt;option value="3"&gt;3&lt;/option&gt;&lt;option value="4"&gt;4&lt;/option&gt;&lt;option value="5"&gt;5&lt;/option&gt;&lt;option value="6"&gt;6&lt;/option&gt;&lt;option value="7"&gt;7&lt;/option&gt;&lt;option value="8"&gt;8&lt;/option&gt;&lt;option value="9"&gt;9&lt;/option&gt;&lt;option value="10"&gt;10&lt;/option&gt;&lt;option value="11"&gt;11&lt;/option&gt;&lt;/select&gt; inches&lt;/td&gt;&lt;/tr&gt;
as you can see the php use is quite handy.

Posted: Fri Jul 04, 2003 8:59 pm
by visionary
Major help thanks to m3rajk,

Select simplified and varified below!

<select name="over" class="formw" id="over">
<?php
if(isset($_POST['over'])){
for($i=3;$i<26;$i++){
if($_POST['over']==$i){
echo "<option value=\"$i Years\" selected >$i Years</option>";
}else{
echo "<option value=\"$i Years\">$i Years</option>";
}
}
if($_POST['over']==''){ echo '<option value="" selected>Please Select</option>'; }
}else{
echo '<option value="" selected >Please Select</option>';
for($i=3;$i<26;$i++){
echo "<option value=\"$i Years\">$i Years</option>";
}
} ?>
</select>

Posted: Wed Jul 09, 2003 6:48 am
by macewan
m3rajk wrote:try starting the file like this...


another thing you cando is break the rendering into sections...

Code: Select all

<?php
/* stuff from above only rendering is done by calling functions */

function pagehead(){
?>
<!-- html for the begining of the page -->
<?php
}

function error_messages($errors){
?>
<!-- html for errors with a <?php for($i=0;$i<count$errors;$i++){ echo $error[$i]; } ?> -->
<?php
}
function page_1st_visit($db stuff passed){
?>
<!-- html for page -->
<?php
}

function thank_you($anything you need to pass)(
?>
<!-- html for page -->
<?php
}

function end_page(){
?>
<!-- html for page -->
<?php
}
?>

One of the books I've been reading on php suggest that it's better/quicker to try and stay in php as much as possible and just echo your <html> when it's needed. What are your feelings on this. I'm in the middle of recoding a small site to just echo all my ( x)html.

Heavy code I would say

Posted: Wed Jul 09, 2003 7:46 am
by visionary
Hi,

I think that echoing your entire pages code out that many times would be heavy on the page file size and rendering of the page that many times etc.

Rather use the feature of PHP to be able to pop in and out of the page formatted code and simply display what is relevant and needed.

If your page as an simplr (X)HTML page would be 10k the way you are going to program it it will multipy by the number of different page setups you have in your page. So for the code above you would have increased the page X5 which for users loading and server stuff is weighty for simple code.

I could be very wrong here as I can see it will be executed by if blah = blah etc. then function () but it would be smoother and easier to edit and update your site if the page is simply programmed once with your iterations throughout.

The same way I am updating the PHP code written above as such

Code: Select all

<?php

// setup error variables.
$error = false;       // This boolean value (flag) marks whether an error
                      // has been encountered.
$error_marker = '*';  // use $error_marker when generating a required form
                      // field that was not filled in the first time around.
                      // This means that if you decide later to change your
                      // error marker (for example, to the string
                      // "REQUIRED!"), you only need change this line.

$error_texts = array(); // the error_texts array will hold all error
                        // messages to be displayed to the user.
                        // currently, your only error messages are
                        // "Required fields missing!" and
                        // "Invalid email address!", but doing it this way
                        // gives you the flexibility to add additional
                        // or more descriptive error messages in the future.
                        // http://www.php.net/function.array

// Check if form Submitted
if (!isset($_POST['submit']))
{
    $error = true;
    $error_texts[] = "Form not submitted!";   // http://www.php.net/types.array
}
else // form submitted
{    
    // set up all the expected (required) POST form fields.
    $fields = array('borrow', 'over', 'property_value', 'outstanding_mortgage',
                    'applicant1_name', 'address', 'town_city', 'postcode',
                    'home_tel', 'work_tel', 'mob_tel', 'time_call', 'call_my',
                    'email', 'app1_am', 'app1_earn');
    // set up all the default (unacceptable) values for POST form fields.
    $defaults = array('over' => '',
                      'time_call' => 'Please Select',
                      'call_my'   => 'Please Select',
                      'app1_am'   => 'Please Select');

    // Check that all required form fields are completed
    $num_fields = count($fields);               // http://www.php.net/count
    for($i = 0; !$error && ($i < $num_fields); ++$i)
    {
        $error = isset($_POST[$fields[$i]);     // http://www.php.net/isset
    }
    
    $reset($defaults);
    while(!$error)
    {
         list($key, $val) = each($defaults);    // http://www.php.net/list
                                                // http://www.php.net/each

         // error when the posted val is the default.
         $error = $_POST[$key] == $val;
    }
    
    if($error)  // A required field was missing or set to the default value.
                // Add an error message to the error_texts array.
    {
        $error_texts[] = "Required Field(s) Missing ({$error_marker})!";
    }
    
    // Check if email exists and verify its syntax
    if (isset($_POST['email']))
    {
        $pattern = '^[a-z0-9]+([-_\.]?[a-z0-9])+@'
                 . '[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}';
        if(eregi($pattern, $_POST['email']))    // http://www.php.net/eregi
        {
            // valid email verified so report form submitted
            echo "<fieldset><legend>Application Received</legend>...";
            // Mail form to us
            $mail_to = "EMAIL HERE";
            $mail_subject = "Application";
            $mail_body = "<html>\n";
            // ...
            // rest of form to be mailed in here!!
            // ...
            $header    = "From: $email\r\n";
            // ...
            mail($mail_to, $mail_subject, $mail_body, $header);
                                            // http://www.php.net/function.mail
        }
        else // invalid email
        {
            $error = true;
            $error_texts[] = "Invalid email address!";
        }
    }
}    
// Output error messages, if any
if ($error)
{
    echo '<div align="center"><span class="red">'
       . join(" ", $error_texts)                // http://www.php.net/join
       . '</span></div>';
}

?>
This thanks to Nik @ Wrox forums Beggining PHP who has pointed out the error of my assumption of global variables, holes instigated by my code as security flaws etc. and the updatability of the code for future iteration and amendments as well as less server intensive operation by only checking certain elements for varification once namely the eregi on the email sytax etc.

Hope that is of some use and makes some sense

All the best

V :lol: