Jump to content

Recommended Posts

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:
[code]
<form action="s.php" method="POST">
<input type="submit" name="submit" />
</form>

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

?>
[/code]

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


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.
Link to comment
https://forums.phpfreaks.com/topic/3970-form-not-passing-variable/
Share on other sites

You pass it via a hidden field:
[code]<?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>
[/code]

Ken
[!--quoteo(post=351205:date=Mar 2 2006, 06:48 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 2 2006, 06:48 PM) [snapback]351205[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You pass it via a hidden field:
[code]<?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>
[/code]

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
[!--quoteo(post=351221:date=Mar 2 2006, 07:53 PM:name=play_)--][div class=\'quotetop\']QUOTE(play_ @ Mar 2 2006, 07:53 PM) [snapback]351221[/snapback][/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.
Hello Ken.
I used the code you gave me:
[code]
<?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>
[/code]



and this page, which receives the form:

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



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
I forgot one minor thing... the name of the field.... without that nothing gets passed.... :-(

Here's the fixed code:
[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>[/code]

Now it should work.

Ken
Guest edwinsweep
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!
[!--quoteo(post=351337:date=Mar 3 2006, 09:31 AM:name=edwinsweep)--][div class=\'quotetop\']QUOTE(edwinsweep @ Mar 3 2006, 09:31 AM) [snapback]351337[/snapback][/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]
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!
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.