As you can see I am trying to create a simple form which outputs the price in the php part. But i get these errors
Notice: Undefined variable: result in /var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w12024193/public_html/test/example2.php on line 3
Notice: Undefined index: booking in /var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w12024193/public_html/test/example2.php on line 4
Warning: Division by zero in /var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w12024193/public_html/test/example2.php on line 4
Total cost is: Notice: Undefined variable: result in /var/www/vhosts/numyspace.co.uk/web_users/home/~unn_w12024193/public_html/test/example2.php on line 18
exculding VAT. Total cost is including VAT.
HTML
<form method="post" action="example2.php">
<select name="booking">
<option value="Double Room">Double Room £50</option>
<option value="Twin Room">Twin Room £70</option>
<option value="Family Room">Family Room £100</option>
</select>
<select name="service">
<option value="10">Yes £10</option>
<option value="0">No</option>
</select>
<input type="submit" value="submit" action="example2.php" name="submit"><br />
</form>
PHP
$vresult = $result * 20%
$booking = $_GET["booking"];
if ($booking == "Double Room"){
$result = 50 + $_GET["service"];
}
else if($booking == "Twin Room"){
$result = 50 + $_GET["service"];
}
else if($booking == "Family Room"){
$result = 100 + $_GET["service"];
}
?>
Total cost is <?php echo $result;?> exculding VAT.
Total cost is <?php echo $vresult; ?> including VAT.
First: vresult should be after you defined $result, and calculate 20% extra with * 1.2 instead of * 20%; Second: You need to use $_POST instead of $_GET
Third: $_POST['service'] can be altered, just check it before you output it
$booking = @$_POST["booking"];
$service = @$_POST["service"] == 10? 10 : 0;
if ($booking == "Double Room"){
$result = 50 + $service;
}
else if($booking == "Twin Room"){
$result = 50 + $service;
}
else if($booking == "Family Room"){
$result = 100 + $service;
}
$vresult = $result * 1.2;
?>
Total cost is <?php echo $result;?> exculding VAT.
Total cost is <?php echo $vresult; ?> including VAT.
$result
is not defined. I am referencing the first line of php: $vresult = $result * 20%
. If you are trying to set the $result
based on the booking type you need to move the noted line below the code that sets $result
Also your form method is POST
so you need to use $_POST['booking']
You are also missing a ;
after the first line of php
This is wrong:
$vresult = $result * 20%
The %
character is the modulus operator and as you are not using a second parameter nor a terminating ;
that leads to all kind of errors as php sees your line as:
$vresult = $result * 20 % $booking = $_GET["booking"];
^^^^^^^^^^^^^^^^ undefined (see below)
^^^^^^^^ NULL leads to division by zero
You probably want:
$vresult = $result * 0.2;
And your form method is POST
so your posted variables will be located in the $_POST
array.
Edit: The price including 20% would of course be 1.2 * $result
and you calculate that after you have calculated the price.
when you open the file for the first time there is no form submitted and these variables are not defined. you need to user isset() function before define them.
It looks to me like you need to move the line that computes the VAT
$vresult = $result * 20%
to AFTER you have actually computed the $result
- so just before you close the PHP script. And you need to write it as
$vresult = $result * 0.2;