I have a from that has NEW a hidden variable. Lets say the variable from previous submission was 2.
<?php echo $variable = 3;
echo $variable; //displays 3
?>
<form action="functions/store.php" method="post">
<input type="hidden" name="variable" value="<?php echo $variable ;?>">
<input type="submit" value="Submit" />
When running the code to store it in the data base, it stores the variable form the previus submission: 2. Why is that. The code I in store.php run is:
$variable= $_POST['variable'];
echo $variable; //displays 2. Should display 3
$stmt = $dbh->prepare("INSERT BlahBlahBlah... //other code
I have no idea what's going on even unset($variable) before $_POST does not work.
It worked fine for a while then, it started to behave like this. Even tried on other browser - same result
Related
Is it possible to pass an Object through a Hidden Field in an HTML Form using $_POST and retrieve that Object on the page that the form links to?
On the first page, I have a form like the one below:
<?php
session_start();
require_once '../Model/player.php'; // To Enable Creation of a New Player Object
$playerName = filter_input(INPUT_POST, 'playerName');
$playerNumber = 1;
$player = new player($playerName, $playerNumber);
if (isset($player))
{
echo '<p>Successfully created your player!</p><br>';
?>
<form class="viewStats" action="../View/displayPlayerStatsView.php" method="post">
<input type="hidden" name="playerObject" value="<?php echo $player; ?>">
<input type="submit" value="View Your Player's Stats">
</form>
<?php
}
?>
And on the second (receiving) page, I have code like the code below:
session_start();
require_once '../Model/player.php'; // To Use Player Object
$player = filter_input(INPUT_POST, 'playerObject'); // ERROR: Thinks the Player Object is a string.
My error seems to be that the receiving page that retrieves the 'playerObject' from the $_POST array is acting like the Object is a string.
Can anyone give me guidance on how to pass an Object from one page to another using the $_POST array? Is this even possible?
Thank you in advance.
UPDATE:
Based on suggestions to serialize the Object, I now am getting the following errors:
If I change my code on the first (sending) page to:
$playerSerial = serialize((object) $player);
<form class="viewStats" action="../View/displayPlayerStatsView.php" method="post">
<input type="hidden" name="playerObject" value="<?php echo $playerSerial; ?>">
<input type="submit" value="View Your Player's Stats">
</form>
and change the code on the second (receiving) page to:
$playerSerial = filter_input(INPUT_POST, 'playerObject');
print_r($playerSerial);
$player = unserialize($playerSerial);
then the output I get from print_r($playerSerial); is O:6:, which I know is incorrect since the object has properties holding a player's name, number, health, strength, etc.
The require_once '../Model/player.php'; code exists in both PHP files, and it comes right at the top of both before any other code is executed.
You have to make a few additions and corrections:
<?php
//... your previous code
$player = serialize($player);
?>
<form class="viewStats" action="../View/displayPlayerStatsView.php" method="post">
<input type="hidden" name="playerObject" value="<?php echo $player; ?>">
<input type="submit" value="View Your Player's Stats">
</form>
Use the serialize() function to create a string that can be passed to your other page which you can unserialize() as follows:
secondPage.php:
$player = $_POST['playerObject'];
$player = unserialize($player);
Also, you forgot to use echo here:
change
value="<?php $player ?>"
to
value="<?php echo $player; ?>"
Get yourself into serialization: the process of making a string from the object, which can in future be deserialized from the string back to object.
Some docs, which could be useful for you:
PHP - How object serialize/unserialize works?
http://php.net/manual/ru/oop4.serialization.php
http://www.phpinternalsbook.com/classes_objects/serialization.html
I'm a beginner at coding, and I have to make a small game in php for school.
I have to use a hidden input, like this:
<form action="process.php" method="post">
<input type="hidden" name="rand" value="<?php rand(1,10); ?>" />
</form>
The value stands for a random number between 1 and 10 (I hope the value is correct). Now, in process.php I want to retrieve the random number by using post, so what I tried to do is the following:
<?php $random = $_POST['rand'];
echo $random; ?>
In my browser (Firefox), I'm getting the following error:
Notice: Undefined index: rand in
G:\xampp\htdocs\process.php on line 2
Does anyone know how I can echo the hidden value without using complex techniques?
Thanks in advance,
Maxime
You haven't echoed your rand function within the hidden input tag.
Also, there should be a submit button. Only then you can access the POST parameters.
<input type="hidden" name="rand" value="<?php echo rand(1,10); ?>" />
<input type="submit" name="submit" value="submit">
Try something like this:
if (isset($_POST['submit'])) {
echo "<pre>";
print_r($_POST); // See your POST array
$random = $_POST['rand'];
echo $random;
}
Hope this helps.
Peace! xD
There are some inputs, and there is a function. The function requires these inputs, and the inputs are user-given. But, the buttons that fire the function and the input submission form are two different buttons. So, when the user presses "submit" to store his variables, the variables are stored fine. But, when he presses the "calculate" button (which fires the function), php says "undefined index" because it reads the $_POST of that input again and again.
If I disable register_globals, it does not show 'undefined index' but these values are 0 again.
If I use another file just to store these values and then redirect back to the page where the function button is, there is a redirect loop, require_once does not work.
What is the way to store the inputs in such way that they can be used again and again in functions and whatsoever? No databases, I need a way to store them in variables.
edit: the form: <label for="asdf">enter value:</label> <input type="text" id="asdf" name="asdf" value="<?php echo $asdf;?>" />
storing the value:
$asdf=$_POST['asdf'];
then I need to write $asdf in the function with the updated value that the user gave through the html form. How to do it? Cannot be much simpler
I would just store them in the session. That way they, they can be used across php scripts, but are not stored in the long-term. Here's an example:
form.php
<?php
session_start();
?>
<html>
<body>
<form action="store.php">
<input type="text" name="x" value="<?php echo $_SESSION['x'] ?>">
<input type="text" name="y" value="<?php echo $_SESSION['y'] ?>">
<input type="submit" value="Submit">
</form>
<form action="calculate.php">
<input type="submit" value="Submit">
</form>
</body>
</html>
store.php
<?php
// Start the session
session_start();
$_SESSION["x"] = $_POST['x']; // substitute your input here
$_SESSION["y"] = $_POST['y']; // substitute your input here
?>
calculate.php
<?php
// Start the session
session_start();
$result = $_SESSION["x"] * $_SESSION["y"];
echo $result;
?>
There is no way to store them in variables. Every request to your server is a new request.
You could store the variables in a cookie/session or give them back after pushing the first button and store them in a hidden field in your html form. Or store them in a file on your server.
Is there a way I can pass a variable from one page to another but it only work on the next page clicked? After that it can be discarded/destroyed.
I've tried a php session but can't seem to kill the session on the next page clicked... or even if that way may be the wrong way to approach it.
Here's my session code:
<?php
session_start();
$x = $category;
$_SESSION['sessionVar'] = $x;
echo "$x";
?>
<?php
session_start();
$x = $_SESSION['sessionVar'];
echo "$x";
?>
I want to do this without having to submit a form.
You can pass a variable via a php session and then unset the variable on the following page.
$_SESSION['my_var'] = "some data";
$_SESSION['my_var'] = null;
unset($_SESSION['my_var']);
Send it as POST:
<form action="nextpage.php" method="post">
<input type="hidden" name="yourvariable" value="12345" />
<input type="submit" name="submit" value="Next page" />
</form>
Is it what was supposed?
I am using one session variable in my php page. As per my infomation, it is accessible throughout the program and it is, but problem is that it is showing different value for the same variable at different place in php page?
the code is as follows
<html><body>
<?php session_start();
if(!isset($_SESSION['x']))
$_SESSION['x']=1;
echo "X=". $_SESSION['x'];
?>
<form>
<input type="submit" name="save" value="save" />
</form>
<?php
if (isset($_GET['save']))
{
if(isset($_SESSION['x']))
$_SESSION['x'] = $_SESSION['x']+1;
echo $_SESSION['x']."<br>";
}
else
echo "no submit";
?>
</body></html>
value becomes different before and after submit button click? Please tell me why it is so?
thanks in advavnce.
You are redeclaring the value of session variable 'x' here
$_SESSION['x'] = $_SESSION['x']+1;
This is why its appearing 1 greater than its initial value.
it is due to the code itself
if(isset($_SESSION['x'])) //It is set
$_SESSION['x'] = $_SESSION['x']+1; //Add 1 to the value
echo $_SESSION['x']."<br>"; return value with +1
Solution
The reason the output is different is the order you echo and update
//Echo
//Update Value
//Echo again
Simple solution would be to move this
if (isset($_GET['save']))
{
if(isset($_SESSION['x']))
$_SESSION['x'] = $_SESSION['x']+1;
echo $_SESSION['x']."<br>";
}
else
echo "no submit";
to above this
if(!isset($_SESSION['x']))
$_SESSION['x']=1;
echo "X=". $_SESSION['x'];
Also note set the method and the action in the form to make sure it calls itself
<form method="GET" action="[url to itself]">
<input type="submit" name="save" value="save" />
</form>
Do it like this :
<html><body>
<?php session_start();
if(!isset($_SESSION['x']))
$_SESSION['x']=1;
echo "X=". $_SESSION['x'];
?>
<form method="GET" action="">
<input type="submit" name="save" value="save" />
</form>
<?php
if (isset($_GET['save']))
{
if(isset($_SESSION['x']))
echo $_SESSION['x']."<br>";
}
else
echo "no submit";
?>
</body></html>
this way the code prints out the same value after submit as it did before.
Either way you try if you print value and change after or change value and print after, when page reloads it will change value. you could add another button called increment and add the following code inside the php :
if (isset($_GET['inc']))
{
if(isset($_SESSION['x']))
$_SESSION['x'] = $_SESSION['x']+1;
}
and this one inside the form:
<input type="submit" name="inc" value="inc" />
this way youre variable increment when you press the inc button