0

I have this loop coded to create a dropdown with the numbers 0-30.

<td style="text-align:left;">
    <select id="iPhone12Case" name="iPhone12Case">
<?php
for ($i=0; $i<=30; $i++) {
?>
    <option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
    </select>
</td>

I cannot seem to figure out how to get the value of I to work in my calculations on an order processing form.

I assign the variable using

$iPhone12 = htmlspecialchars($_POST['iPhone12Case']);

but if I try to output $iPhone12 * 15 for example the answer is always zero.

4
  • 1
    Simple test, add a print_r($_POST); so you can see whats in the data passed to you. Commented Oct 18, 2022 at 16:50
  • Also Check the <form> tag has a method="POST" attribute Commented Oct 18, 2022 at 16:52
  • Also before using ANY values from the form at least check that it exists using if (isset($_POST'iPhone12Case'])) { Commented Oct 18, 2022 at 16:53
  • you need to somehow send it to the server... via ajax or via GET or POST request... please clarify your code and how are you sending it to the server to calculate it Commented Oct 18, 2022 at 17:02

1 Answer 1

3

Make sure you select is wrapped in a form element with method="POST". You should also use isset to make sure it exists.

<form action="" method="post">
    <td style="text-align:left;"><select id="iPhone12Case" name="iPhone12Case">
            <?php
            for ($i = 0; $i <= 30; $i++) {
            ?>
                <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
            <?php
            }
            ?>
        </select></td>
    <button>submit</button>
</form>

<?php

if (isset($_POST['iPhone12Case'])) {
    $iPhone12 = htmlspecialchars($_POST['iPhone12Case']);
    printf($iPhone12 * 15);
}
Sign up to request clarification or add additional context in comments.

2 Comments

But also make sure, that you are not creating invalid HTML - as this example does. A form can not be nested into a table row like that - either the form needs to go around the whole table, or it must be contained within a single table cell.
Oh yes, of course.

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.