I'm learning PHP with the book PHP Fast&Easy Web Development 2nd Edition by Julie C Meloni
I'm learning how to handle the variables in $_POST in a simple calculation form (below)
My problem is I get this message when a value is blank:
Notice: Use of undefined constant val1 - assumed 'val1' in D:\websites\development\webdev\calculate.php on line 3
Is this a problem with my PHP setup that can be fixed in the php.ini file or is this a problem with the code (which is straight from the book):
The Form:
<form method="post" action="calculate.php">
<br>Value 1: <input type="text" name="val1" size="10">
<br>Value 2: <input type="text" name="val2" size="10">
<br>Calculation:<br>
<input type="radio" name="calc" value="add">Add<br>
<input type="radio" name="calc" value="subtract">Subtract<br>
<input type="radio" name="calc" value="multiply">Multiply<br>
<input type="radio" name="calc" value="divide">Divide<br>
<br><input type="submit" value="Calculate">
and the PHP Script:
<?
if(($_POST[val1]=="") || ($_POST[val2]=="") || ($_POST[calc]=="")){
header("Location: <!-- m --><a class="postlink" href="http://webdev">http://webdev</a><!-- m -->");
exit;
}
if($_POST[calc]=="add"){
$result=$_POST[val1] + $_POST[val2];
}
else if($_POST[calc]=="subtract"){
$result=$_POST[val1] - $_POST[val2];
}
else if($_POST[calc]=="multiply"){
$result=$_POST[val1] * $_POST[val2];
}
else if($_POST[calc]=="divide"){
$result=$_POST[val1] / $_POST[val2];
}
?>
<html>
<head>
<title>Calculation</title>
</head>
<body>
<br><br>The result of the calculation is <? echo"$result"; ?>
</body>
</html>
Having some coding experience, I can interpret it to mean that it doesn't understand the value of val1 (since it is "") but why does it know the value of val1 if the form field has something in it?what this is saying is that you have not set the variable var1, so it just "assumes" that the variable is var1, does the code work?Shouldn't the variable be set to "" by the post since the field is blank?
The Code isn't supposed to work- this is supposed to catch a blank value so you can go back to your user and tell them to put something in the blank field.
But later in the book when the code does error checking to give feedback to the user, the same thing is happening so it stops the code.
??? I'm still confusedYou need to escape your header() line. Also, you need to put the names in your $POST array in inverted commas:
f(($_POST["val1"]=="") || ($_POST["val2"]=="") || ($_POST["calc"]=="")){
header("Location: <a href=http://www.htmlforums.com/archive/index.php/\"http://webdev\" target=\"_blank\"></a>");Wiffles:
The header was messed up by the BB system-
Is is actually:
header("Location: <!-- m --><a class="postlink" href="http://webdev">http://webdev</a><!-- m -->");
which is straight from the book (except, I put in the actual address)
Any other ideas? I'm stumped!Hey souldrifter2, I've edited my post. Replace the first two lines with those and it should work.Wiffles- that helped though I thought I tried it before posting to the forum.
That line now reads:
if(($_POST['val1']=="") || ($_POST['val2']=="") || ($_POST['calc']=="")){
If there is no selection for calc it is still not getting a value- I think because it is radio group instead of a textfield- does that make sense?cals should still be empty if no radio button is selected. Can you post the PHP you have now?
I'm learning how to handle the variables in $_POST in a simple calculation form (below)
My problem is I get this message when a value is blank:
Notice: Use of undefined constant val1 - assumed 'val1' in D:\websites\development\webdev\calculate.php on line 3
Is this a problem with my PHP setup that can be fixed in the php.ini file or is this a problem with the code (which is straight from the book):
The Form:
<form method="post" action="calculate.php">
<br>Value 1: <input type="text" name="val1" size="10">
<br>Value 2: <input type="text" name="val2" size="10">
<br>Calculation:<br>
<input type="radio" name="calc" value="add">Add<br>
<input type="radio" name="calc" value="subtract">Subtract<br>
<input type="radio" name="calc" value="multiply">Multiply<br>
<input type="radio" name="calc" value="divide">Divide<br>
<br><input type="submit" value="Calculate">
and the PHP Script:
<?
if(($_POST[val1]=="") || ($_POST[val2]=="") || ($_POST[calc]=="")){
header("Location: <!-- m --><a class="postlink" href="http://webdev">http://webdev</a><!-- m -->");
exit;
}
if($_POST[calc]=="add"){
$result=$_POST[val1] + $_POST[val2];
}
else if($_POST[calc]=="subtract"){
$result=$_POST[val1] - $_POST[val2];
}
else if($_POST[calc]=="multiply"){
$result=$_POST[val1] * $_POST[val2];
}
else if($_POST[calc]=="divide"){
$result=$_POST[val1] / $_POST[val2];
}
?>
<html>
<head>
<title>Calculation</title>
</head>
<body>
<br><br>The result of the calculation is <? echo"$result"; ?>
</body>
</html>
Having some coding experience, I can interpret it to mean that it doesn't understand the value of val1 (since it is "") but why does it know the value of val1 if the form field has something in it?what this is saying is that you have not set the variable var1, so it just "assumes" that the variable is var1, does the code work?Shouldn't the variable be set to "" by the post since the field is blank?
The Code isn't supposed to work- this is supposed to catch a blank value so you can go back to your user and tell them to put something in the blank field.
But later in the book when the code does error checking to give feedback to the user, the same thing is happening so it stops the code.
??? I'm still confusedYou need to escape your header() line. Also, you need to put the names in your $POST array in inverted commas:
f(($_POST["val1"]=="") || ($_POST["val2"]=="") || ($_POST["calc"]=="")){
header("Location: <a href=http://www.htmlforums.com/archive/index.php/\"http://webdev\" target=\"_blank\"></a>");Wiffles:
The header was messed up by the BB system-
Is is actually:
header("Location: <!-- m --><a class="postlink" href="http://webdev">http://webdev</a><!-- m -->");
which is straight from the book (except, I put in the actual address)
Any other ideas? I'm stumped!Hey souldrifter2, I've edited my post. Replace the first two lines with those and it should work.Wiffles- that helped though I thought I tried it before posting to the forum.
That line now reads:
if(($_POST['val1']=="") || ($_POST['val2']=="") || ($_POST['calc']=="")){
If there is no selection for calc it is still not getting a value- I think because it is radio group instead of a textfield- does that make sense?cals should still be empty if no radio button is selected. Can you post the PHP you have now?