James J.
James J.

Reputation: 39

php form issue, arrays & $_POST

I'm currently trying to build a html/php form that allows the user to send their contact info and select quantity of pre-defined products. So far the contact info is working perfectly, it sends an email to my email containing the contact info BUT the quantity of each product does not work. Please help.

Form Structure

Name: [INPUT FIELD]

Lastname: [INPUT FIELD]

Message: [INPUT FIELD]

Products:

Prod 1 [QUANTITY FIELD] - simple numeric input field

Prod 2 [QUANTITY FIELD] - simple numeric input field

Prod 3 [QUANTITY FIELD] - simple numeric input field

Anyways, here is the PHP code for my products, I'm using arrays.

<?php
    $my_products = array(
        '0' => 'product 0',
        '1' => 'product 1',
        '2' => 'product 2',
        '3' => 'product 3',
        '4' => 'product 4',
        '5' => 'product 5',
        '6' => 'product 6',
        '7' => 'product 7',
        '8' => 'product 8',
        '9' => 'product 9'
        '10' => 'product 10'
    );

    foreach ($my_products as $key => $value) {
        echo "<div class=\"col-xs-6\" style=\"margin: 10px 0 5px 0;\">";
            echo "<li class=\"list-group-item\" data-style=\"button\">";
                echo $value;
                echo "<input class=\"form-control selectBox\" maxlength=\"2\" min=\"0\" max=\"10\" type=\"number\" onkeypress=\"return isNumeric(event)\" oninput=\"maxLengthCheck(this)\" name=\"quantity[$key]>\"";
            echo "</li>";
        echo "</div>";
    }
?>

I'm trying to echo out the result from the input field (quantity) but it doesn't work, it says that it's undefined.

Further down the html file I have the php mailing function and as I said everything works except the quantity.

 $products = array(
    @$_POST['quantity[0]'], 
    @$_POST['quantity[1]'], 
    @$_POST['quantity[2]'],
    @$_POST['quantity[3]'],
    @$_POST['quantity[4]'],
    @$_POST['quantity[5]'],
    @$_POST['quantity[6]'],
    @$_POST['quantity[7]'],
    @$_POST['quantity[8]'],
    @$_POST['quantity[9]'],
    @$_POST['quantity[10]']);

$message = "<br /><strong>".$products[0]." - product 0</strong>"
  . "<br /><strong>".$products[1]." - product 1</strong>"
  . "<br /><strong>".$products[2]." - product 2</strong>"

and so on..

Upvotes: 1

Views: 502

Answers (2)

Truefalse
Truefalse

Reputation: 814

The following example was tested and works (best practices have been "forgotten" just to keep things identical) If you still have a problem with your code I suggest you to post the concerned files...

index.php

<?php
$my_products = array(
    '0' => 'product 0',
    '1' => 'product 1',
    '2' => 'product 2',
    '3' => 'product 3',
    '4' => 'product 4',
    '5' => 'product 5',
    '6' => 'product 6',
    '7' => 'product 7',
    '8' => 'product 8',
    '9' => 'product 9',
    '10' => 'product 10');

?>

<form action="show.php" method="post" accept-charset="utf-8">

<?php
foreach ($my_products as $key => $value) {
    echo "<div class=\"col-xs-6\" style=\"margin: 10px 0 5px 0;\">";
        echo "<li class=\"list-group-item\" data-style=\"button\">";
            echo $value;
            echo "<input class=\"form-control selectBox\" maxlength=\"2\" min=\"0\" max=\"10\" type=\"number\" name=\"quantity[$key]>\"";
        echo "</li>";
    echo "</div>";
}
?>
<input type="submit">
</form>

show.php

<?php

$products = array(
$_POST['quantity']['0'],
$_POST['quantity']['1'],
$_POST['quantity']['2'],
$_POST['quantity']['3'],
$_POST['quantity']['4'],
$_POST['quantity']['5'],
$_POST['quantity']['6'],
$_POST['quantity']['7'],
$_POST['quantity']['8'],
$_POST['quantity']['9'],
$_POST['quantity']['10']);

$message = "<br /><strong>".$products[0]." - product 0</strong>"
  . "<br /><strong>".$products[1]." - product 1</strong>"
  . "<br /><strong>".$products[2]." - product 2</strong>";

echo $message;
?>

Upvotes: 0

Elon Than
Elon Than

Reputation: 9765

First of all don't use @ sign. It's evil, really.

Your values are in $_POST['quantity'] array so you can access it like that $_POST['quantity'][2].

Upvotes: 3

Related Questions