0

I have the following code which loops through an array to create forms that are filled with values (which the user will be able to edit) on a single page. There are as many forms as there are loops in the array, which is always changing.

<body>
<div id="main">
    <?php
    foreach($articles as $item) { ?>
        <div id='container'>
            <form>
                Title: <input type="text" name="title" size="80" value="<?php echo $item[0]; ?>">
                <br>
                URL: <input type="text" name="url" size="80" value="<?php echo $item[1]; ?>">
                <br>
                End Date: <input type="text" name="endDate" value="<?php echo substr($item[7], 14, strpos($item[7], '@') - strlen($item[7])); ?>">
                <br>
                <?php
                    if (substr($item[8], 0, 2) === 'Su'){
                    } else {
                ?>
                Start Date: <input type="text" name="startDate" value="<?php echo substr($item[8], 7, 9); ?>">
                <?php } ?>
            </form>
        </div>
    <?php } ?>
</div>
</body>

Now, I want the user to have a single submit button at the bottom of the page which will submit ALL the forms on the page to MySQL database. The problem is I don't know how to do that.

I know the submit button takes the format of

<input type="submit" value="Submit">

I am assuming I need to give each form in the loop a unique name but from there I am at a loss as to what my next step should be to actually send and receive the information from these multiple forms.

Any help would be appreciated. Thanks.

6
  • 5
    What is wrong with putting all sets of <input>s into a single form? You can't submit more than one form at once, your options are AJAX, iFrames or refreshing and submitting (in order of my recommendation) Commented Dec 28, 2012 at 18:15
  • How would you suggest I put all the inputs in a single form? Commented Dec 28, 2012 at 18:18
  • Take the <form> out of the loop.. Ill post an answer for it. Commented Dec 28, 2012 at 18:18
  • Does your $item array contains the ID of the field in the database? Commented Dec 28, 2012 at 18:21
  • 1
    Btw it's easier to just echo the html code with a single quote(') Like this echo '<div id="container">'; And it's easier to put the form outside the loop and the submit button after the loop Commented Dec 28, 2012 at 18:33

1 Answer 1

1

You can't submit more than one form at once. What is wrong with putting all sets of <input>s into a single form?:

<body>
<div id="main">
    <form>
    <?php
    $inpCnt = 0;
    foreach($articles as $item) {
        $inpCnt++;  ?>
        <div id='container'>
                Title: <input type="text" name="title_<?php echo $inpCnt; ?>" size="80" value="<?php echo $item[0]; ?>">
                <br>
                URL: <input type="text" name="url_<?php echo $inpCnt; ?>" size="80" value="<?php echo $item[1]; ?>">
                <br>
                End Date: <input type="text" name="endDate_<?php echo $inpCnt; ?>" value="<?php echo substr($item[7], 14, strpos($item[7], '@') - strlen($item[7])); ?>">
                <br>
                <?php
                    if (substr($item[8], 0, 2) === 'Su'){
                    } else {
                ?>
                Start Date: <input type="text" name="startDate_<?php echo $inpCnt; ?>" value="<?php echo substr($item[8], 7, 9); ?>">
                <?php } ?>
        </div>
    <?php } ?>
</form>
</div>
</body>

You need to be able to define each of these inputs aswel. So I've used the loop to give each one a unique name.

Sign up to request clarification or add additional context in comments.

9 Comments

@jeroen Feel free to elaborate
You have multiple fields with the same name, only the last one will be in your $_REQUEST array.
That would be a problem indeed. Would an i++ counter work inside a foreach loop?
You can change them to array named fields- title[], url[], endDate[], startDate[].
@Jonathan You should change them to arrays but I would use the ID of the database row as the array index to make it easier to handle at the back-end.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.