A simple concept I can't get to function
Posted: Sat Jan 10, 2009 7:44 pm
This is very simple, but as I am new to PHP I am having trouble getting it to function as I want.
In a portion of a website I am creating, I have a form that asks for a name and has a 'add' button next to it.
What I want is; when someone types in a name in the form then clicks 'add' it will add that name to an array as well as list the name they just added below the name input box, so they can see what they have added so far. I will then use that array to populate a database table.
Here is my current code that won't work, the page is called 'add_info.php' and the form calls its own page.
At the top I have:
if(isset($_POST['add_persons'])) {
$persons = array();
$persons[] = $_POST['name'];
}
And for the form portion I have:
<form action="add_info.php" method="post">
People: <input type="text" name="name" />
<input type="submit" name="add_persons" value="Add" />
</form>
And below the form I have:
if(!empty($persons)) {
foreach($persons as $person) {
echo $person . "<br />";
}
}
It will display the first name added, but won't add the next name it will just replace the first and only value in the array. I know it is really simple but I can't put my finger on it.
Thanks for any help!
In a portion of a website I am creating, I have a form that asks for a name and has a 'add' button next to it.
What I want is; when someone types in a name in the form then clicks 'add' it will add that name to an array as well as list the name they just added below the name input box, so they can see what they have added so far. I will then use that array to populate a database table.
Here is my current code that won't work, the page is called 'add_info.php' and the form calls its own page.
At the top I have:
if(isset($_POST['add_persons'])) {
$persons = array();
$persons[] = $_POST['name'];
}
And for the form portion I have:
<form action="add_info.php" method="post">
People: <input type="text" name="name" />
<input type="submit" name="add_persons" value="Add" />
</form>
And below the form I have:
if(!empty($persons)) {
foreach($persons as $person) {
echo $person . "<br />";
}
}
It will display the first name added, but won't add the next name it will just replace the first and only value in the array. I know it is really simple but I can't put my finger on it.
Thanks for any help!