I want $id to increase by 1 every time the form is submitted. Then it should be appended to the array $users.
Why is this not working?
<?php
$users = array();
$id = 0;
if(isset($_POST["submit"])){
$id = $id + 1;
$users[] = $id;
}
echo "<pre>";
print_r($users);
echo "</pre>";
?>
<form action="random.php">
buy a ticket
<input type="submit" name="submit">
</form>
This is because once the PHP code stops executing the value of $id and $users is gone forever. HTTP and PHP is stateless. Once that page is processed it is gone and it is like it never existed. If you want to persist state you need to use a persistent data store like sessions or database.
<?php
session_start();
if(isset($_POST["submit"])){
if (!isset($_SESSION['users'])) { $_SESSION['users'] = 0 }
$_SESSION['users']++;
}
echo "<pre>";
print_r($_SESSION['users']);
echo "</pre>";
?>
<form action="random.php" method="post">
buy a ticket
<input type="submit" name="submit">
</form>
N.B.: Forms defaults to GET when a method isn't defined, therefore it needs the method="post" since you are working with POST variables.
Related
I have a task to take input from user and push it in array.
How can I take input from input field submit it, push it in to array and then do this again?
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Input Stuff in this Table :
<input type="text" name="insert">
<input type="submit" name="submit">
</form>
<?php
$insert = ($_POST["insert"]);
array_push($data, $insert);
print_r($data);
?>
you will miss the content of your variable and will re-initialize it on every page load, the solution is to keep your $data variable in the session and use it on every form submission:
<?php
session_start();
$data = isset($_SESSION['data']) ? $_SESSION['data'] : [];
array_push($data, $_POST["insert"]);
$_SESSION['data'] = $data;
?>
I want to add my $_POST variable to my array every time I submit a name. With this code it empties the array every time I use the Form. How should I do this if I want to add to the array everytime I submit a name?
<?php
$array = array();
if (isset($_POST['name'])){
$new_name = $_POST['name'];
array_push($array, $new_name);
}
print_r($array);
?>
<form action="index.php" method="POST">
<input type="text" name="name">
</form>
See you need something that will remember what $array was, even after a refresh. So either you would need to save it in a database / cookie.
Here is an example using a session ($_SESSION).
<?php
session_start();
if(!isset($_SESSION['names'])){
$_SESSION['names'] = array();
}
if (isset($_POST['name'])){
$_SESSION['names'][] = $_POST['name'];
}
foreach($_SESSION['names'] as $name){
echo $name . '<br>';
}
?>
<form action="index.php" method="POST">
<input type="text" name="name">
</form>
If you don't understand anything, please do ask.
I have numerous pages that I need to access a variable on. This variable is assigned a value when a user enters an ID into a form on accounts.php:
account.php
<form action="afterlog.php" class="form" method="post">
<input type="text" name="amid" id = "amid" class="input" />
<input class="btn" type="submit" value="Go" />
</form>
which posts the value 'amid' to afterlog.php
afterlog.php
<?php
session_start();
if($_SERVER['REQUEST_METHOD']=='POST')
{
$_SESSION['account_manager_id']=$account_manager_id;
$account_manager_id = $_POST['amid'];
header('Location: customer_view.php');
}
?>
which checks the POST, assigns the session variable, and redirects rhe user to customer_view.php
customer_view.php
I need to use '$account_manager_id' in this page and all pages after. Here is how I am assigning it the value of the _SESSION variable:
<?php
session_start();
$_SESSION['account_manager_id']=$account_manager_id;
?>
Bu the value isn't being held on any of the pages, including customer_view.php. I know its passing to afterload.php because it prints out on that page, but its gone after that page.
What I am doing wrong?
Thanks for your help!
You are trying to assign a value to $_SESSION['account_manager_id'] before $account_manager_id has any value in it. You just need to switch the order:
$_SESSION['account_manager_id']=$account_manager_id;
$account_manager_id = $_POST['amid'];
or simply:
$_SESSION['account_manager_id'] = $_POST['amid'];
in afterlog.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$account_manager_id = $_POST['amid'];
$_SESSION['account_manager_id']=$account_manager_id;
header('Location: customer_view.php');
}
?>
or
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$_SESSION['account_manager_id']=$_POST['amid'];
header('Location: customer_view.php');
}
?>
I am trying to set a session variable for the location selected in the dropdown menu when the user hits submit. The goal is to pass this variable to another php page so that I can display the drop down option they chose along with two values associated with it from a MYSQL table.
Below is a section of my code from my first php file. It shows the dropdown that is created from a list of values from the MYSQL database along with where I am trying to set the session variable for the selected option.
session_start();
<form action="/locationsprocessing.php" method="post">
<select id="locations" name="locations"><? echo $option; ?></select>
<input type="submit" value="Submit" name="submit" id="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$_SESSION['locations'] = $_POST['locations'];
$_SESSION['animal'] = 'cat';
}
Below is my code from the second page where I call the session variables for display. As you can see I have input another variable for testing purposes. It is supposed to display "cat" and it does.
session_start();
echo $_SESSION['animal'];
echo $_SESSION['locations'];
It may also be important to note that locationsprocessing.php runs a redirect using header. But that is really all that file does.
Am I missing something? I have gone through countless different tuts and such and been staring at this code forever. I can not see why it wo
you need start session in every pages where you need to use session:
<form action="/locationsprocessing.php" method="post">
<select id="locations" name="locations"><? echo $option; ?></select>
<input type="submit" value="Submit" name="submit" id="submit">
</form>
<?php
if (isset($_POST['submit'])) {
session_start();
$_SESSION['locations'] = $_POST['locations'];
$_SESSION['animal'] = 'cat';
}
This may be going wrong: When you submit your form in the first piece of code from the questions, you are sending the data entered in the form to /locationsprocessing.php. You say that all that file does is redirecting to another page. That means the data entered in the form is effectively lost.
I think what you want to do is to remove the form processing code from the first piece of code, so with only this remaining:
session_start();
<form action="/locationsprocessing.php" method="post">
<select id="locations" name="locations"><? echo $option; ?></select>
<input type="submit" value="Submit" name="submit" id="submit">
</form>
Then place that removed piece of code in /locationsprocessing.php, like this:
session_start();
if (isset($_POST['submit'])) {
$_SESSION['locations'] = $_POST['locations'];
$_SESSION['animal'] = 'cat';
header('Location: ' . $destination);
}
$destination would be the path for the page with the second piece of code in the question.
I'm trying to save a session variable in one of my script and it seems to work fine for a portion of the script, however, it gets dropped in the second part of my code. I've echoed every line and can't figure out why this is happening. Also, this code was working just fine before, but ever since I switched servers, it seems to be malfunctioning. I don't think it's the session save path because the first part of the code works fine. Here is the basic outer structure of my code (I've removed all the irrelevant parts to save space):
<!--raw-->
<?php
session_start();
include 'config.php';
?>
<html>
<body>
<form id="driver_record" method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
Enter Driver Number: <input type="text" name="driver" />
<input type="submit" value="Submit" name="user_submit" />
</form>
<?php
if(isset($_POST['user_submit']))
{
if(username_exists($_POST['driver'])){
$ulog = $_POST['driver'];
$_SESSION['user_id'] = $ulog;
echo $_SESSION['user_id']; //Session echos fine here
$tablename_cc = "cc_".$ulog;
$tablename_db = "db_".$ulog;
$tablename_misc = "misc_".$ulog;
$tablename_cash = "cash_".$ulog;
?>
<form id="expenses_update" method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
<input type="submit" value="Approve" name="record_approve" />
<?php
}
}
?>
<?php
if(isset($_POST['record_approve']))
{
echo $_SESSION['user_id']; //Session does not echo here
$ulog = $_SESSION['user_id'];
$tablename_cc = "cc_".$ulog;
$tablename_db = "db_".$ulog;
$tablename_misc = "misc_".$ulog;
$tablename_cash = "cash_".$ulog;
}
//unset($_SESSION['user_id']);
//session_destroy();
?>
</body>
</html>
<!--/raw-->
EDIT: I have commented out the unset statement, and also tried putting them inside the if statement, with no change to the behavior of the code.
Everytime the code is run you are calling the unset($_SESSION['user_id']); this is because it is not within your IF statement, you have a curly brace in the wrong place.
<?php
if(isset($_POST['record_approve']))
{
echo $_SESSION['user_id']; //Session does not echo here
$ulog = $_SESSION['user_id'];
$tablename_cc = "cc_".$ulog;
$tablename_db = "db_".$ulog;
$tablename_misc = "misc_".$ulog;
$tablename_cash = "cash_".$ulog;
unset($_SESSION['user_id']);
session_destroy();
} //moved curly brace here
?>
EDIT: The code runs fine for me, I had to comment out the include line and the function used, the fault must be within the config.php. Do you have a session_start() at the top of that file and no unsets used ?
Here is a simple session script, are you having problems with this ?
If so then there is a problem with php.
<?php
session_start();
$_SESSION['testing'] = "boo";
echo "Session 1=" . $_SESSION['testing'];
?>
<form action="session1.php" method="post">
<input type="text" name="input"
<input type="submit" />
</form>
<pre>
<?php
if (isset($_POST['input']))
{
print_r ($_SESSION);
print_r ($_POST);
}
?>
It seems that neither I or anyone else here can find a logical solution as to what's happening. I've decided to simply remove the session variables and include a 'hidden' input field that just sends the variable through a POST variable. This seems to solve all my problems without having to worry about the behavior of the session variables.
You set the session value only when the first form is submitted (isset($_POST['user_submit'])). When the script is processing the second form submission (isset($_POST['record_approve'])), no session value is set. Hence no output.
You do realize that, even if the script is in one PHP file, it will be triggered by two mutually exclusive form submissions, right? So the session value will be available only after isset($_POST['user_submit']. So if you submit the second form after submitting the first one, you will have a session variable 'user_id', provided you have commented out the lines that unset and destroy the session.
Hope it makes sense.