Can't use form variables on action page!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikeb
Forum Commoner
Posts: 60
Joined: Tue Jul 08, 2003 4:37 pm
Location: Dublin, Ireland

Can't use form variables on action page!

Post 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.
Owe Blomqvist
Forum Commoner
Posts: 33
Joined: Wed Oct 16, 2002 2:27 pm

Post 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">
Last edited by Owe Blomqvist on Tue Jul 08, 2003 4:55 pm, edited 1 time in total.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

'

Post by phpScott »

try putting your $_POST[var] like $_POST['var'];
In other words put single quotes around your POST var;

phpScott
mikeb
Forum Commoner
Posts: 60
Joined: Tue Jul 08, 2003 4:37 pm
Location: Dublin, Ireland

Post 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>
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post 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.
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post 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.
mikeb
Forum Commoner
Posts: 60
Joined: Tue Jul 08, 2003 4:37 pm
Location: Dublin, Ireland

Post by mikeb »

Thanks guys. All appreciated. Red Hair? You must have roots in my own country (Ireland!). Cheers, Mike
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post 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.
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post 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?
Owe Blomqvist
Forum Commoner
Posts: 33
Joined: Wed Oct 16, 2002 2:27 pm

Post 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';
    }
?>
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post by redhair »

Yep...even better :wink:
Post Reply