ToddAtWSU Posted March 4, 2007 Share Posted March 4, 2007 If I want to pass a value I read from a file using firstPhpFile.php to something like secondPhpFile.php, how do I store this value so I can use the $_POST variable to retrieve it later? I know how to retrieve values from my HTML code using $_POST by using the HTML element's name but how do I do this if the variable is a PHP variable holding a value I need to pass to a different PHP file? Thanks! Quote Link to comment Share on other sites More sharing options...
papaface Posted March 4, 2007 Share Posted March 4, 2007 You can either use hidden input fields and store it in there. Or use something better such a sessions. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 4, 2007 Share Posted March 4, 2007 Well, you could either pass it through the URL, or through a form. $_POST way <?php $send_var = 387593; print<<<HERE <form action = "secondPhpFile.php" method="POST"> <input type = "hidden" name = "pass_this" value = "$send_var"> <input type = "submit" name = "submit" value = "Go to next page"> </form> HERE; ?> To get the information on your second page just type: $info = $_POST['pass_this']; ---------------------------------- $_GET way <?php $send_var = 387593; print<<<HERE <form action = "secondPhpFile.php?info=$send_var" method="POST"> <input type = "submit" name = "submit" value = "Go to next page"> </form> HERE; ?> On your second page you can retrieve the information from the first by typing: $info = $_GET['info']; Quote Link to comment Share on other sites More sharing options...
ToddAtWSU Posted March 4, 2007 Author Share Posted March 4, 2007 Thanks I like the sound of the hidden field. I am new to PHP and HTML and I didn't think about the hidden fields. Thanks! Quote Link to comment Share on other sites More sharing options...
ToddAtWSU Posted March 4, 2007 Author Share Posted March 4, 2007 How do you access a php variable inside the <input> tag? My form code is not inside a <?php> tag. Is there something I need to put before the <form> tag if I put it inside a <?php> tag? Do I need to echo all my HTML tags if I put it all inside a <?php> tag? Thanks! Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 4, 2007 Share Posted March 4, 2007 If you put your HTML inside of your PHP tags you will need to echo it. And you can't have PHP without the PHP tags, so you would have to open it then close it again. <form action = "somewhere.php?id=<? echo $var; ?>" method="POST"> That should work if you don't have the starting PHP tags. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted March 4, 2007 Share Posted March 4, 2007 How do you access a php variable inside the <input> tag? My form code is not inside a <?php> tag. Is there something I need to put before the <form> tag if I put it inside a <?php> tag? Do I need to echo all my HTML tags if I put it all inside a <?php> tag? Thanks! You might find it easier having the bulk of your code at the start of the file and the HTML at the end, something like this: <?php $username=$_POST['strname']; if ($_POST['subsend']) { if ($username=="fred") {$msg="Hello $username";} else {$msg="I don't know you $username";} } else {$msg="Please enter your name:";} ?> <html> <head> <title>Just a test</title> </head> <body> <?php echo $msg; ?><br /> <form action="" method="post"> Username: <input type="text" name="strname" /><br /> <input type="submit" name="subsend" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
ToddAtWSU Posted March 4, 2007 Author Share Posted March 4, 2007 I did this but it does not seem to be working. <input type="hidden" name="numGames" value="<?php global $totalGames; echo $totalGames; ?>"> Can you see what is wrong here? $totalGames is define in another <?php ?> set of tags. Thanks. Edit: Nevermind, I am an idiot and was forgetting to use the $ so I was seeing the variable name instead of the value it held. Thanks! 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.