0

I have several drop down lists in a table, I am trying to retrive the value of the drop down list if its not 0.

I have a table of items each item has the drop down list. Below is how I create the drop down list, I give it the tableid as a name because this is a unique identifier for the particular product.

echo "<td><select name=". $row['goosedown_id'] .">
                <option value=''> 0 </option>
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
                <option>6</option>
                <option>7</option>
                <option>8</option>
            </select>
        </td>";

Then once the form is submitted I am trying to just print any values i can... cause im not sure how to proceed... I do this like so.

$goosedown_id = check_input($_POST['goosedown_id']);
//..

goose down = $goosedown_id

I am hoping to be able to print out the associated values or calculate the price based off other items in the table i.e. price etc. but I am hoping someone can help me with trying to access the items that are no 0 first.

0

2 Answers 2

1

Have a look at the following example:

<!DOCTYPE html>
<head>
    <link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <?php
    if (count($_POST)) {
        for ($i = 0; $i < $_POST["nr_rows"]; $i++) {
            echo "<h2>id=".$_POST["goosedown_id"][$i].", qty=".$_POST["qty"][$i]."</h2>\n";
        }
        die("<pre>".print_r($_POST, true)."</pre>\n");
    } else {
        // connect to the database
        $link = mysqli_init();

        // Adjust hostname, username, password and database name before use!
        $db = mysqli_real_connect($link, "localhost", "root", "", "test") or die(mysqli_connect_error());

        $SQL = "SELECT * FROM 5050goosedown ORDER BY price ASC";
        $result = mysqli_query($link, $SQL) or die(mysqli_error($link));

        $rows = array();
        for ($i = 1; $row = mysqli_fetch_assoc($result); $i++){
            $rows[] = array(
                'id'        => $row['goosedown_id'],
                'bgcolor'   => ($i % 2) ? '#F5F5F5' : '#E5E5E5',
                'name'      => htmlentities($row['name']),
                'size'      => ($row['width'] || $row['height']) ? $row['width'].'/'.$row['height'] : '',
                'fill'      => $row['normal_fill'].'/'.$row['our_fill'],
                'old_price' => $row['old_price'] ? $row['old_price'] : '',
                'price'     => $row['price']
            );
        }
    }
    ?>
    <form action="<?= $_SERVER['SCRIPT_NAME'] ?>" method="post">
        <table border="0" width="600">
            <tr>
                <th width="30%" colspan="2"><b>50/50 Goose Down:</b></th>
                <th width="30%"><i>Normal Fill / Our Fill</i></th>
                <th width="12.5%"><i>Old Price</i></th>
                <th width="12.5%"><i>Price</i></th>
                <th width="12.5%"><i>Quantity</i></th>
            </tr>
            <?php foreach ($rows as $row): ?>
            <tr bgcolor="<?= $row['bgcolor'] ?>">
                <td><?= $row['name'] ?></td>
                <td><?= $row['size'] ?></td>
                <td><?= $row['fill'] ?></td>
                <td><?= $row['old_price'] ?></td>
                <td><?= $row['price'] ?></td>
                <td>
                    <select name="qty[]">
                        <?php for ($i = 0; $i <= 8; $i++): ?>
                        <option value="<?= $i ?>"><?= $i ?></option>
                        <?php endfor ?>
                    </select>
                    <input type="hidden" name="goosedown_id[]" value="<?= $row['id'] ?>" />
                </td>
            </tr>
            <?php endforeach ?>
        </table>
        <input type="submit" name="myButton" id="myButton" value="Submit" />
        <input type="hidden" name="nr_rows" value="<?= count($rows) ?>">
    </form>    
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

1

Did you look at the source of the page you're building? Your form is going to be

<td><select name="value_of_the_variable_your_insert">

not

<td><select name="goosedown_id">

since you're inserting $row['goosedown_id'], you need to look for the VALUE of that variable, e.g.

$_POST[$row['goosedown_id']]

3 Comments

Could you fix your answer? I don't see any extra right-square-bracket in his code.
oh okay.. it just kinda clicked.. after I read your post like 10 times.. so I put the $_POST[$row['goosedown_id']] in my reciving php file.
I am going to keep trying but I have not been able to get this to work yet :( thanks for the help anyway. looking at the source code has given me some more insight.

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.