Page 1 of 1

Can't use form variables on action page!

Posted: Tue Jul 08, 2003 4:37 pm
by mikeb
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.

Posted: Tue Jul 08, 2003 4:43 pm
by Owe Blomqvist
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.

Code: Select all

<FORM ACTION="action.php">

'

Posted: Tue Jul 08, 2003 4:44 pm
by phpScott
try putting your $_POST[var] like $_POST['var'];
In other words put single quotes around your POST var;

phpScott

Posted: Tue Jul 08, 2003 4:53 pm
by mikeb
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>

Posted: Tue Jul 08, 2003 5:01 pm
by Galahad
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:

Code: Select all

<?php
print_r($_POST);
exit();
?>
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.

Posted: Tue Jul 08, 2003 5:20 pm
by redhair
Maybe using 'isset' will help.
example:

Code: Select all

<?php
if (isset($_POST['val1']))  {$learn_something} else { header("Location:http://localhost/webroot/sandbox/phpLearning/calculate_form.htm");}
?>
Note: Using exit; is not needed, since you are beeing redirected anyway after the header command.

Posted: Tue Jul 08, 2003 5:37 pm
by mikeb
Thanks guys. All appreciated. Red Hair? You must have roots in my own country (Ireland!). Cheers, Mike

Posted: Tue Jul 08, 2003 5:44 pm
by Galahad
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.

Posted: Tue Jul 08, 2003 6:32 pm
by redhair
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.
True! But you don't putt it directly after a header()...it has no use in this (or his) example.
Galahad 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.
No you are not wrong,...i just didnt reveal the right code straight away :oops:
here we go again:

Code: Select all

<?php
if ((isset($_POST['val1'])) && $_POST['val1']=="")
{
print "I can take it ";
} 
?>
Is this better?

Posted: Tue Jul 08, 2003 6:52 pm
by Owe Blomqvist
How about:

Code: Select all

<?php
if( empty( $_POST["val1"] ) or empty( $_POST["val2"] ) or empty( $_POST["calc"] ) )
    {
        echo 'Do your header stuff here';
    }
?>

Posted: Wed Jul 09, 2003 3:10 am
by redhair
Yep...even better :wink: