Extracting variables from form

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
JeffElkins
Forum Newbie
Posts: 7
Joined: Tue Jun 24, 2003 12:50 pm

Extracting variables from form

Post by JeffElkins »

Code: Select all

echo "<b>Calculations</b>";
echo "<form method="post">";
echo "<br>Variable One: <input type="text" name="var1" size="5" maxlength="5">";
echo "<br>Variable Two: <input type="text" name="var2" size="5" maxlength="5">";

echo "<br><br><input type="submit" name="tst"
value="calc">";

echo "</form>";

echo $var1;
echo $var2;
I'm scratching my head here...

I did try:

echo $_POST['variable'];

w/o success.

My eventual goal is some calculations on imputted variables, but the two variables never show up. Obviously a newbie, thanks for any help....
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

Hi,

This works fine for me.. are you sure you are trying to use this on a php enabled server?

it could be a bug in your i.e. or something as you have not set your action value in your form.

try this:

Code: Select all

echo "<b>Calculations</b>"; 
echo "<form method="post" action="whateveryourscriptiscalled.php">"; 
echo "<br>Variable One: <input type="text" name="var1" size="5" maxlength="5">"; 
echo "<br>Variable Two: <input type="text" name="var2" size="5" maxlength="5">"; 

echo "<br><br><input type="submit" name="tst" value="calc">"; 

echo "</form>"; 

echo $var1; 
echo $var2;
Last edited by m@ndio on Tue Jun 24, 2003 1:30 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.

not going to work

Post by phpScott »

as you have your code now $var1 and $var2 will never have values because they are not assigned anything at the moment.

the only way they will have a value is when you submit your form then they will have the value the is stored in there respective html elements.

find some nice simple form submissin tutorials or code snippet and it will help you alot.

phpScott
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Code: Select all

<?php
echo "<b>Calculations</b>";
echo "<form method="post" action="$PHP_SELF" METHOD=post enctype="multipart/form-data">";
echo "<br>Variable One: <input type="text" name="var1" size="5" maxlength="5">";
echo "<br>Variable Two: <input type="text" name="var2" size="5" maxlength="5">";
echo "<input type="hidden" name="action" value="showvars">";
echo "<br><br><input type="submit" name="tst" value="calc">";

echo "</form>";

if ($action == 'showvars')
{
echo $var1;
echo $var2;
}
?>
I tested the above code and it works for me.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

It could be you have register globals off (which is good).

Code: Select all

<?php

// did you echo these or $_POST['variable']?
echo $_POST['var1'];
echo $_POST['var2'];

// general debugging of $_POST (and other) arrays:
echo '<pre>';
print_r($_POST);
echo '</pre>';

?>
Last edited by McGruff on Thu Aug 11, 2005 4:18 am, edited 1 time in total.
JeffElkins
Forum Newbie
Posts: 7
Joined: Tue Jun 24, 2003 12:50 pm

Post by JeffElkins »

m@ndio wrote:it could be a bug in your i.e. or something as you have not set your action value in your form.
That was the problem.

Thanks for the replies!
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

thought so I had it once :D
Post Reply