Jump to content

[SOLVED] $_POST question


ToddAtWSU

Recommended Posts

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!

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.