1

I need to add items to my existing array through a form on the site i made. Basically once i submit something on my form it needs to add the item to the array, i can only use php and html for this problem.

i tried array_push but it doesnt give me what i need because it doesnt use the form

<form action="" method="post">
    <input type="text" name="boodschappen"><br><br>
    <input type="submit" value="Verstuur">
</form>
<ul>
    <?php
    $boodschappen = ["aardappelen","aardbeien","3 pakken melk","yoghurt"];

    foreach ($boodschappen as $boodschap) {
        echo "<li>".$boodschap."</li>";
    }
    ?>
</ul>
</body>
</html>

5 Answers5

0
<?php
$post = $_POST;
$boodschappen = ["aardappelen","aardbeien","3 pakken melk","yoghurt"];
$result = array_merge($post, $boodschappen);

foreach ($result as $item) {
        echo "<li>".$item."</li>";
}
0
<?php
$a=array("red","green");
array_push($a,"blue","yellow");

echo "<pre>";
print_r($a);
?>
KUMAR
  • 1,764
  • 2
  • 6
  • 19
0

Perhaps like this?

<html>
    <head><title></title></head>
    <body>
        <form action="" method="post">
            <input type="text" name="boodschappen"><br><br>
            <input type="submit" value="Verstuur">
        </form>
        <ul>
            <?php

                $boodschappen = ["aardappelen","aardbeien","3 pakken melk","yoghurt"];

                if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['boodschappen'] ) ){
                    $boodschappen=array_merge( $boodschappen, explode(' ', $_POST['boodschappen'] ) );
                }

                foreach( $boodschappen as $boodschap ) {
                    echo "<li>".$boodschap."</li>";
                }
            ?>
        </ul>
    </body>
</html>

To update the array with persistance you can use a session variable like so:

<?php
    session_start();

    if( !isset( $_SESSION['boodschappen'] ) ){
        $_SESSION['boodschappen']=["aardappelen","aardbeien","3 pakken melk","yoghurt"];
    }

?>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <form action="" method="post">
            <input type="text" name="boodschappen"><br><br>
            <input type="submit" value="Verstuur">
        </form>
        <ul>
            <?php



                if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['boodschappen'] ) ){
                    $items=explode(' ', $_POST['boodschappen'] );
                    foreach( $items as $item )$_SESSION['boodschappen'][]=$item;
                }

                foreach( $_SESSION['boodschappen'] as $boodschap ) {
                    echo "<li>".$boodschap."</li>";
                }
            ?>
        </ul>
    </body>
</html>
Professor Abronsius
  • 26,348
  • 5
  • 26
  • 38
  • it does the same as the one from Lancehunter it adds only 1 item and changes the same one its not adding more new ones – Hicham Harmaz Sep 23 '19 at 09:14
  • each time the page loads the original array will be re-instated, that is the nature of PHP. If you need to permanently alter the array so that it always gets larger as each item is added you need some storage mechanism - such as a session – Professor Abronsius Sep 23 '19 at 09:17
0

If I understand your code correctly, you'll need some sort of persistent storage to store all the previous submission in the listing. A database would be ideal for long term storage. Session would be the minimal requirement:

<?php

session_start();

if (!isset($_SESSION['past_submission']) || !is_array($_SESSION['past_submission'])) {
  $_SESSION['past_submission'] = [];
}
if (!empty($_POST) && isset($_POST['boodschappen']) && !empty(trim($_POST['boodschappen']))) {
  array_push($_SESSION['past_submission'], $_POST['boodschappen']);
}
$past_submission = $_SESSION['past_submission'];

?>
<html>
<body>
<form action="" method="post">
    <input type="text" name="boodschappen"><br><br>
    <input type="button" value="Verstuur">
</form>
<ul>
    <?php
    $boodschappen = ["aardappelen","aardbeien","3 pakken melk","yoghurt"];
    array_push($boodschappen, ...$past_submission);

    foreach ($boodschappen as $boodschap) {
        echo "<li>".$boodschap."</li>";
    }
    ?>
</ul>
</body>
</html>

Please note that Session only works for the visitor session alone. The data is not available to other visitors or you.

As described before, you'd probably need a MariaDB / MySQL / PostgreSQL to store the submissions for long term. You'd probably need to use PDO to insert data into, or retrieve data from database.

Koala Yeung
  • 6,191
  • 2
  • 24
  • 46
-1
    <?php session_start(); ?>
    <ul>
        <?php
        if (!empty($_POST['submit'])) {

            $_SESSION['boodschappen'][] = $_POST['boodschap'];

           foreach ($_SESSION['boodschappen'] as $boodschap) {
               echo "<li>".$boodschap."</li>";
           }
        } else {
           $_SESSION['boodschappen'] = [];
        }  
        ?>
    </ul>
    <form action="" method="post">
        <input type="text" name="boodschap"><br>
        <input type="submit" name="submit" value="Verstuur">
    </form>
  • i still got the same problem with that it adds only 1 item and changes only that one it doesnt add more new ones – Hicham Harmaz Sep 23 '19 at 09:18
  • This is exactly what i needed in the back of my mind i was considering using a session but i stlil wanted to know if it could be done without. – Hicham Harmaz Sep 23 '19 at 09:24
  • @HichamHarmaz POST resets after each new pagerequest so it cannot be done without any type of storage such as a session or a database, or even a text file. But I'm glad it helped :) – Aaron NoHuanKnows Sep 23 '19 at 09:24
  • @AaronNoHuanKnows: [session_start](https://www.php.net/manual/en/function.session-start.php) in the middle of PHP output unless you have used [output buffer](https://www.php.net/manual/en/book.outcontrol.php). You'd catch some [header already sent error](https://stackoverflow.com/questions/6431935/php-session-header-already-send-error) and won't have your variable set as expected. – Koala Yeung Sep 23 '19 at 09:25
  • @AaronNoHuanKnows: Still, you're assigning data to `$_SESSION` after the first line of output. It wouldn't work unelss you're using output buffer. – Koala Yeung Sep 23 '19 at 09:29
  • @KoalaYeung Your pc must be broken then. Look up what a session variable is and how it functions on the PHP manual. Altering data isn't unrecommended and it'd work perfetly fine even after the first output. The only thing that wouldn't work are headers and starting the session in some cases, which is why I edited my answer. – Aaron NoHuanKnows Sep 23 '19 at 09:32
  • Accusing my computer "must be broken" wouldn't make a difference to reality. "Assigning session variable after output doesn't work" is the normal behavior for years. It is repeatable on all of the server (either setup by me or 3rd party vendor) all along the years. That's why I have to learn output buffer and other workaround. You may simply be using platform that have that done for you somehow. – Koala Yeung Sep 23 '19 at 09:37
  • @AaronNoHuanKnows: I didn't mean to offend or annoy you. Even smart people make mistake. I merely stating a fact that a piece of code would fail normally (and I provided the solution). – Koala Yeung Sep 23 '19 at 09:40
  • @KoalaYeung It's fine dude, let's just forget about this argument. The poster is happy and that's what matters. The code works for me and clearly for him so i don't know where you're getting all this from... But whatever. Have a good day. – Aaron NoHuanKnows Sep 23 '19 at 09:45
  • @AaronNoHuanKnows one last thing tho, even after refreshing it adds new items do you have a solution that if you refresh it keeps the items you have unless you close the whole page – Hicham Harmaz Sep 23 '19 at 09:46
  • @HichamHarmaz Make it refresh the page after it's been added – Aaron NoHuanKnows Sep 23 '19 at 09:48
  • what do you mean? – Hicham Harmaz Sep 23 '19 at 09:52
  • @HichamHarmaz `https://css-tricks.com/snippets/html/meta-refresh/` Try that – Aaron NoHuanKnows Sep 23 '19 at 09:53
  • iknow what u mean but thats not what im getting at. if you try the code yourself you will see that everytime you refresh after u added your first item it keeps adding lists – Hicham Harmaz Sep 23 '19 at 09:58