Passing a variable from Javascript to PHP

I have a function which validates a form, which i've cut short just to show what i'm after.  
function validate_flight_form ( )
{
	var d = new Date();
	var date = d.getDate();
	var mon = d.getMonth() + 1;
		var pass = document.flight_form.pass.value;
	pricing = true;
	var value = 0;
	valid = true;	

else if ((document.flight_form.type[1].checked) && ((document.flight_form.day.value <= date - 21) && (document.flight_form.month.value == mon + 1) || ((document.flight_form.day.value <= date + 7) && (document.flight_form.month.value == mon + 0 ))))
{
	value = 200;
		
	pricing = false; 
	
}

 if (document.flight_form.pass.value >= 1)
{
    value = pass * value;
		
}

return valid;
return pricing;

}

Open in new window


Now I want to pass the variable 'value' to price.php to use later, using:

location.href="price.php?value=" + value;

My problem is i'm not sure where to put that code in the function as it doesn't seem to work wherever I put it.

price.php is set up just to test if it's working by:
 
<?php

//shorter variable names
session_start();
$HTTP_POST_VARS = $_POST;

$HTTP_SESSION_VARS = $_SESSION;

//create short variable names
$value = $_POST['value'];

echo ('$value');
?>

Open in new window

This may not be correct as i'm new to PHP.

Anyone offer any help?
LVL 1
Lexx877Asked:
Who is Participating?
 
minjosefaCommented:
Better solution:

HTML:
<form....
  <input type="hidden id="my_value" name="value" value="" />
</form>

Open in new window


Javascript:
document.getElementById('my_value').value = value;
return valid;
//return pricing; 

Open in new window


PHP:
$value = $_POST['value']; 

Open in new window

0
 
ikrommCommented:
First and foremost, you are using a GET method and you have the PHP script to read from the POST variables.

Apart from that, can you please explain what you need to do exactly, I don't understand...
0
 
minjosefaCommented:
Javascript:
window.location = "price.php?value=" + value;

Open in new window


PHP:
$value = $_GET['value'];

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Dave BaldwinFixer of ProblemsCommented:
These are going to cause problems.  Besides, they already contain the same info.

$HTTP_POST_VARS = $_POST;

$HTTP_SESSION_VARS = $_SESSION;

Just use the versions on the right, $_POST and $_SESSION, they're the current methods.

http://www.php.net/manual/en/reserved.variables.post.php
http://www.php.net/manual/en/reserved.variables.session.php
0
 
Mohamed AbowardaSoftware EngineerCommented:
By using AJAX you can pass variables to any PHP script by using POST or GET method:

Using AJAX in jQuery:
http://api.jquery.com/jQuery.ajax/

Using AJAX without jQuery:
http://www.tizag.com/ajaxTutorial/ajax-javascript.php
0
 
Lexx877Author Commented:
I didn't want to paste the entire function but i'll put it here. It validates a form i'll put at the bottom, and sets a value to 'value' depending on the user choices. That code works perfectly.  You want to go to line 238:
 
function validate_flight_form ( )
{
	var d = new Date();
	var date = d.getDate();
	var mon = d.getMonth() + 1;
		var pass = document.flight_form.pass.value;
	pricing = true;
	var value = 0;
	valid = true;



//makes sure check boxes are checked
if ((document.flight_form.type[0].checked == false )
&& ( document.flight_form.type[1].checked == false ))
{
document.getElementById("error").innerHTML="You must select either a One-way or Return trip";	
valid = false;
}

//makes sure the destination is filled
else if ( document.flight_form.destination.value != "Paris" )
{

document.flight_form.destination.value = "Paris";
document.flight_form.departfrom.focus(); //as the field is filled for the user, the next field is selected for them
document.getElementById("error").innerHTML="You must input a destination...We're sorry, due to problems beyond our control only flights to Paris are currently availiable.";
valid = false;	
}
//makes sure the depart value is filled
else if (document.flight_form.departfrom.value != "York")
{
document.flight_form.departfrom.value = "York";
	
document.getElementById("error").innerHTML="You must input a departing airport...We're sorry, due to problems beyond our control only flights from York are availiable";
valid = false;
}
//makes sure depart dates are entered
else if ((document.flight_form.day.value == "") || (document.flight_form.month.value== "") || (document.flight_form.year.value == ""))
{
	document.getElementById("error").innerHTML="You must input a depart date; the day, month and year. ";
	document.flight_form.day.focus();
	valid = false;
}
//performs function IsNumeric on characters in the depart date boxes
else if ((!IsNumeric(document.flight_form.day.value))|| (!IsNumeric(document.flight_form.month.value)) || (!IsNumeric(document.flight_form.year.value)))
{
	document.getElementById("error").innerHTML="You must only input numbers in the date boxes such as 01, 02, 03..."
	valid = false;
}


//very messy way of ensuring the correct entries are made to the day field. Cannot use < 1 or > 31 because this assumes integers, which 
//Javascript automatically converts from 01, 02 etc.
else if ((document.flight_form.day.value != '01')&& (document.flight_form.day.value != '02') &&  (document.flight_form.day.value != '03') &&  (document.flight_form.day.value != '04') && (document.flight_form.day.value != '05') &&  (document.flight_form.day.value != '06') &&  (document.flight_form.day.value != '07') &&  (document.flight_form.day.value != '08') && (document.flight_form.day.value != '09') && (document.flight_form.day.value != '10')&&(document.flight_form.day.value != '10')&& (document.flight_form.day.value != '11') &&  (document.flight_form.day.value != '12') &&  (document.flight_form.day.value != '13') && (document.flight_form.day.value != '14') &&  (document.flight_form.day.value != '15') &&  (document.flight_form.day.value != '16') &&  (document.flight_form.day.value != '17') && (document.flight_form.day.value != '18') && (document.flight_form.day.value != '19')&&(document.flight_form.day.value != '20')&& (document.flight_form.day.value != '21') &&  (document.flight_form.day.value != '22') &&  (document.flight_form.day.value != '23') && (document.flight_form.day.value != '24') &&  (document.flight_form.day.value != '25') &&  (document.flight_form.day.value != '26') &&  (document.flight_form.day.value != '27') && (document.flight_form.day.value != '28') && (document.flight_form.day.value != '29')&& (document.flight_form.day.value != '30') && (document.flight_form.day.value != '31'))
{
document.getElementById("error").innerHTML="Day values must of the correct format: 01, 02, 03...and 31 or below";
valid = false;
}

else if ((document.flight_form.day.value < date) && (document.flight_form.month.value <= mon))
{
document.getElementById("error").innerHTML="Your depart date cannot be in the past!";
valid = false;	
}
//cannot have a day over 28 as Feburary does not have any days over 28, unless it's a leap year. 2011 is not a leap year and the form only allows year 2011 to book flights. 
else if ((document.flight_form.day.value > 28) &&(document.flight_form.month.value == '02'))
{
document.getElementById("error").innerHTML="Febuary has only 28 days this year, please input a day 28 or below";
valid = false;
}

//ensure that months April, June, September and November cannot be booked on the 31st, as these months only reach 30 days.
else if ((document.flight_form.day.value > 30) && ((document.flight_form.month.value == '04') || (document.flight_form.month.value == '06') || (document.flight_form.month.value == '09') || (document.flight_form.month.value == '11')))
{
document.getElementById("error").innerHTML="Problem with depart date: April, June, September and November only have 30 days";	
valid = false;

}

//very messy way of ensuring months are in correct format
else if ((document.flight_form.month.value != '01')&& (document.flight_form.month.value != '02') &&  (document.flight_form.month.value != '03') &&  (document.flight_form.month.value != '04') && (document.flight_form.month.value != '05') &&  (document.flight_form.month.value != '06') &&  (document.flight_form.month.value != '07') &&  (document.flight_form.month.value != '08') && (document.flight_form.month.value != '09') && (document.flight_form.month.value != '10') && (document.flight_form.month.value != '11')&& (document.flight_form.month.value != '12')) 
{
document.getElementById("error").innerHTML="Month values must of the correct format: 01, 02, 03...";
valid = false;
}
//flights are only availaible in 2011
else if (document.flight_form.year.value != 2011)
{
document.getElementById("error").innerHTML="Flights are only currently availiable for 2011, we have updated your input";
document.flight_form.year.value = 2011;	
valid = false;
}

//the following are the same but for Return dates, in the same order.
else if ((document.flight_form.dayr.value == "") || (document.flight_form.monthr.value== "") || (document.flight_form.yearr.value == ""))
{
	document.getElementById("error").innerHTML="You must input a return date; the day, month and year.";
	document.flight_form.dayr.focus();
	valid = false;
}
else if ((!IsNumeric(document.flight_form.dayr.value))|| (!IsNumeric(document.flight_form.monthr.value)) || (!IsNumeric(document.flight_form.yearr.value)))
{
	document.getElementById("error").innerHTML="You must only input numbers in the date boxes such as 01, 02, 03..."
	valid = false;
}


else if ((document.flight_form.dayr.value != '01')&& (document.flight_form.dayr.value != '02') &&  (document.flight_form.dayr.value != '03') &&  (document.flight_form.dayr.value != '04') && (document.flight_form.dayr.value != '05') &&  (document.flight_form.dayr.value != '06') &&  (document.flight_form.dayr.value != '07') &&  (document.flight_form.dayr.value != '08') && (document.flight_form.dayr.value != '09') && (document.flight_form.dayr.value != '10')&&(document.flight_form.dayr.value != '10')&& (document.flight_form.dayr.value != '11') &&  (document.flight_form.dayr.value != '12') &&  (document.flight_form.dayr.value != '13') && (document.flight_form.dayr.value != '14') &&  (document.flight_form.dayr.value != '15') &&  (document.flight_form.dayr.value != '16') &&  (document.flight_form.dayr.value != '17') && (document.flight_form.dayr.value != '18') && (document.flight_form.dayr.value != '19')&&(document.flight_form.dayr.value != '20')&& (document.flight_form.dayr.value != '21') &&  (document.flight_form.dayr.value != '22') &&  (document.flight_form.dayr.value != '23') && (document.flight_form.dayr.value != '24') &&  (document.flight_form.dayr.value != '25') &&  (document.flight_form.dayr.value != '26') &&  (document.flight_form.dayr.value != '27') && (document.flight_form.dayr.value != '28') && (document.flight_form.dayr.value != '29')&& (document.flight_form.dayr.value != '30') && (document.flight_form.dayr.value != '31'))
{
document.getElementById("error").innerHTML="Day values must of the correct format: 01, 02, 03...and 31 or below";
valid = false;
}
else if ((document.flight_form.dayr.value < date) && (document.flight_form.monthr.value <= mon))
{
document.getElementById("error").innerHTML="Your return date cannot be in the past!";
valid = false;	
}
else if ((document.flight_form.dayr.value > 28) &&(document.flight_form.monthr.value == '02'))
{
document.getElementById("error").innerHTML="Febuary has only 28 days this year, please input a day 28 or below";
valid = false;
}
//make sure the month values are in the correct format, and only these numbers are used. Var months used to reduce amount of code. 
else if ((document.flight_form.dayr.value >30) &&  ((document.flight_form.monthr.value ==  '04') || (document.flight_form.monthr.value == '06') || (document.flight_form.monthr.value == '09') || (document.flight_form.monthr.value == '11')))
{
document.getElementById("error").innerHTML="April, June, September and November only have 30 days";	
valid = false;
}

else if ((document.flight_form.monthr.value != '01')&& (document.flight_form.monthr.value != '02') &&  (document.flight_form.monthr.value != '03') &&  (document.flight_form.monthr.value != '04') && (document.flight_form.monthr.value != '05') &&  (document.flight_form.monthr.value != '06') &&  (document.flight_form.monthr.value != '07') &&  (document.flight_form.monthr.value != '08') && (document.flight_form.monthr.value != '09') &&  (document.flight_form.monthr.value != '10') &&  (document.flight_form.monthr.value != '11') &&  (document.flight_form.monthr.value != '12')) 
{
document.getElementById("error").innerHTML="Month values must be of the correct format, between 01 and 12";
valid = false;
}

else if (document.flight_form.yearr.value != 2011)
{
document.getElementById("error").innerHTML="Flights are only currently availiable for 2011, we have updated your input";
document.flight_form.yearr.value = 2011;	
valid = false;
}

else if (document.flight_form.pass.value == '')
{
document.getElementById("error").innerHTML="You must state how many passengers will be taking the flight";	
valid = false;
}

else if (document.flight_form.pass.value > 16)
{
	document.getElementById("error").innerHTML="You can have a maximum of 16 passengers";
	
	valid = false;
}


 
if ((document.flight_form.type[0].checked) && ((document.flight_form.month.value >= mon + 2) || ((mon == 01) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 03) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 05) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 07) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 08) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 10) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 12) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) ||((document.flight_form.day.value >= date - 1) && (document.flight_form.month.value == mon + 1)|| ((document.flight_form.day.value >= date + 29 ) && (document.flight_form.month.value == mon + 0))))))))))))
	
	{
	 value = 100;
	
	 pricing = false; 
	
	}
	//less than 29 days in advance (counting whole days)
else if ((document.flight_form.type[0].checked) &&   ((document.flight_form.day.value >= date - 14) && (document.flight_form.month.value == mon + 1 ) ||((document.flight_form.day.value >= date + 15) && (document.flight_form.day.value <= date + 28)&& (document.flight_form.month.value == mon + 0))))
	
{
	value = 120;
	
	pricing = false; 

}
//less than 14 days in advance
	else if ((document.flight_form.type[0].checked) && ((document.flight_form.day.value >= date + 8) && (document.flight_form.day.value <= date + 14) && (document.flight_form.month.value == mon + 0) || ((document.flight_form.day.value <= date - 15) && (document.flight_form.day.value >= date - 22) && (document.flight_form.month.value == mon + 1))))

{
		value = 130;
	
	pricing = false; 

//Less than 8 days in advance
}
	else if ((document.flight_form.type[0].checked) && ((document.flight_form.day.value <= date - 21) && (document.flight_form.month.value == mon + 1) || ((document.flight_form.day.value <= date + 7) && (document.flight_form.month.value == mon + 0 ))))
{
	value = 150;
	
	pricing = false; 
	
}
	//RETURN PRICING
	//adds in conditions of months which have 31 days where the calculation has to be different. 
	else if ((document.flight_form.type[1].checked) && ((document.flight_form.month.value >= mon + 2) || ((mon == 01) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 03) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 05) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 07) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 08) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 10) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 12) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) ||((document.flight_form.day.value >= date - 1) && (document.flight_form.month.value == mon + 1)|| ((document.flight_form.day.value >= date + 29 ) && (document.flight_form.month.value == mon + 0))))))))))))
	
	{
	 value = 150;
	
	 pricing = false; 
	
	}
	//less than 29 days in advance (counting whole days)
else if ((document.flight_form.type[1].checked) &&   ((document.flight_form.day.value >= date - 14) && (document.flight_form.month.value == mon + 1 ) ||((document.flight_form.day.value >= date + 15) && (document.flight_form.day.value <= date + 28)&& (document.flight_form.month.value == mon + 0))))
	
{
	value = 170;

	pricing = false; 

}
//less than 14 days in advance
	else if ((document.flight_form.type[1].checked) && ((document.flight_form.day.value >= date + 8) && (document.flight_form.day.value <= date + 14) && (document.flight_form.month.value == mon + 0) || ((document.flight_form.day.value <= date - 15) && (document.flight_form.day.value >= date - 22) && (document.flight_form.month.value == mon + 1))))

{
		value = 180;
	
	pricing = false; 

//Less than 8 days in advance
}
	else if ((document.flight_form.type[1].checked) && ((document.flight_form.day.value <= date - 21) && (document.flight_form.month.value == mon + 1) || ((document.flight_form.day.value <= date + 7) && (document.flight_form.month.value == mon + 0 ))))
{
	value = 200;
		
	pricing = false; 
	
}

 if (document.flight_form.pass.value >= 1)
{
    value = pass * value;
		
}

return valid;
return pricing;
window.location ="price.php?value=" + value;

}

Open in new window


window.location ="price.php?value=" + value; is in the wrong place I realise that. It needs to run after the form successfully passes all the validation criteria.  The form runs that function as its 'onSubmit' value as seen in the below code. I've fixed the PHP thanks to the above comments too.

 
<form name ="flight_form" onSubmit="return validate_flight_form ();" >


<tr> 
<td> 
<input name="type"  type="radio" value="oneway" /> <label for="type">One-way </label></td> 
<td colspan="5"> 
<input name="type" type="radio" value="return" /> <label for="type">Return</label></td> 
</tr> 
<tr> 
<td><label for="destination">Destination:</label></td> 
<td colspan="5"> 
<input id="destination" name="destination" maxlength="100" size="20" type="text" /></td> 
</tr> 
<tr> 
<td><label for="departfrom">Departing from:</label></td> 
<td colspan="5"> 
<input id="departfrom" name= "departfrom" maxlength="100" size="20" type="text" /></td> 
</tr> 
<tr> 
<td><label for="day">Depart date: &nbsp;&nbsp;&nbsp;Day:</label></td> 
<td> 
<input id="day" name="day" maxlength="2" size="2" type="text" /></td> 
<td><label for="month">Month:</label></td> 
<td> 
<input id="month" name="month" maxlength="2" size="2" type="text" /></td> 

<td>
<label for="year">Year:</label>
</td> 
<td> 
<input id="year" name="year" maxlength="4" size="4" type="text" /> 
<formhint>In format DD/MM/YYYY </formhint>
</td> 
</tr> 
<tr> 
<td><label for="dayr">Return date: &nbsp;&nbsp;&nbsp;Day:</label></td> 
<td> 
<input id="dayr" name="dayr" maxlength="2" size="2" type="text" /></td> 
<td><label for="monthr">Month:</label></td> 
<td> 
<input id="monthr" name="monthr" maxlength="2" size="2" type="text" /></td> 
<td><label for="yearr">Year:</label></td> 
<td> 
<input id="yearr" name="yearr" maxlength="4" size="4" type="text" /></td> 
</tr> 
<tr>
<td>
<label for="pass">No. Passengers</label></td> 
<td colspan="5"><input id="pass" name="pass" maxlength="2" size="2" type="text"/> <formhint>Max. 16 passengers </formhint></td>

</tr>
<tr>
<td colspan="5" align='center'>
     <input type='submit' value='Confirm'></td>
</tr>


</form> 
</table>

Open in new window

0
 
minjosefaCommented:
Your code below will never reach window.location ="price.php?value=" + value;, because it stops executing after your first "return".

return valid;
return pricing;
window.location ="price.php?value=" + value;

Open in new window


So try this:
//return valid;
//return pricing;
window.location ="price.php?value=" + value;

Open in new window

0
 
minjosefaCommented:
Sorry for my last post; try something like this:

HTML & Javascript:
document.flight_form.action  = "price.php?value=" + value; 
return valid;
return pricing;

Open in new window

0
 
Lexx877Author Commented:
Like I said I knew it was in the wrong place just putting it there for somewhere to put :) I've tried putting it where you suggested, but if that runs before the two returns, no form validation takes place as it runs window.location ="price.php?value=" + value; as soon as you press 'Submit' I assume as it reaches that line and executes it, before the returns and gives 'price.php?value=0' in the url string, as of course value hasn't had anything else assigned to it and the form is empty.

If I fill the form in anyway with correct values, and press submit, it doesn't work and resets the form as if there is an error.
0
 
Lexx877Author Commented:
Which is the point I got confused and came here :P
0
 
Lexx877Author Commented:
Bit confused about the HTML part:

Is that to go with the current submit button, or to replace it? Obviously replacing it means there is no submit button at all so you can't submit the form at all. Apologise if I'm being dense!
0
 
Lexx877Author Commented:
The entire.js if it helps including functions used in the one to validate the form:

 
function GetPrice()
{
	var d = new Date();
	var date = d.getDate();
	var mon = d.getMonth() + 1; //month values are 0-11 for no apparant reason, this makes the first month 1 not 0. 
	var pass = document.flight_form.pass.value;
	pricing = true;
	var value = 0;
	
	
	//ONE-WAY PRICES 
	//adds in conditions of months which have 31 days where the calculation has to be different. 
	
if ((document.flight_form.type[0].checked) && ((document.flight_form.month.value >= mon + 2) || ((mon == 01) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 03) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 05) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 07) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 08) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 10) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 12) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) ||((document.flight_form.day.value >= date - 1) && (document.flight_form.month.value == mon + 1)|| ((document.flight_form.day.value >= date + 29 ) && (document.flight_form.month.value == mon + 0))))))))))))
	
	{
	 value = 100;
	
	 pricing = false; 
	
	}
	//less than 29 days in advance (counting whole days)
else if ((document.flight_form.type[0].checked) &&   ((document.flight_form.day.value >= date - 14) && (document.flight_form.month.value == mon + 1 ) ||((document.flight_form.day.value >= date + 15) && (document.flight_form.day.value <= date + 28)&& (document.flight_form.month.value == mon + 0))))
	
{
	value = 120;
	
	pricing = false; 

}
//less than 14 days in advance
	else if ((document.flight_form.type[0].checked) && ((document.flight_form.day.value >= date + 8) && (document.flight_form.day.value <= date + 14) && (document.flight_form.month.value == mon + 0) || ((document.flight_form.day.value <= date - 15) && (document.flight_form.day.value >= date - 22) && (document.flight_form.month.value == mon + 1))))

{
		value = 130;
	
	pricing = false; 

//Less than 8 days in advance
}
	else if ((document.flight_form.type[0].checked) && ((document.flight_form.day.value <= date - 21) && (document.flight_form.month.value == mon + 1) || ((document.flight_form.day.value <= date + 7) && (document.flight_form.month.value == mon + 0 ))))
{
	value = 150;
	
	pricing = false; 
	
}
	//RETURN PRICING
	//adds in conditions of months which have 31 days where the calculation has to be different. 
	else if ((document.flight_form.type[1].checked) && ((document.flight_form.month.value >= mon + 2) || ((mon == 01) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 03) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 05) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 07) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 08) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 10) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 12) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) ||((document.flight_form.day.value >= date - 1) && (document.flight_form.month.value == mon + 1)|| ((document.flight_form.day.value >= date + 29 ) && (document.flight_form.month.value == mon + 0))))))))))))
	
	{
	 value = 150;
	
	 pricing = false; 
	
	}
	//less than 29 days in advance (counting whole days)
else if ((document.flight_form.type[1].checked) &&   ((document.flight_form.day.value >= date - 14) && (document.flight_form.month.value == mon + 1 ) ||((document.flight_form.day.value >= date + 15) && (document.flight_form.day.value <= date + 28)&& (document.flight_form.month.value == mon + 0))))
	
{
	value = 170;
		
	pricing = false; 

}
//less than 14 days in advance
	else if ((document.flight_form.type[1].checked) && ((document.flight_form.day.value >= date + 8) && (document.flight_form.day.value <= date + 14) && (document.flight_form.month.value == mon + 0) || ((document.flight_form.day.value <= date - 15) && (document.flight_form.day.value >= date - 22) && (document.flight_form.month.value == mon + 1))))

{
		value = 180;
	
	pricing = false; 

//Less than 8 days in advance
}
	else if ((document.flight_form.type[1].checked) && ((document.flight_form.day.value <= date - 21) && (document.flight_form.month.value == mon + 1) || ((document.flight_form.day.value <= date + 7) && (document.flight_form.month.value == mon + 0 ))))
{
	value = 200;
		
	pricing = false; 
	
}

 if (document.flight_form.pass.value >= 1)
{
    value = pass * value;
	document.getElementById("price").innerHTML="The total cost of your flight will be:" + " " + "£" + value;
	pricing = false;
}
	return pricing;
}

function IsNumeric(sText)

{
var ValidChars = "0123456789";
var IsNumber=true;
var Char;


for (i = 0; i < sText.length && IsNumber == true; i++) 
{ 
Char = sText.charAt(i); 
if (ValidChars.indexOf(Char) == -1) 
{
IsNumber = false;
}
}
return IsNumber;

}
function validate_flight_form ( )
{
	var d = new Date();
	var date = d.getDate();
	var mon = d.getMonth() + 1;
		var pass = document.flight_form.pass.value;
	pricing = true;
	var value = 0;
	valid = true;



//makes sure check boxes are checked
if ((document.flight_form.type[0].checked == false )
&& ( document.flight_form.type[1].checked == false ))
{
document.getElementById("error").innerHTML="You must select either a One-way or Return trip";	
valid = false;
}

//makes sure the destination is filled
else if ( document.flight_form.destination.value != "Paris" )
{

document.flight_form.destination.value = "Paris";
document.flight_form.departfrom.focus(); //as the field is filled for the user, the next field is selected for them
document.getElementById("error").innerHTML="You must input a destination...We're sorry, due to problems beyond our control only flights to Paris are currently availiable.";
valid = false;	
}
//makes sure the depart value is filled
else if (document.flight_form.departfrom.value != "York")
{
document.flight_form.departfrom.value = "York";
	
document.getElementById("error").innerHTML="You must input a departing airport...We're sorry, due to problems beyond our control only flights from York are availiable";
valid = false;
}
//makes sure depart dates are entered
else if ((document.flight_form.day.value == "") || (document.flight_form.month.value== "") || (document.flight_form.year.value == ""))
{
	document.getElementById("error").innerHTML="You must input a depart date; the day, month and year. ";
	document.flight_form.day.focus();
	valid = false;
}
//performs function IsNumeric on characters in the depart date boxes
else if ((!IsNumeric(document.flight_form.day.value))|| (!IsNumeric(document.flight_form.month.value)) || (!IsNumeric(document.flight_form.year.value)))
{
	document.getElementById("error").innerHTML="You must only input numbers in the date boxes such as 01, 02, 03..."
	valid = false;
}


//very messy way of ensuring the correct entries are made to the day field. Cannot use < 1 or > 31 because this assumes integers, which 
//Javascript automatically converts from 01, 02 etc.
else if ((document.flight_form.day.value != '01')&& (document.flight_form.day.value != '02') &&  (document.flight_form.day.value != '03') &&  (document.flight_form.day.value != '04') && (document.flight_form.day.value != '05') &&  (document.flight_form.day.value != '06') &&  (document.flight_form.day.value != '07') &&  (document.flight_form.day.value != '08') && (document.flight_form.day.value != '09') && (document.flight_form.day.value != '10')&&(document.flight_form.day.value != '10')&& (document.flight_form.day.value != '11') &&  (document.flight_form.day.value != '12') &&  (document.flight_form.day.value != '13') && (document.flight_form.day.value != '14') &&  (document.flight_form.day.value != '15') &&  (document.flight_form.day.value != '16') &&  (document.flight_form.day.value != '17') && (document.flight_form.day.value != '18') && (document.flight_form.day.value != '19')&&(document.flight_form.day.value != '20')&& (document.flight_form.day.value != '21') &&  (document.flight_form.day.value != '22') &&  (document.flight_form.day.value != '23') && (document.flight_form.day.value != '24') &&  (document.flight_form.day.value != '25') &&  (document.flight_form.day.value != '26') &&  (document.flight_form.day.value != '27') && (document.flight_form.day.value != '28') && (document.flight_form.day.value != '29')&& (document.flight_form.day.value != '30') && (document.flight_form.day.value != '31'))
{
document.getElementById("error").innerHTML="Day values must of the correct format: 01, 02, 03...and 31 or below";
valid = false;
}

else if ((document.flight_form.day.value < date) && (document.flight_form.month.value <= mon))
{
document.getElementById("error").innerHTML="Your depart date cannot be in the past!";
valid = false;	
}
//cannot have a day over 28 as Feburary does not have any days over 28, unless it's a leap year. 2011 is not a leap year and the form only allows year 2011 to book flights. 
else if ((document.flight_form.day.value > 28) &&(document.flight_form.month.value == '02'))
{
document.getElementById("error").innerHTML="Febuary has only 28 days this year, please input a day 28 or below";
valid = false;
}

//ensure that months April, June, September and November cannot be booked on the 31st, as these months only reach 30 days.
else if ((document.flight_form.day.value > 30) && ((document.flight_form.month.value == '04') || (document.flight_form.month.value == '06') || (document.flight_form.month.value == '09') || (document.flight_form.month.value == '11')))
{
document.getElementById("error").innerHTML="Problem with depart date: April, June, September and November only have 30 days";	
valid = false;

}

//very messy way of ensuring months are in correct format
else if ((document.flight_form.month.value != '01')&& (document.flight_form.month.value != '02') &&  (document.flight_form.month.value != '03') &&  (document.flight_form.month.value != '04') && (document.flight_form.month.value != '05') &&  (document.flight_form.month.value != '06') &&  (document.flight_form.month.value != '07') &&  (document.flight_form.month.value != '08') && (document.flight_form.month.value != '09') && (document.flight_form.month.value != '10') && (document.flight_form.month.value != '11')&& (document.flight_form.month.value != '12')) 
{
document.getElementById("error").innerHTML="Month values must of the correct format: 01, 02, 03...";
valid = false;
}
//flights are only availaible in 2011
else if (document.flight_form.year.value != 2011)
{
document.getElementById("error").innerHTML="Flights are only currently availiable for 2011, we have updated your input";
document.flight_form.year.value = 2011;	
valid = false;
}

//the following are the same but for Return dates, in the same order.
else if ((document.flight_form.dayr.value == "") || (document.flight_form.monthr.value== "") || (document.flight_form.yearr.value == ""))
{
	document.getElementById("error").innerHTML="You must input a return date; the day, month and year.";
	document.flight_form.dayr.focus();
	valid = false;
}
else if ((!IsNumeric(document.flight_form.dayr.value))|| (!IsNumeric(document.flight_form.monthr.value)) || (!IsNumeric(document.flight_form.yearr.value)))
{
	document.getElementById("error").innerHTML="You must only input numbers in the date boxes such as 01, 02, 03..."
	valid = false;
}


else if ((document.flight_form.dayr.value != '01')&& (document.flight_form.dayr.value != '02') &&  (document.flight_form.dayr.value != '03') &&  (document.flight_form.dayr.value != '04') && (document.flight_form.dayr.value != '05') &&  (document.flight_form.dayr.value != '06') &&  (document.flight_form.dayr.value != '07') &&  (document.flight_form.dayr.value != '08') && (document.flight_form.dayr.value != '09') && (document.flight_form.dayr.value != '10')&&(document.flight_form.dayr.value != '10')&& (document.flight_form.dayr.value != '11') &&  (document.flight_form.dayr.value != '12') &&  (document.flight_form.dayr.value != '13') && (document.flight_form.dayr.value != '14') &&  (document.flight_form.dayr.value != '15') &&  (document.flight_form.dayr.value != '16') &&  (document.flight_form.dayr.value != '17') && (document.flight_form.dayr.value != '18') && (document.flight_form.dayr.value != '19')&&(document.flight_form.dayr.value != '20')&& (document.flight_form.dayr.value != '21') &&  (document.flight_form.dayr.value != '22') &&  (document.flight_form.dayr.value != '23') && (document.flight_form.dayr.value != '24') &&  (document.flight_form.dayr.value != '25') &&  (document.flight_form.dayr.value != '26') &&  (document.flight_form.dayr.value != '27') && (document.flight_form.dayr.value != '28') && (document.flight_form.dayr.value != '29')&& (document.flight_form.dayr.value != '30') && (document.flight_form.dayr.value != '31'))
{
document.getElementById("error").innerHTML="Day values must of the correct format: 01, 02, 03...and 31 or below";
valid = false;
}
else if ((document.flight_form.dayr.value < date) && (document.flight_form.monthr.value <= mon))
{
document.getElementById("error").innerHTML="Your return date cannot be in the past!";
valid = false;	
}
else if ((document.flight_form.dayr.value > 28) &&(document.flight_form.monthr.value == '02'))
{
document.getElementById("error").innerHTML="Febuary has only 28 days this year, please input a day 28 or below";
valid = false;
}
//make sure the month values are in the correct format, and only these numbers are used. Var months used to reduce amount of code. 
else if ((document.flight_form.dayr.value >30) &&  ((document.flight_form.monthr.value ==  '04') || (document.flight_form.monthr.value == '06') || (document.flight_form.monthr.value == '09') || (document.flight_form.monthr.value == '11')))
{
document.getElementById("error").innerHTML="April, June, September and November only have 30 days";	
valid = false;
}

else if ((document.flight_form.monthr.value != '01')&& (document.flight_form.monthr.value != '02') &&  (document.flight_form.monthr.value != '03') &&  (document.flight_form.monthr.value != '04') && (document.flight_form.monthr.value != '05') &&  (document.flight_form.monthr.value != '06') &&  (document.flight_form.monthr.value != '07') &&  (document.flight_form.monthr.value != '08') && (document.flight_form.monthr.value != '09') &&  (document.flight_form.monthr.value != '10') &&  (document.flight_form.monthr.value != '11') &&  (document.flight_form.monthr.value != '12')) 
{
document.getElementById("error").innerHTML="Month values must be of the correct format, between 01 and 12";
valid = false;
}

else if (document.flight_form.yearr.value != 2011)
{
document.getElementById("error").innerHTML="Flights are only currently availiable for 2011, we have updated your input";
document.flight_form.yearr.value = 2011;	
valid = false;
}

else if (document.flight_form.pass.value == '')
{
document.getElementById("error").innerHTML="You must state how many passengers will be taking the flight";	
valid = false;
}

else if (document.flight_form.pass.value > 16)
{
	document.getElementById("error").innerHTML="You can have a maximum of 16 passengers";
	
	valid = false;
}


 
if ((document.flight_form.type[0].checked) && ((document.flight_form.month.value >= mon + 2) || ((mon == 01) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 03) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 05) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 07) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 08) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 10) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 12) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) ||((document.flight_form.day.value >= date - 1) && (document.flight_form.month.value == mon + 1)|| ((document.flight_form.day.value >= date + 29 ) && (document.flight_form.month.value == mon + 0))))))))))))
	
	{
	 value = 100;
	
	 pricing = false; 
	
	}
	//less than 29 days in advance (counting whole days)
else if ((document.flight_form.type[0].checked) &&   ((document.flight_form.day.value >= date - 14) && (document.flight_form.month.value == mon + 1 ) ||((document.flight_form.day.value >= date + 15) && (document.flight_form.day.value <= date + 28)&& (document.flight_form.month.value == mon + 0))))
	
{
	value = 120;
	
	pricing = false; 

}
//less than 14 days in advance
	else if ((document.flight_form.type[0].checked) && ((document.flight_form.day.value >= date + 8) && (document.flight_form.day.value <= date + 14) && (document.flight_form.month.value == mon + 0) || ((document.flight_form.day.value <= date - 15) && (document.flight_form.day.value >= date - 22) && (document.flight_form.month.value == mon + 1))))

{
		value = 130;
	
	pricing = false; 

//Less than 8 days in advance
}
	else if ((document.flight_form.type[0].checked) && ((document.flight_form.day.value <= date - 21) && (document.flight_form.month.value == mon + 1) || ((document.flight_form.day.value <= date + 7) && (document.flight_form.month.value == mon + 0 ))))
{
	value = 150;
	
	pricing = false; 
	
}
	//RETURN PRICING
	//adds in conditions of months which have 31 days where the calculation has to be different. 
	else if ((document.flight_form.type[1].checked) && ((document.flight_form.month.value >= mon + 2) || ((mon == 01) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 03) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 05) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 07) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 08) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 10) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) || ((mon == 12) && (document.flight_form.day.value == 08) && (document.flight_form.month.value == mon + 1) ||((document.flight_form.day.value >= date - 1) && (document.flight_form.month.value == mon + 1)|| ((document.flight_form.day.value >= date + 29 ) && (document.flight_form.month.value == mon + 0))))))))))))
	
	{
	 value = 150;
	
	 pricing = false; 
	
	}
	//less than 29 days in advance (counting whole days)
else if ((document.flight_form.type[1].checked) &&   ((document.flight_form.day.value >= date - 14) && (document.flight_form.month.value == mon + 1 ) ||((document.flight_form.day.value >= date + 15) && (document.flight_form.day.value <= date + 28)&& (document.flight_form.month.value == mon + 0))))
	
{
	value = 170;

	pricing = false; 

}
//less than 14 days in advance
	else if ((document.flight_form.type[1].checked) && ((document.flight_form.day.value >= date + 8) && (document.flight_form.day.value <= date + 14) && (document.flight_form.month.value == mon + 0) || ((document.flight_form.day.value <= date - 15) && (document.flight_form.day.value >= date - 22) && (document.flight_form.month.value == mon + 1))))

{
		value = 180;
	
	pricing = false; 

//Less than 8 days in advance
}
	else if ((document.flight_form.type[1].checked) && ((document.flight_form.day.value <= date - 21) && (document.flight_form.month.value == mon + 1) || ((document.flight_form.day.value <= date + 7) && (document.flight_form.month.value == mon + 0 ))))
{
	value = 200;
		
	pricing = false; 
	
}

 if (document.flight_form.pass.value >= 1)
{
    value = pass * value;
		
}
document.getElementById('my_value').value = value;
return valid;
return pricing;


}

Open in new window

0
 
minjosefaCommented:
It is not a button, but a hidden field. Don't replace, but add to your excisting HTML, between your <form>-tags:
<input type="hidden id="my_value" name="value" value="" />

Open in new window

0
 
minjosefaCommented:
Also see my first simpler/dirtier solution:

Javascript:
document.flight_form.action  = "price.php?value=" + value; 
return valid;
//return pricing; 

Open in new window


PHP:
$value = $_GET['value'];  

Open in new window

0
 
Lexx877Author Commented:
Right it works! It did before too but for some reason Chrome keeps loading cached pages and unless I hard refresh it won't load the new page. So I was getting the same errors because to the browser the same code was behind it. I'm using your not as simple version :)

Using that hidden field is actually a nice clever way of doing it, I had no idea you could have such a thing :) Always learning though so thanks for your persisting help.
0
 
minjosefaCommented:
I'm glad to have been of help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.