Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8432645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:11:03+00:00 2026-06-10T06:11:03+00:00

I’m trying to make a basic html form that passes information to a php

  • 0

I’m trying to make a basic html form that passes information to a php object then adds said object to an array. It works to the point of passing the information from the form, to the object, and adding it to the array and displaying said object. However, when I try to add a second object to the array it only seems to replace the array with a new single element array rather then adding to it. Here is my code… any ideas?

index.php file:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Custom Forms</title>
    </head>
    <body>
        <h2>Add Data</h2>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            First Name:<input type="text" size="12" maxlength="12" name="Fname"><br />
            Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />
            <button type="submit" name="submit" value="client">Submit</button>
        </form>
        <?php
        include_once 'clientInfo.php';
        include_once 'clientList.php';

        if ($_POST) {
            $clientArray[] = new clientInfo($_POST["Fname"], $_POST["Lname"]);
        }

        if (!empty($clientArray)) {
            $clientList = new clientList($clientArray);
        }
        ?>
        <p><a href="clientList.php">go to client list</a></p>
    </body>
</html>

clintInfo.php file:

<?php
class clientInfo {

    private$Fname;
    private$Lname;

    public function clientInfo($F, $L) {
        $this->Fname = $F;
        $this->Lname = $L;
    }

    public function __toString() {
        return $this->Fname . " " . $this->Lname;
    }
}
?>

clientList.php file:

<?php
class clientList {
    public function clientList($array) {
        foreach($array as $c) {
            echo $c;
        }
    }
}
?>

EDITED WORKING CODE WITH ANSWER

index.php file:

<?php
include('clientInfo.php');
session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Custom Forms</title>
    </head>
    <body>
        <h2>Add Data</h2>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            First Name:<input type="text" size="12" maxlength="12" name="Fname"><br />
            Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />
            <button type="submit" name="submit" value="client">Submit</button>
        </form>
        <?php
        if ($_POST) {
            $testClient = new clientInfo($_POST["Fname"], $_POST["Lname"]);

            echo $testClient . " was successfully made. <br/>";

            $_SESSION['clients'][] = $testClient;

            echo end($_SESSION['clients']) . " was added.";
        }
        ?>
        <p><a href="clientList.php">go to client list</a></p>
    </body>
</html>

clientList.php file:

<?php
include('clientInfo.php');
session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <title>
            Accessing session variables
        </title>
    </head>
    <body>
        <h1>
            Content Page
        </h1>
        <?php
        for ($i = 0; $i < sizeof($_SESSION['clients']); $i++) {
            echo $_SESSION['clients'][$i] . " was added. <br/>";
        }
        ?>
        <p><a href="index.php">return to add data</a></p>
    </body>
</html>

The object file clientInfo.php stayed the same. The objects needed to be stored in a multidimensional $_SESSION array and recalled with a for loop, a foreach loop would not work, unless some one else knows a way to make a foreach loop work, stick to a for. On a side note the $testClient variable could be skipped and just created and placed with in the $_SESSION at the same time, however doing it with the temp variable made it easier to trouble shoot and see how to make it work. Just thought I’d post the working code with the answer supplied by Josh!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:11:05+00:00Added an answer on June 10, 2026 at 6:11 am

    You need to add persistance to your array object.

    See PHP’s $_SESSION.

    If you don’t store it to memory or disk in between requests, it cannot possibly exist on successive loads. There is a nice tutorial in that link that should get you up and running.

    Alternatively, you can store things in a database, for larger needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.