play_ Posted March 3, 2006 Share Posted March 3, 2006 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><?phpif(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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 3, 2006 Share Posted March 3, 2006 You pass it via a hidden field:[code]<?phpif(!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 Link to comment Share on other sites More sharing options...
Ssanutokh Posted March 3, 2006 Share Posted March 3, 2006 [!--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]<?phpif(!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 Quote Link to comment Share on other sites More sharing options...
play_ Posted March 3, 2006 Author Share Posted March 3, 2006 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 Link to comment Share on other sites More sharing options...
Ssanutokh Posted March 3, 2006 Share Posted March 3, 2006 [!--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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 3, 2006 Share Posted March 3, 2006 Play...Please post the code that didn't work and explain why it didn't work.Ken Quote Link to comment Share on other sites More sharing options...
play_ Posted March 3, 2006 Author Share Posted March 3, 2006 Hello Ken.I used the code you gave me:[code]<?phpif(!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 2echo $sq;?>[/code]error:Notice: Undefined index: variable in c:\program files\web\easy php\www\s.php on line 2I have tried making it $_POST, but didn't work Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 3, 2006 Share Posted March 3, 2006 I forgot one minor thing... the name of the field.... without that nothing gets passed.... :-(Here's the fixed code:[code]<?phpif(!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 Quote Link to comment Share on other sites More sharing options...
play_ Posted March 3, 2006 Author Share Posted March 3, 2006 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 Quote Link to comment Share on other sites More sharing options...
Guest edwinsweep Posted March 3, 2006 Share Posted March 3, 2006 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 Link to comment Share on other sites More sharing options...
play_ Posted March 3, 2006 Author Share Posted March 3, 2006 [!--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] Quote Link to comment Share on other sites More sharing options...
play_ Posted March 4, 2006 Author Share Posted March 4, 2006 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\" /] Quote Link to comment Share on other sites More sharing options...
play_ Posted March 6, 2006 Author Share Posted March 6, 2006 ...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? Quote Link to comment Share on other sites More sharing options...
txmedic03 Posted March 7, 2006 Share Posted March 7, 2006 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 arrayif ( 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.