Recently I started with PHP and there's something im trying to find out. This is what I want:
Input field + Submit. When u submit the form, the value of the input field goes in an array. lets say $array = array(); Every time you submit the value gets put in that array.
I've done things with $_GET and $_POST and other things like $_SESSION but I just can't get this to work..
Help would be appreciated! thanks!
EDIT: The information (list) will just be showed on the page. Not send to a mysql database or anything. When u refresh the page it would be gone.
It sounds like you want to keep the data that you submitted.
index.php
<?php
session_start();
if(isset($_POST['a_value']))
{
if(!isset($_SESSION['a_value']) || !is_array($_SESSION['a_value']))
{
$_SESSION['a_value'] = array();
}
array_push($_SESSION['a_value'], $_POST['a_value']);
}
?>
<form action="index.php" method="POST">
<input type="text" name="a_value">
<input type="submit" value="Go">
</form>
<?php
if(isset($_SESSION['a_value']))
{
echo '<br><br>Values so far:<br><pre>'.print_r($_SESSION['a_value'], true).'</pre>';
}
?>
there some better way for do this, but you can use the session
like this:
you need 2 session variable for this aim, first for counting the array index, and second for value...
here is an example
<form method="post" name="frm" action="#">
<input type="text" name="txt" id="txt" />
<input type="submit" name="btn" value="submit">
</form>
<?php
session_start();
if ( isset($_POST['btn']) ) {
$_SESSION['counter'] += 1;
$_SESSION['val'][$_SESSION['counter']] = $_POST['txt'];
var_dump($_SESSION);
}
?>