Can't use form variables on action page!
Moderator: General Moderators
Can't use form variables on action page!
Hi,
CF programmer new to php (don't tell anyone on the CF forums!). I'm using a php tutorial and have fallen at the first hurdle. I'm posting a form to an action page and the variables do not seem to be coming over from the form page. I have read the stickies, and global variables are set to 'on' so I can't figure the problem. I am well versed in html and for this question you can assume that my form is cool. Here is the receiving (action) page
<?
if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] == "")) {
header("Location:http://localhost/webroot/sandbox/phpLea ... e_form.htm");
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 Result</TITLE>
</HEAD>
<BODY>
<P>The results of the calculation is: <? echo "$result"; ?></p>
</BODY>
</HTML>
Any help. Symptoms are: page refreshes back to form even with all data filled in.
CF programmer new to php (don't tell anyone on the CF forums!). I'm using a php tutorial and have fallen at the first hurdle. I'm posting a form to an action page and the variables do not seem to be coming over from the form page. I have read the stickies, and global variables are set to 'on' so I can't figure the problem. I am well versed in html and for this question you can assume that my form is cool. Here is the receiving (action) page
<?
if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] == "")) {
header("Location:http://localhost/webroot/sandbox/phpLea ... e_form.htm");
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 Result</TITLE>
</HEAD>
<BODY>
<P>The results of the calculation is: <? echo "$result"; ?></p>
</BODY>
</HTML>
Any help. Symptoms are: page refreshes back to form even with all data filled in.
-
Owe Blomqvist
- Forum Commoner
- Posts: 33
- Joined: Wed Oct 16, 2002 2:27 pm
Does it reload the page after you hit the submit button?
If so, look at the form tag.
Action should hold the name of the page which process the form.
If so, look at the form tag.
Action should hold the name of the page which process the form.
Code: Select all
<FORM ACTION="action.php">
Last edited by Owe Blomqvist on Tue Jul 08, 2003 4:55 pm, edited 1 time in total.
Tried putting single quotes around the vars e.g. ['val1'] etc. Still bouncing me back to the form page. Here's the form code just in case I've done something wrong there:
<form method="post" action="calculate.php">
<p>Your name: <input type="text" name="firstName" size="10"></p>
<p>Value 1: <input type="text" name="val1" size="10"></p>
<p>Value 2: <input type="text" name="val2" size="10"></p>
<p>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="divide">divide<br>
<input type="radio" name="calc" value="multiply">multiply
</p>
<input type="submit" name="submit" value="Calculate">
</form>
<form method="post" action="calculate.php">
<p>Your name: <input type="text" name="firstName" size="10"></p>
<p>Value 1: <input type="text" name="val1" size="10"></p>
<p>Value 2: <input type="text" name="val2" size="10"></p>
<p>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="divide">divide<br>
<input type="radio" name="calc" value="multiply">multiply
</p>
<input type="submit" name="submit" value="Calculate">
</form>
I always find it helpful to use print_r to make sure my variables are getting sent ok. That will at least narrow it down. print_r prints a variable in "readable" format, so for arrays (such as $_POST), it will print out all of the contents of the array. Try something like:
If you put that right at the top of your script, it'll show you what get's posted, then stops the script before it redirects your browser. Hope that helps.
Code: Select all
<?php
print_r($_POST);
exit();
?>- redhair
- Forum Contributor
- Posts: 300
- Joined: Fri May 30, 2003 4:36 pm
- Location: 53.23N-6.57E
- Contact:
Maybe using 'isset' will help.
example:
Note: Using exit; is not needed, since you are beeing redirected anyway after the header command.
example:
Code: Select all
<?php
if (isset($_POST['val1'])) {$learn_something} else { header("Location:http://localhost/webroot/sandbox/phpLearning/calculate_form.htm");}
?>redhair, the exit() call is pretty handy if you want to see what print_r outputs before you are redirected to the new page. If you don't exit (or comment out the redirect), it's most likely that you will not have time to see the result of the print_r.
Also, I believe that calling isset on a form variable will always return true, because the value is set when the form is submitted, even if the form field is blank (then it's set to ""). That's what I remember anyway, I could be wrong.
Also, I believe that calling isset on a form variable will always return true, because the value is set when the form is submitted, even if the form field is blank (then it's set to ""). That's what I remember anyway, I could be wrong.
- redhair
- Forum Contributor
- Posts: 300
- Joined: Fri May 30, 2003 4:36 pm
- Location: 53.23N-6.57E
- Contact:
True! But you don't putt it directly after a header()...it has no use in this (or his) example.Galahad wrote:redhair, the exit() call is pretty handy if you want to see what print_r outputs before you are redirected to the new page.
No you are not wrong,...i just didnt reveal the right code straight awayGalahad wrote: Also, I believe that calling isset on a form variable will always return true, because the value is set when the form is submitted, even if the form field is blank (then it's set to ""). That's what I remember anyway, I could be wrong.
here we go again:
Code: Select all
<?php
if ((isset($_POST['val1'])) && $_POST['val1']=="")
{
print "I can take it ";
}
?>-
Owe Blomqvist
- Forum Commoner
- Posts: 33
- Joined: Wed Oct 16, 2002 2:27 pm
How about:
Code: Select all
<?php
if( empty( $_POST["val1"] ) or empty( $_POST["val2"] ) or empty( $_POST["calc"] ) )
{
echo 'Do your header stuff here';
}
?>