PHP unable to grab name of dynamically generated forms

I'm trying to update my database, based on a value in a textbox. The first foreach loop is working fine ($emails is grabbed from another class), but in the second loop I am unable to grab the name of each form ("form_UD") without getting an undefined index error. I'm also pretty sure that my syntax in the second foreach loop is wrong. What I'm trying to do is check that for each of these forms, if the user clicks its corresponding "btn_update" button, then call the $RUD class' updateEmail method and insert the value from this button, as well as the value from the form's corresponding "input_email" value.

foreach ($emails as $email) {
            echo "<li>
                    <form action='' method='POST' id='form_email' name='form_UD'>
                        <input type='text' value='" . $email['news_email'] . "' name='input_email'/>
                        <button type='submit' class='btn_update' name='btn_update' value='" . $email['news_id'] . "'>Update</button>
                    </form>
                </li>";
        }
        foreach (($_POST['form_UD']) as $form) {
            if ((isset($form->$_POST['btn_update']))) {
            $RUD->updateEmail($form->$_POST['btn_update'], $form->$_POST['input_email']);
        }

EDIT: New idea:

        <form action='' method='POST' id='form_email'>
        <ul>
        <?php
            foreach ($emails as $email) {
                echo "<li>
                        <input type='text' value='" . $email['news_email'] . "' name='input_email" . $email['news_id'] . "'/>
                        <button type='submit' class='btn_update' name='btn_update" . $email['news_id'] . "' value='" . $email['news_id'] . "'>Update</button>
                    </li>";
            }

            foreach ($emails as $email) {
                if (isset($_POST['btn_update' . $email['news_id']])) {
                    echo $_POST['btn_update' . $email['news_id']];
                    echo $_POST['input_email' . $email['news_id']];
                    $RUD->updateEmail(($_POST['btn_update' . $email['news_id']]), ($_POST['input_email' . $email['news_id']]));
                }
            }
        ?>
        </ul>
    </form>

The echo statements are getting the correct values on click. By the way, $RUD was instantiated elsewhere in the same file. This leads me to believe there may be an issue in my method that's handling the update? Here it is:

public function updateEmail($id, $email){

    $db = Dbclass::getDB();
    $query = "UPDATE newsletter
            SET news_email = :email,
            WHERE id = :id ";
    $statement = $db->prepare($query);
    $statement->bindParam(':id', $id);
    $statement->bindParam(':email', $email, PDO::PARAM_STR, 100);
    $statement->execute();
}

Dbclass:getDB() does work. I confirmed it with other methods.

Answers


I could be wrong - but I don't believe this is possible unless you submit via XHR. The reason is that you have 1 submit button to 1 form to submit that form - you can not submit many forms from within a form with a submit, it will only submit that form the submit button exists as child element of and it will arrive in your $_POST.

There is no ability to submit ALL forms via 1 button without client side scripting and preparing the statement as $_GET request or XHR post (get could be used as well via XHR). Generally you would name inputs with [] in the name to signify it as an array on a form when you want the inputs to arrive as an array value.

You seem to be trying to use form as an array and assuming it arrives as an array of objects with all of the input elements as array elements of a property named after the form - which is not how this works. Maybe there is some darker art now available for PHP that I haven't seen or been exposed to but I doubt it, it has been a while since I handled a form in this method.

Check out: Submit Multiple Forms With One Button for how to do this via XHR.

Further details on XHR: http://www.w3schools.com/ajax/ajax_xmlhttprequest_create.asp

Check out this link for the HTML5 form standard: https://www.w3.org/TR/html5/forms.html

A simpler write up can be found here: http://www.w3schools.com/html/html_forms.asp

Further read this on how this works with PHP: http://www.w3schools.com/php/php_forms.asp

Finally read this for how $_POST works: http://php.net/manual/en/reserved.variables.post.php

Again, I could be wrong but it's doubtful.

Added example of debugging code from comment below:

if (isset($_POST) && count($_POST) > 0) {
    print_r($_POST);
    die();
}

Here's the working code:

Main PHP file:

<form action="" method="POST" id="form_email">
        <ul>
        <?php
            foreach ($emails as $email) {
                echo "<li>
                        <input type='text' value='" . $email['news_email'] . "' name='input_email" . $email['news_id'] . "'/>
                        <button type='submit' class='btn_update' name='btn_update" . $email['news_id'] . "' value='" . $email['news_id'] . "'>Update</button>
                    </li>";
            }

            foreach ($emails as $email) {
                if (isset($_POST['btn_update' . $email['news_id']])) {
                    $RUD->updateEmail(($_POST['btn_update' . $email['news_id']]), ($_POST['input_email' . $email['news_id']]));
                }
            }
        ?>
        </ul>
    </form>

Method in class RUD:

  public function updateEmail($id, $email){

    $db = Dbclass::getDB();
    $query = "UPDATE newsletter
            SET news_email = :email
            WHERE news_id = :id ";
    $statement = $db->prepare($query);
    $statement->bindParam(':id', $id);
    $statement->bindParam(':email', $email, PDO::PARAM_STR, 100);
    $statement->execute();
}

I had some silly typos/errors in my SQL statement...removed a comma after ":email" in WHERE clause, and updated "id" to be the proper "news_id" from the table. Ha!


Need Your Help

Python UnicodeEncodeError

python mysql database python-2.7 python-unicode

I am trying to insert some data into a MySQL Database with this statment:

Parsing command line options in Jython 2.1

python websphere jython websphere-6.1 wsadmin

I'm using Jython 2.1 for wsadmin scripting and want to find a better way of parsing command line options. I'm currently doing this: