Jump to content


Photo

Form not passing variable


  • Please log in to reply
13 replies to this topic

#1 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 03 March 2006 - 01:26 AM

Ok. It's been a month almost since i've done php.
Anyways, i am trying to pass a variable from a page to another, without using a session (using the $_POST global var)

I know how to pass values FROM a form, that the user entered in a field. But i wanna pass a value that is not user entered.


i have this:
<form action="s.php" method="POST">
<input type="submit" name="submit" />
</form>

<?php
if(isset($_POST['submit'])) {
    $variable = "testing";
}

?>

which processes to this page:
<?php
$sq = $_POST['variable'];
echo $sq;
?>


and of course i've tried some other things but it won't work.
is it possible at all? (without Sessions?)

Ive been thinking that it is not possible because $variable is not a $_POST, and for a variable to be $_POST, it has to be user input. but im not sure.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 March 2006 - 01:48 AM

You pass it via a hidden field:
<?php
if(!isset($_POST['submit']))  $variable = "testing";
?>
<form action="s.php" method="POST">
<input type="hidden" value="<?php echo $variable ?>" />
<input type="submit" name="submit" />
</form>

Ken

#3 Ssanutokh

Ssanutokh
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 March 2006 - 02:20 AM

[!--quoteo(post=351205:date=Mar 2 2006, 06:48 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 2 2006, 06:48 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You pass it via a hidden field:
<?php
if(!isset($_POST['submit']))  $variable = "testing";
?>
<form action="s.php" method="POST">
<input type="hidden" value="<?php echo $variable ?>" />
<input type="submit" name="submit" />
</form>

Ken
[/quote]

Can you elaborate on this? I've been pondering the exact same issue - I need to populate a PNG image with variables taken from prior forms in the script and I'm banging my head against the wall in frustration trying to figure out how to get them there without screwing up the content header.

-Carl

#4 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 03 March 2006 - 02:53 AM

Thank you Ken.


And Carl,
If you need variables from prior Forms, as in, user inputed variables, you can use $_POST, and $_GET.

not sure if that is what youre really asking or not, but worth a shot lol.

ps: ken, didn't work :(
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#5 Ssanutokh

Ssanutokh
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 March 2006 - 02:59 AM

[!--quoteo(post=351221:date=Mar 2 2006, 07:53 PM:name=play_)--][div class=\'quotetop\']QUOTE(play_ @ Mar 2 2006, 07:53 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Thank you Ken.
And Carl,
If you need variables from prior Forms, as in, user inputed variables, you can use $_POST, and $_GET.

not sure if that is what youre really asking or not, but worth a shot lol.

ps: ken, didn't work :(
[/quote]

I need to be able to use imagestring() to output both user-input variables and declared variables.

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 March 2006 - 03:25 AM

Play...

Please post the code that didn't work and explain why it didn't work.

Ken

#7 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 03 March 2006 - 04:06 AM

Hello Ken.
I used the code you gave me:
<?php
if(!isset($_POST['submit']))  $variable = "testing";
?>
<form action="s.php" method="POST">
<input type="hidden" value="<?php echo $variable ?>" />
<input type="submit" name="submit" />
</form>



and this page, which receives the form:

<?php
$sq = $_POST['variable'];  <---- line 2
echo $sq;
?>



error:
Notice: Undefined index: variable in c:\program files\web\easy php\www\s.php on line 2




I have tried making it $_POST, but didn't work
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 March 2006 - 05:10 AM

I forgot one minor thing... the name of the field.... without that nothing gets passed.... :-(

Here's the fixed code:
<?php
if(!isset($_POST['submit']))  $variable = "testing";
?>
<form action="s.php" method="POST">
<input type="hidden" name="variable" value="<?php echo $variable ?>" />
<input type="submit" name="submit" />
</form>

Now it should work.

Ken

#9 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 03 March 2006 - 02:24 PM

I did try adding a name also last night, but didn't work.
I didn't name it "variable" though.


Ill give it a shot when i get home today.
Thanks ken
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#10 Guest_edwinsweep_*

Guest_edwinsweep_*
  • Guests

Posted 03 March 2006 - 02:31 PM

i have been using the <form> manners a lot for passing data, its just that the only way i know to send it is to put a button in the browser that they have to click that will perform the submit. is there a manner to do this automaticly so i can just give normal hyperlinks instead of a <form> SUBMIT button all the time???
thanks in advance!

#11 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 03 March 2006 - 02:35 PM

[!--quoteo(post=351337:date=Mar 3 2006, 09:31 AM:name=edwinsweep)--][div class=\'quotetop\']QUOTE(edwinsweep @ Mar 3 2006, 09:31 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
i have been using the <form> manners a lot for passing data, its just that the only way i know to send it is to put a button in the browser that they have to click that will perform the submit. is there a manner to do this automaticly so i can just give normal hyperlinks instead of a <form> SUBMIT button all the time???
thanks in advance!
[/quote]


I once asked this same question.
It's in a thread somewhere....

Here edwin,
[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=82929&hl=\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?...topic=82929&hl=[/a]
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#12 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 04 March 2006 - 04:39 PM

ok id like to infrom that ive been trying to post a reply for the past 10 minutes, but i get this error:
[img src=\"http://img.photobucket.com/albums/v425/play/help/f38bb280.jpg\" border=\"0\" alt=\"IPB Image\" /]



regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#13 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 06 March 2006 - 02:19 AM

...im trying to ask for help but i can't because whenever i post my code, i get that error.


I think all my quotes or a variable name is interfering with the forum code?
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#14 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 07 March 2006 - 10:20 AM

You could do something like this.

<?php

// check to see if the user reached the page by 'POST' method.
if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
print_r($_POST); // print the contents of the $_POST array
}

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" id="field1" name="field1" />
<input type="hidden" id="field2" name="field2" value="this is a hidden field." />
<input type="submit" value="Send >>" />
</form>

This will submit the values to itself, but the same can be applied by creating a page to process the values separate from the form that sent them. Also note that you may pass values to the $_GET array at the same time you pass variables to the $_POST array by using <form action="<?php echo $_SERVER['PHP_SELF']; ?>?field3=field3"> then in your PHP you would need something like:

<?php

// Check to see if $_GET is an established array
if ( is_array($_GET) ) {
print_r($_GET); // print the contents of the $_GET array
}

?>

You would only need to check to make sure $_GET is actually an array to keep from encurring errors with print_r(). I only use print_r() for simplicity sake and your error handling may vary depending on exactly what you wish to do with your data once you get it from the form.

Happy coding!

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users