A simple concept I can't get to function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
logike30
Forum Newbie
Posts: 5
Joined: Sat Jan 10, 2009 7:33 pm

A simple concept I can't get to function

Post by logike30 »

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!
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: A simple concept I can't get to function

Post by Eran »

Every form submission is a new separate request to the server, and does not share information with previous requests. So your $persons array at the beginning of the script is generated from scratch each time. You have two options to pass information between requests:
1. Pass the previous information as hidden inputs in the form
Iterate over the persons array and dump each value in as a hidden input:

Code: Select all

if(isset($_POST['add_persons'])) {
    //Retrieve previous persons information or create a new array
    $persons = isset($_POST['persons']) ? $_POST['persons'] : array(); 
    $persons[] = $_POST['name'];
} 
 
<form action="add_info.php" method="post">
    <?php 
    if( isset($persons) ) {
        foreach($persons as $person) {
              echo '<input type="hidden" name="persons[]" value="' . $person . '" />';
        }
    }
    ?>
    People: <input type="text" name="name" />
    <input type="submit" name="add_persons" value="Add" />
</form>
 
<?php
if(!empty($persons)) {
    foreach($persons as $person) {
         echo $person . "<br />";
    }
}
2. Use sessions - http://www.php.net/manual/en/intro.session.php
Instead of passing your persons information through the form you can persist them using sessions. Sessions are files stored on the server that uniquely identify a browser session and are used to persist information between requests. To use sessions you need to send the proper headers by calling session_start() (http://www.php.net/manual/en/function.session-start.php) before any output is sent to the browser (preferably at the top of your scripts) and then you could store persons into a session variable:

Code: Select all

 
if(!isset($_SESSION['persons']) ) {
     //Initialize the session variable
     $_SESSION['persons'] = array();
}
 
if(isset($_POST['add_persons'])) {
    //Retrieve previous persons information or create a new array
    $_SESSION['persons'][] = $_POST['name'];
} 
 
The rest should be similar to what you wrote.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: A simple concept I can't get to function

Post by califdon »

Excellent explanation, pytrin.

Very possibly, you shouldn't be using PHP to do this in the first place. PHP is a server script that is interpreted by the web browser before sending a page to the browser, so the only way it can react to anything that occurs after that is by initiating another page (or sending asynchronous data, as is done in Ajax). User interactions, if they don't involve referencing database data on the server or something, are usually much easier and cleaner to do with client script, namely Javascript.
logike30
Forum Newbie
Posts: 5
Joined: Sat Jan 10, 2009 7:33 pm

Re: A simple concept I can't get to function

Post by logike30 »

I will consider looking into that. I do not have a good understanding of javascript but I have no doubt I will be working with it soon. Thank you for your advice.
Post Reply