AwsAyad
AwsAyad

Reputation: 11

HTML post vs PHP arrays

Without using Ajax or Java script, I need a POST form to store more than one value in php array. for example, if I entered a text value in my text area (i.e. Dog, Cat) I want the output to remain on the page when I enter a second value.

Here is my code, its working for one value but the page refresh and I lose the value when I enter a new one:

<!DOCTYPE HTML>
<html>  
<body>
<?php 
$name = array();
?>
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post">
<label for="animal">Please add an Animal:</label><br>
<input type="text" name="animal" />
<br>
<br>

<input type="submit" value="Submit">
</form>
<hr>
<p>Results:</p> 

<?php 

if ($_SERVER["REQUEST_METHOD"] === "POST") {
array_push($name, $_POST["animal"]);
}

foreach ($name as $animal)
{
 echo "You entered: $animal <br>" ;
}
?>

</body>  
</html>

Upvotes: 0

Views: 52

Answers (2)

HPierce
HPierce

Reputation: 7409

I'd let the HTML do all the heavy lifting:

<html>
    <body>
        <form method="post">
            <?php echo "You've posted " . implode(',', $_POST['animals']); ?>

            <input type="text" name="animals[]"/>

            <?php foreach ($_POST['animals'] as $animal) { ?>
                <input type="hidden" name="animals[]" value="<?php echo $animal;?>"/>
            <?php } ?>

            <input type="submit">
        </form>
    </body>
</html>

You're able to create arrays of elements in forms by naming your inputs with []. In this case the name of animals[] allows you to build up an array of animals over multiple HTTP requests without needing to manually merge multiple items.

On a side note, there's an XSS vulnerability in the code I've posted, but that probably isn't important to you right now.

Upvotes: 1

mickmackusa
mickmackusa

Reputation: 47991

I am making a few empty checks before trying to access/use the potentially submitted data -- this avoids getting some nasty warnings.

I am passing the old values as a json string, then decoding it to add the new value to it upon each submission.

A hidden input is used to pass the old values with every new submission.

Code:

<!DOCTYPE HTML>
<html>  
<body>
<?php 
if (!empty($_POST['storedanimals']) && !empty($_POST['animal'])) {
    $storedanimals = array_merge(json_decode($_POST['storedanimals'], true), array($_POST['animal']));
} elseif(!empty($_POST['animal'])) {
    $storedanimals = array($_POST['animal']);
} else {
    $storedanimals = array();
}
?>
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post">
    <label for="animal">Please add an Animal:</label><br>
    <input type="text" name="animal"><br><br>
    <input type="hidden" name="storedanimals" value='<?=json_encode($storedanimals)?>'>
    <input type="submit" value="Submit">
</form>

<?php 

if ($storedanimals) {
    echo "<hr>";
    echo "<p>Results:</p>";
    foreach ($storedanimals as $animal) {
        echo "You entered: $animal <br>" ;
    }
}
?>
</body>  
</html>

Upvotes: 1

Related Questions