Page 1 of 1

Problems regarding common / novice PHP.

Posted: Thu Mar 12, 2015 11:31 am
by Illeteratedane
Hi everyone.

So long story short, I've enrolled myself in an MsC programme for PHP and SQL, it's strictly on a voluntary basis as I am studying something else on a Bsc level. This makes it tricky for me to ask my lecturer for help, as I'd feel a bit guily if I was to steal time from the 'real' students.

So, for my first assignment, I am to make a BMI calculator, I've been going at it for about 3 hours now and I still cannot make it work.
I keep getting this error message when I attempt to test it "( ! ) Parse error: syntax error, unexpected end of file in C:\wamp\www\handle-form.php on line 16"


I've added the code for the initial form and the php script below.

handle-form.html

Code: Select all

<html>
<head>
<title>Calculate your BMI</title>
</head>
<body>

<form action="handle-form.php" method="post"

<p><input type="text" name="$weight" size "10"<br>
<p>Please enter your weight in kilos</p>
<p><input type="text" name="$height" size "10"<br>
<br><p>Please enter your height in m</p>

<p><input type="submit" name="submit" value="Submit"/></p>

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

handle-form.php

Code: Select all

<?php
if (isset($_POST['submit'])):
echo '';
$w = $_POST['weight'];
print_r("weight: $w<br/>");
$h = $_POST['height'];
print_r("$h height");

if(empty($c_temp)):
if(empty($c_temp)):

$b = (($w / $h) * $h);

print_r ("Your BMI is $b <br/>");
?>
A bit of advice would be greatly appreciated! My lecturer told me that I had to be persistant, but after tinkering for hours I feel a bit demotivated, and I've also managed to chug down 8 cups of coffee.

Re: Problems regarding common / novice PHP.

Posted: Thu Mar 12, 2015 11:36 am
by Celauran
You have no endif; to close your if (): block. You've also got a couple of empty if(): blocks (also missing endif) that look like they don't belong.

Re: Problems regarding common / novice PHP.

Posted: Thu Mar 12, 2015 11:38 am
by Celauran
What I mean:

Code: Select all

<?php

if (isset($_POST['submit'])) {
	echo '';
	$w = $_POST['weight'];
	print_r("weight: $w<br/>");
	$h = $_POST['height'];
	print_r("$h height");

	$b = (($w / $h) * $h);

	print_r ("Your BMI is $b <br/>");
}

?>

Re: Problems regarding common / novice PHP.

Posted: Thu Mar 12, 2015 12:02 pm
by Illeteratedane
Celauran wrote:You have no endif; to close your if (): block. You've also got a couple of empty if(): blocks (also missing endif) that look like they don't belong.

I tried fixing it only to recieve these new errors.

! ) Notice: Undefined index: weight in C:\wamp\www\handle-form.php on line 5
Call Stack
# Time Memory Function Location
1 0.0010 242016 {main}( ) ..\handle-form.php:0
weight:

( ! ) Notice: Undefined index: height in C:\wamp\www\handle-form.php on line 7
Call Stack
# Time Memory Function Location
1 0.0010 242016 {main}( ) ..\handle-form.php:0
height
( ! ) Warning: Division by zero in C:\wamp\www\handle-form.php on line 10
Call Stack
# Time Memory Function Location
1 0.0010 242016 {main}( ) ..\handle-form.php:0
Your BMI is 0


I was able to make a Celcius to Fahrenheit converter yesterday without any major problems, but it would seem that working with two input variables is causing me some trouble :banghead:

Re: Problems regarding common / novice PHP.

Posted: Thu Mar 12, 2015 12:25 pm
by Celauran
Remove the $ in your form field names. This also illustrates the importance of checking if something is set before trying to use it.