0

I have a foreach loop (see below):

<form action="code/update-to-dispatched.php" method="post" name="markAsDispatched">
    <?php
        foreach ($orders as $row) {
            $_POST['Username'] = $row['Username'];

            echo "<tr class='even'>";
            echo "<td>";
            echo "<strong>Order Date:</strong> ". $row['OrderDate'] ." <br />";
            echo "</td>";           
            echo "<td>";
            echo "<strong>Order ID:</strong> ". $row['OrderID'] ."";
            echo "</td>";
            echo "<td>";
            echo "<strong>Username:</strong> <input type='text' name='username' value=". $row['Username'] ." readonly style='border: 0; background: none;'>";
            echo "</td>";
            echo "<td>";
            echo '<input type="checkbox" name="chkBox[]" id="chkBox" value="'. $row['OrderID'] .'">';
            echo "</td>";
            echo "</tr>";
        }
    ?>

    <span class="tag"><a href="javascript:checkall('markAsDispatched','chkBox',true)">CHECK ALL</a></span>
    <span class="tag"><a href="javascript:checkall('markAsDispatched','chkBox',false)">UNCHECK ALL</a></span>
    <input type="submit" name="markAsDispatched" value="MARK AS DISPATCHED" />
</form>

I currently have 3 orders in my Database so the code above shows 3 orders. I am trying to pass all of the email addresses from the field via the $_POST['Username']. Why does the post variable only equal to the last email rather than a comma separated list such "[email protected], [email protected], [email protected]"?

1 Answer 1

1

If you want a comma-delimited list, try this:

$usernames = array();
foreach ($orders as $row) {
            //$_POST['Username'] = $row['Username'];
            $usernames[] = $row['Username'];

            echo "<tr class='even'>";
            echo "<td>";
            echo "<strong>Order Date:</strong> ". $row['OrderDate'] ." <br />";
            echo "</td>";           
            echo "<td>";
            echo "<strong>Order ID:</strong> ". $row['OrderID'] ."";
            echo "</td>";
            echo "<td>";
            echo "<strong>Username:</strong> <input type='text' name='username' value=". $row['Username'] ." readonly style='border: 0; background: none;'>";
            echo "</td>";
            echo "<td>";
            echo '<input type="checkbox" name="chkBox[]" id="chkBox" value="'. $row['OrderID'] .'">';
            echo "</td>";
            echo "</tr>";
        }

echo implode(',', $usernames);

You should never override the $_POST parameters by the way, it's very bad practise.

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

9 Comments

Thank you for helping @Ashley. I will bare that in mind. However, I am trying to echo out echo implode(',', $usernames); on "update-to-dispatched.php" which is the action of the form and I am getting the error: Warning: implode() [function.implode]: Invalid arguments passed in...
Can you output the value of $usernames in a var_dump?
var_dump($usernames); displays NULL.
Odd. And you say that in the loop detailed above, it definitely outputs some rows? As that suggests that it's not going through the loop above.
On the page where the foreach loop is I can see 3 rows, each with a different email address on... I am trying to echo out the array of emails on the action of the form which is update-to-dispatched.php, any other ideas?
|

Your Answer

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