Jump to content

How To Avoid Multiple Include Files.


Beeeeney

Recommended Posts

I'm building a form for our website where customers enter their information. Not sure how best to explain my problem but basically there are 3 points on the form where the date must be submitted. I have the code for generating a date dropdown in an include file, but with Javascript validation I need all of the dropdowns to be named seperately.

 

Does anyone understand what I mean?

 

 

Generate dropdowns (includeDOB.php):

	 <select>
	 <option selected>Select</option>

	 <?php
	 $day=1;
	 while($day<32) {

		 ?>

		 <option value="<?php echo $day; ?>"><?php echo $day; ?></option>
	 <?php
		 $day++;



	 }
	 ?>
	 </select>



	 <select>
	 <option selected>Select</option>
	 <?php
	 $month = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Novermber", "December");

	 foreach ($month as $key) {
	 ?> <option value="<?php echo $key; ?>"><?php echo $key; ?></option>
	 <?php } ?>
	 </select>



	 <select>
	 <option selected>Select</option>	
				 <?php
	 $year=1901;
	 while($year<2012 && $year > 1900) {

		 ?>

		 <option value="<?php echo $year; ?>"><?php echo $year; ?></option>
	 <?php
		 $year++;



	 }
	 ?>

	 </select>

 

Form:

<form action="submit.php" method="post" name="CustomerInformation" onsubmit="return validatePage()">

Booking Reference: <input type="text" name="Bookingreference"><br>

 <h1>Passport Details</h1>
 <!--PASSPORT INFORMATION -->


Title:	 <select name="Title">
		 <option selected>Please Choose</option>
		 <option value="Mr">Mr</option>
		 <option value="Mrs">Mrs</option>
		 <option value="Mr">Ms</option>
		 <option value="Dr">Dr</option>
	 </select>
	 <br>

First Name: <input type="text" name="Firstname"><br>
Middle Name:<input type="text" name="Middlename"><br>
Last Name: <input type="text" name="Lastname"><br>		
Date of Birth: <?php include("includeDOB.php"); ?>





<br>Passport Number: <input type="text" name="PassportNo"><br>
Date of Issue:		 <?php include("includeDOB.php"); ?><br>
Date of Expiry:		 <?php include("includeDOB.php"); ?><br>
Nationality:	 <input type="text" name="Nationality"><br>
Issue Country:	 <input type="text" name="Issuecountry"><br>


	 <h1>Next of Kin Information</h1>
	 <!--NEXT OF KIN INFO v -->

Full Name:		 <input type="text" name="Fullname"><br>
Address:		 <input type="text" name="Address"><br>
Postcode:		 <input type="text" name="Postcode"><br>
Relationship:	 <input type="text" name="Relationship"><br>
Mobile Number:	 <input type="text" name="Mobilenumber"><br>
Daytime Phone:	 <input type="text" name="Daytimephone"><br>
Evening Phone:	 <input type="text" name="Eveningphone"><br>
Email Address:	 <input type="text" name="Email"><br>


	 <h1>Travel Insurance Details</h1>
	 <!--TRAVEL INSURANCE DETAILS-->

Insurance Company: <input type="text" name="Company"><br>
Policy Number:	 <input type="text" name="Policynumber"><br>
Medical Company Name:<input type="text" name="Underwriter"><br>
Emergency Medical Contact Number:<input type="text" name="Emergencynumber"><br><br>

I have read and agree to the <a href="#">terms and conditions</a><input type="checkbox" name="checkbox">
<br><br><input type="submit" value="Submit">

   </form>  
   </body>
   </html>

 

Javascript validation:

 

<script type="text/javascript">

function validatePage() {
var msg="";

if (document.forms.CustomerInformation.Title.value == "Please Choose") {
msg += "* Please provide a Title\n";
} else {
msg += "";
}
if (document.forms.CustomerInformation.Firstname.value == "") {
msg += "* Please provide a First Name\n";
}
if (document.forms.CustomerInformation.Lastname.value == "") {
msg += "* Please provide a Surname\n";
}
if (document.forms.CustomerInformation.Nationality.value == "") {
msg += "* Please provide a Nationality\n";
}	
if (document.forms.CustomerInformation.Issuecountry.value == "") {
msg += "* Please provide a Country of Issue\n";
}
if (document.forms.CustomerInformation.Fullname.value == "") {
msg += "* Please provide a Full name for your next of kin\n";
}
if (document.forms.CustomerInformation.Address.value == "") {
msg += "* Please provide an Address for your next of kin\n";
}
if (document.forms.CustomerInformation.Postcode.value == "") {
msg += "* Please provide a Postcode for your next of kin\n";
}
if (document.forms.CustomerInformation.Relationship.value == "") {
msg += "* Please give details of your Relationship to your next of kin\n";
}
if (document.forms.CustomerInformation.Mobilenumber.value == "") {
msg += "* Please provide a Mobile Number for your next of kin\n";
}
if (document.forms.CustomerInformation.checkbox.checked == false) {
msg += "* Please read and agree to the Terms and Conditions\n";
}


if (msg == "") {
return true;
} else {
alert("This form cannot be submitted. Please correct the following issue(s):\n\n"+ msg);
return false;
}
}


</script>

 

Basically I need to validate the Date of Birth, Date of Issue and Date of Expiry separately. I may be missing something simple but I don't want to have 3 different include files for each date entry.

Edited by Beeeeney
Link to comment
Share on other sites

You haven't exactly explained your issue.

 

Sorry if I seem vague. I need to validate the three separate date entries with Javascript, to stop the form being submitted if the field returns as not selected. My code for generating the dropdowns for selecting Day, Month and Year is in a separate include file. Now, if I were to validate this part of the form with Javascript, it would try to validate all three different date entries with the one Javascript statement.

 

Basically I need to have three different <select><?php generate dropdowns?></select>, so I can validate them separately, without having three different include files.

Link to comment
Share on other sites

Basically I need to have three different <select><?php generate dropdowns?></select>, so I can validate them separately, without having three different include files.

 

Still not understanding the issue. If you need three drop downs, you need the code for the three drop downs. What's it matter if they are in include files or not?

Link to comment
Share on other sites

[/font][/color]

 

Still not understanding the issue. If you need three drop downs, you need the code for the three drop downs. What's it matter if they are in include files or not?

 

*facepalm*

 

I have the code for the three dropdowns. One is for Day, one is for Month and one is for Year. I have three separate bits on the form where a date needs to be entered. Date of Birth, Date of Issue and Date of Expiry.

 

I need to validate them separately with Javascript, but I am using the same code for each instance that the date needs to be entered on the form. Now because of this, when the Javascript tries to validate, it won't be able to determine which one of the three is wrong.

 

I need to have separate code for each date entry, but I wanted to know if there was a way to do it that didn't involve me repeating the code three times or making three separate include files.

Edited by Beeeeney
Link to comment
Share on other sites

Have your include file accept a variable that defines the ID/Name of the select boxes, then use that to identify which ones to validate.

 

eg:

Date of Birth: <?php $fieldName='birth'; include("includeDOB.php"); ?>

 

                 <select name="<?php echo $fieldName;?>_Day" id="<?php echo $fieldName;?>_Day">
                 <option selected>Select</option>
                
                 <?php
                 $day=1;
                 while($day<32) {
...

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.