Adding a variable to an array using a Post Form - php

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.

Related

How to add Data in Array Dynamically, from form In PHP

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;
?>

Storing form data in session with php

I'm testing a really basic PHP form, where the form data is saved in a session.
Later, i want that session data to be the default value of the form:
<strong>Test Form</strong>
<form action="" method"post">
<input type="text" name="var" value=<?php $name ?>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
$name = $_SESSION['var'];
}
echo $name;
?>
So, for example if i input "MyName" it should echo "MyName" and in the form there should be the value "MyName". The problem with the actual code is that it gives an E_NOTICE : type 8 -- Undefined variable: name -- at line 18 error. I think that the variable is not being stored, can someone help me out on this?
The first error I notice is this piece of code:
<form action="" method"post">
Where method does not contain the symbol "=" which causes the loss of the parameter post.
Furthermore, the "session_start ()" function must be placed before any other code. The code derives from this is as follows:
<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
$name = $_SESSION['var'];
} else {
$name = null;
}
?>
<strong>Test Form</strong>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="var" value="<?= ($name != null) ? $name : ''; ?>">
<input type="submit" name="Submit" value="Submit!" />
</form>
Spotted a few things:
I'd put the session_start(); at the top of the page before outputting anything.
Your method was incorrectly written it needs a '=' when specifying the method - thats the main reason why nothing was being stored, the form wasn't submitting properly.
Same with how you've put in the value on the name input - it has no '=' and you don't close the input tag properly - I've left it blank and added a placeholder - you can change it to what you need.
Heres how I'd do it:
<?php session_start(); ?>
<strong>Test Form</strong>
<form action="" method="post">
<input type="text" name="var" value="" placeholder="enter name">
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
}
// Store the session in a variable after the submit - otherwise it will be forgotten on refresh
$name = $_SESSION['var'];
// check if session exists
if(isset($name)) {
echo $name;
}
else {
echo 'no name entered...';
}
?>
You can edit the above to hide the form if a name has been submitted etc. Use session_destroy(); to reset the stored session.
Cheers

php prevent form resubmission using sessions

So I am trying to prevent form resubmission using sessions
and this is my code :
<?php
session_start();
if(isset($_GET['unid']))
{
if ($_GET['unid']==$_SESSION["uid"])
{
echo "Form is Submited do something";
}
else
{
echo "no you can't do that";
}
}
$unid = md5(uniqid());
$_SESSION["uid"] = $unid;
?>
<form method="GET">
<input name="name" value="test">
<input name="unid" value="<?php echo $unid;?>">
<input type="submit">
and it works ...but if the user opens another tab then it will break so how can I fix it ?
I'm not sure about this but may be assigning a new global session variable will work, say $_SESSION['checkSub']. So once the form is submitted, set it to 1 or true and only let form submission if it isn't 1 or false.
You can check to see if the unid is set in the session before generating a new unique id. See updated code below:
<?php
session_start();
if(isset($_GET['unid']))
{
if (isset($_SESSION['unid']) && $_GET['unid']==$_SESSION['unid'])
{
//form has been submitted with matching unid - process it as needed
unset($_SESSION['unid']);
}
else
{
// Form was resubmitted or some other logic error
}
}
$unid = ''; //declare here for good scope
if (isset($_SESSION["unid"])) {
$unid = $_SESSION["unid"];
}
else {
$unid = md5(uniqid());
$_SESSION["unid"] = $unid;
}
?>
<form method="GET">
<input name="name" value="test">
<input name="unid" value="<?php echo $unid;?>">
<input type="submit">
Rather than using form method GET, try to use POST. It will work for you. The $_POST array will only have data in it when form is submitted, so you should not have to use the session to know whether form is submitted or not.

Variable count is not set in array

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.

PHP Session variables in multiple pages

So, here I have page1.php:
<form action="action_form.php" method="post">
<select name="font_syle">
<option value="tahoma">Tahoma</option>
<option value="arial">Arial</option>
</select>
<input type="submit" value="Done" />
</form>
Here action_form.php:
<?php
session_start();
$font_style = $_POST["font_syle"];
$_SESSION["font_syle"] = $font_style;
if($_SESSION["font_syle"] == 'tahoma') $font_style = 10;
else if($_SESSION["font_syle"] == 'arial') $font_style = 20;
$total = $font_style;
echo $total;
?>
And here page.php
<?php
ob_start();
include 'action_form.php';
ob_end_clean();
echo $total;
?>
I don't know why the value of "$total" is not printed on page.php
page.php includes action_form.php. That sets the value of $font_style to:
$font_style = $_POST["font_syle"];
Since page.php hasn't just been posted through a form, it's setting $font_style to an empty string. So when you come to echo it out, there's nothing there to echo.
You can do echo $_SESSION["font_syle"]; in the page.php to print it
The reason is your form is going to action_form.php and store the data inside the variable $_SESSION.
When you open page.php the data doesn't exist anymore because $total does not move between page.
The solution here is to change :
<form action="action_form.php" method="post">
for
<form action="page.php" method="post">
OR
Print out the session variable instead.

Categories