vendredi 29 novembre 2019

Adding User Input to the Array

I am trying to take user input by form and add it to array. Basically, I have one form which has "name" and "description" fields, and "Submit" button. Every time user fills in the form and clicks the submit button, I want it to be added into array. Right now it takes input and stores it, but I have no idea how to add more names and description from the user without removing the existing ones.

This is my Form in HTML:

<form method="post" action="">
            <div class="row form-group">
                <div class="col-md-12">
                    <label class="sr-only" for="name">Project Name</label>
                    <input type="text" name="project_name[]" id="name" class="form-control" placeholder="Project Name">
                </div>
            </div>
            <div class="row form-group">
                <div class="col-md-12">
                    <label class="sr-only" for="desc">Project Description</label>
                    <input type="text" name="project_desc[]" id="desc" cols="30" rows="10" class="form-control" placeholder="Project Description">
                </div>
            </div>
            <div class="form-group">
                <input type="submit">
            </div>
        </form>

And this is how I display it:

<h2 id="admin_project_name">
                            <?php
                                $project_names = $_POST['project_name'];
                                foreach( $project_names as $key => $n ) {
                                  print "The name is ".$n;
                                }
                            ?>
                        </h2>
                        <p id="admin_project_desc">
                            <?php
                                $project_desc = $_POST['project_desc'];
                                foreach( $project_desc as $key => $d ) {
                                    print "The description is ".$d;
                                }
                            ?>
                        </p>

How I can implement what I want? Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire