Php Session Variable Deleted After Post - php

This code is supposed to save a random number into a session varible when generateCode is pressed
It does generate it and store in the variable but when i try to acces is from the if(isset($_POST['confirmCode'])){ it is empty
<?php
session_start();
if(isset($_POST['generateCode'])){
$_SESSION["key"] = mt_rand(100000, 999999);
}
if(isset($_POST['confirmCode'])){
die($_SESSION["key"]);
}
?>
<form method="POST">
<input type="text" name="confcode" placeholder="Confirmation Code">
<input type="submit" name="confirmCode" value="Confirm">
<input type="submit" name="generateCode" value="Generate">
</div>
</div>
</form>
Does anybody know how i can make a variable that stays some time even after post request
Edit:
So to get this clear confirmCode is a button to submit the code entered in a <input type="text></input> But i decided to not include the code to get the content if confCode because this question is about how to keep a variable generated by mt_rand even after <input type="submit"> is pressed

The problem isn't the button, PHP actually puts the name and value in the parameters.
The problem is with the die function. It expects either a message (string) or a status code (integer). When you generate the random number it interprets the message as a number (status code) instead of a string (message). To make sure it interprets the key as a string you could wrap it in a strval() function like this:
die(strval($_SESSION["key"]));

Related

How do i make form variable to display on all my site pages?

This is my form:
<form id="form1" name="form1" method="post" action="tracking.php">
<label>
<input type="text" name="trckno_trk" id="trckno_trk" />
</label>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</form>
When i submit my form, the submitted form variable displays well on the "tracking.php" page using <?php echo $_POST['trckno_trk']; ?>.
But when i click on other pages in the site, it doesn't seem to display. That mean that the form variable echo $_POST['trckno_trk'] displays only on one page but does not display on any other page.
Please, how can i get it to display on every other page on my site.
Try this
<?php
// Start the session
session_start();
?>
Then you html
<form id="form1" name="form1" method="post" action="tracking.php">
<label>
<input type="text" name="trckno_trk" id="trckno_trk" />
</label>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</form>
Then you should save the variable in your Session
<?php echo $_POST['trckno_trk'];
$_SESSION["trckno_trk"] = $_POST['trckno_trk'];
?>
Now you can use this Session to display on other pages.
Form submited data are available only for page you specified in action parameter. They are not stored in any way. You need to save them for example in database or SESSION variables and retrive them again when accessing other pages.
Form parameters are accesible only when you post them, to use them later you will need to save them somewhere.
If you'd like to save the value & display it everywhere for all clients, you will need to store it in a database (e.g. MySQL), fetch the value and print it
If you'd like it to be unique for each client, you could use cookies, for example:
setcookie('cookiename',
'the value',
time()+86400 /* seconds until it expires */,
'/' /* On what pages do you want to use it, '/' means all pages*/,
);
The code above saves the cookie, to print its value:
echo $_COOKIE['cookiename'];
Another option is to save it in the session, but it will expire after a short time
session_start(); // put this on the start of every page to start the session
$_SESSION['name'] = 'value'; // save a value in the session
echo $_SESSION['name']; // print it
I suggest you use $_SESSION[] to store your POSTED value, this way you will be able to access it on any other page.
So on your tracking.php you can have code like this
session_start();
$_SESSION['trckno_trk']=$_POST['trckno_trk'];
then on other pages you can access the variable using
session_start();
echo $_SESSION['trckno_trk'];

use $_GET['']; value inside $_POST[''] and submit it to the same page and display in the same page

i am new to php and while i am practing i came across a problem. actually,i have two files index1.php and index2.php. in index1.php i have a link with a unique id as
<a href="index2.php?companyid=<?php echo $row('company_id');?>>details</a>
i have got this value in index2.php as
if(isset($_GET['companyid'])){
$companyid = $_GET['companyid'];
}
now i have a search form in the index2.php as
<form method="POST" action="index2.php">
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
now on button click i want the search results be displayed in the same page as
'index2.php?companyid=$companyid'
but some how if i try to use $_POST['submit']; in the same page it takes me to index2.php and instead of index2.php?companyid=$companyid and also it throws error as undefined index of $companyid if i don't use $_POST['submit']; and echo $companyid; it gives value and works fine. all i want is that to use $companyid' value inside ``$_POST['submit']; as and display the result in the same url as before
if(isset($_POST['submit']){
$companyid //throws an error index of company id
}
any help will be appreciated
First off, it looks like you are not using the company id in the form itself, so it will not be submitted as part of the the POST. You could possibly use:
<form method="POST" action="index2.php">
<?php if (isset($companyid)): ?>
<input type="hidden" name="companyid" value="<?= $companyid; ?>">
<?php endif; ?>
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
But you would probably also need to change your logic to:
if(isset($_POST['companyid'])){
$companyid = $_POST['companyid'];
}else if(isset($_GET['companyid'])){
$companyid = $_GET['companyid'];
}
As Josh pointed out in the comments, PHP is not able to remember your previous GET request but this can easily be solved by altering the action attribute of the form element. By doing this you can pass on the previous data. This would look a little something like this:
<form method="POST" action="index2.php?companyid=<?php echo $companyid;?>">
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
This way you will be redirected to index2.php with the URL parameters present and you will be able to retrieve both search and companyid using $_POST and $_GET or use $_REQUEST for both.

Sending Input Type Text Value to Another File Using href in PHP (pure php solution)

I have an input type text box as follows
<input type="text" name="deleteprofileconfirmation" id="deleteprofileconfirmation" class="editprofileinput">
Delete Account
I need to pass the value entered in the input type text to deleteaccount.php
I can do with help of jquery, no problem, i need a pure php solution...
I tried using sessions, but problem is how to read the value in input type when link is clicked.. $_POST is also not working...
i cannot use form because this is a form in another form so html5 is not allowing nested forms, sorry should have mentioned that earlier
the following is not working on deleteaccount.php
if (isset($_POST['deleteprofilebutton']))
{
$delete_profile = strtolower($_POST['deleteprofileconfirmation']);
}
Make your link as
href="../controllers/deleteaccount.php?id=$ID_VALUE"
and update the POST to GET
if (isset($_GET['id']))
{
$delete_profile = strtolower($_GET['id']);
}
That would be GET now.
Make sure users with no privileges can hit this url and delete the profiles. Do check the user rights before processing the delete operation.
You can do this using form
<form action="../controllers/deleteaccount.php" method="post">
<input type="text" name="deleteprofileconfirmation" id="deleteprofileconfirmation" class="editprofileinput">
<input type="submit" class="deleteprofilebutton" name="deleteprofilebutton" id="deleteprofilebutton" value="Delete Account">
</form>
You could also give each one a unique name and just check the $_POST for the existence of that input.
<input type="submit" name="deleteprofileconfirmation" value="Delete" />
<input type="submit" name="submit" value="Submit" />
And in the code:
if (isset($_POST['deleteprofileconfirmation'])) {
//delete action
} else if (isset($_POST['submit'])) {
//submit action
} else {
//no button pressed
}

PHP - store input variable for static reuse

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.

isset function is not taking variable passed from other page

here i am getting a value from previous page with form here i assign the value to php variable $foodid i want to echo its value after the continue button is clicked
//its value is passed from the previous page form with action to this page
$foodid = $_REQUEST['foodid'];
//as soon as continue button is clicked i want to display $foodid
<form method="post" action="">
<input type="submit" name="continue" value="continue">
</form>
if(isset($_POST['continue'])){
echo $foodid;//here the foodid variable must be declared
}
PHP is a server side languaue
JAVASCRIPT - is a client side language
After redirecting to new page , you have the value with your self, but displaying it on click is possible with javascript only (will display the number without refreshing the page)
in PHP - its not impossible, but it does not make sense to redirect to same page with some additional parameters's to display
eg; on click continue , submit a form with no action and there form should have that input field with value which you want to display (can be in hidden type), it will get submitted to same page and you will get your value using
$_REQUEST['field_name'];
But its not recommended , use JS for this, people purposely use JS for such kind of things
You should pass the $foodid value through form to the php page. This can be done by declaring a hidden variable and assigning foodid value to it.
Try this
<?php
$foodid = $_REQUEST['foodid'];
?>
<form method="post" action="">
<input type="hidden" value="<?php echo $foodid ?>"
<input type="submit" name="continue" value="continue">
</form>
<?php
if(isset($_POST['continue'])){
echo $foodid = $_POST['foodid'];
}

Categories