0

I'm trying to create a system for my organization that keeps track of points for our members. Part of that system is a page where the officers can create events for the members to sign up for in a form. Part of the event creation form is the "shift(s)" for that event, which looks like this:

<div id="shifts-div">
    <div class="form-inline">
        <div class="form-group">
            <label for="start">Start Time: </label>
            <input type="time" class="form-control" name="start[]" value="12:00:00">
        </div>

        <div class="form-group">
            <label for="end">End Time: </label>
            <input type="time" class="form-control" name="end[]" value="12:00:00">
        </div>

        <div class="form-group">
            <label for="end">Spots Available: </label>
            <input type="number" class="form-control" name="spots[]">
        </div>
    </div>
    <br>
</div>

Since there can be any number of shifts for a given event I allow the officers to dynamically add or remove shifts using the following buttons:

<button type="button" class="btn btn-default" onclick="addShift()">
  <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add Shift
</button>
<button type="button" class="btn btn-default" onclick="removeShift()">
  <span class="glyphicon glyphicon-minus" aria-hidden="true"></span> Remove Shift
</button>

Which run these two javascript functions:

function addShift(){
    var shift = 
        `<div class="form-inline">
            <div class="form-group">
                <label for="start">Start Time: </label>
                <input type="time" class="form-control" name="start[]" value="12:00:00">
            </div>

            <div class="form-group">
                <label for="end">End Time: </label>
                <input type="time" class="form-control" name="end[]" value="12:00:00">
            </div>

            <div class="form-group">
                <label for="end">Spots Available: </label>
                <input type="number" class="form-control" name="spots[]">
            </div>
        </div>
        <br>`;

    document.getElementById("shifts-div").innerHTML += shift;
}

and

function removeShift() {
    $('#shifts-div').children().last().remove();
    $('#shifts-div').children().last().remove();
}

This works well as far as adding and removing shifts to the page, but when I submit the form to a php file start, end, and spots are not an array and only hold the values for the last shift. I check this by running this in the php:

foreach($_POST as $key => $value){
    echo $key.' -> '.$value . ' ';
}

So, say I add 3 shifts with different values for each input and click submit, I would get this as the result (I took out the parts I don't think are relevant as they are just other non-array values for the event, like name and date, and are working properly):

start -> 14:00 end -> 17:00 spots -> 3

Where the start, end, and spots values are what I entered for the third shift that was added.

After some research I found that using innerHTML can cause some weird problems since it rewrites every node in the div, so I changed the addShift function to the following just to see if this was the problem (Not the prettiest way to do it, I know)

function addShift(){
    $("#shifts-div").append("<div class='form-inline'><div class='form-group'><label for='start'>Start Time: </label><input type='time' class='form-control' name='start[]' value='12:00:00'></div><div class='form-group'><label for='end'>End Time: </label><input type='time' class='form-control' name='end[]' value='12:00:00'></div><div class='form-group'><label for='end'>Spots Available: </label><input type='number' class='form-control' name='spots[]'></div></div><br>");
}

The same problem persists with the same results. This is my first time doing this kind of web development and this is how I came up with to tackle this problem, but I recognize that there are probably better ways to do this that I am unaware of, so there are two parts to my question:

  1. Why isn't my code running how I intend, and how do I fix that?

  2. How would an experienced web developer implement the functionality I am after (assuming he was also just using basic javascript, html, and php)?

Like I said I am new, so feel free to critique any parts of my code, even if it doesn't directly solve the problem. There are so many different ways to do anything, it is hard to know which is the "correct" way for any given situation. Thanks in advance and let me know if more of the code needs to be given.

EDIT:

I submit the form with this button:

<button type="submit" class="btn btn-default btn-lg">Submit</button>

and this is what the form looks like:

<form action="../php/create-event.php" method="POST" name="event">
John Slegers
  • 45,213
  • 22
  • 199
  • 169
  • How are you submitting the form? I don't see anything obviously wrong related to how you're naming your input fields. The `[]` should tell php to join them into an array. Also, have a look at the outgoing request in the dev-tools. – Michael Sacket Jan 14 '17 at 20:08
  • I added how I submit in the edit. How do you check the request in dev-tools? @MichaelSacket – MaxWMcKinley Jan 14 '17 at 20:10
  • My guess is that you are wrapping only one element inside form tags. Apart from that everything looks fine. You can refer http://stackoverflow.com/questions/13830276/how-to-append-multiple-values-to-a-single-parameter-in-html-form for more info. – Vignesh T.V. Jan 14 '17 at 20:27
  • @vignesh the – MaxWMcKinley Jan 14 '17 at 20:47
  • @MaxWMcKinley I have tried seeing your code and I am not able to find any issue. Can you paste the output of print_r($_POST); after adding multiple fields? – Vignesh T.V. Jan 14 '17 at 21:19
  • @vignesh So, it works now, I didn't change anything. I believe what happened is that my change from innerhtml to the jquery solution as described above actually fixed it, but i messed up somehow when trying to upload the changes to the server. And then when changing the file again and adding your suggestion and uploading it, it now works. – MaxWMcKinley Jan 14 '17 at 21:46
  • What should I do with this question now? – MaxWMcKinley Jan 14 '17 at 21:47

1 Answers1

0

So, it turns out I had just messed up with uploading my changes to the server. Once I changed the innerHTML to jquery as described in the question and properly uploaded it the problem was fixed.

I'm going to leave this question up because even finding that solution took me a long time and I never found anyone directly addressing this problem. If you are having the problem I described above do the change from innerHTML to another DOM manipulation method. The problem has something to do with innerHTML rewriting the entire element and losing references to the elements that were already there.

I'll gladly accept a different answer if someone can come along and explain it better than I can.