0

I have some PHP code that checks a MYSQL database using MySQLi and outputs all of the 'stock' type into a form with a while statement:

$result = mysqli_query($link,"SELECT name FROM items WHERE type='stock' ORDER BY name");
echo '<table style="text-align:center;"><tr><th>Name</th><th>Quantity</th><th>Use</th>';

while($item = mysqli_fetch_array($result)){

        echo '<tr><form action="chooseProduct" method="POST"><td>' . $item['name'] . '</td><td><input type="number" name="amount[]"></td><td><input type="checkbox" name="stock[]" value="' . $item['name'] . '"></td></tr>';

    }

echo '<tr><td rowspan="3" colspan="3"><input type="submit" value="Select"></td></tr></form></table>';

I then need to be able to select the stock that I wish to use and click on select to send me to the "chooseProduct" page where there I should then be able to use the data 'amount' and 'stock' in an array form. However, when using POST to retrieve these values on the chooseProduct page by doing the following:

$stock = $_POST['stock'];
$amount = $_POST['amount'];

I get the error as follows:

Notice: Undefined index: stock in C:\xampp\htdocs\production\chooseProduct\index.php on line 59

Notice: Undefined index: amount in C:\xampp\htdocs\production\chooseProduct\index.php on line 60

The checkbox is definitely checked, so what could be causing this error?

2
  • What does var_dump($_POST) show? Commented Feb 19, 2014 at 15:25
  • @JohnConde array(0) { } Commented Feb 19, 2014 at 15:26

2 Answers 2

2

Your form tag is inside of your loop. This is creating an invalid mess of open forms on your page. This is almost certainly causing your form to submit invalid data or nothing at all. Move that opening <form> tag outside of your loop to fix this.

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

22 Comments

This is definitely helpful, thanks. Unfortunately, it doesn't seem to solve the problem.
Does your form have a closing </form> tag? Are you sure the rest of your HTML is valid?
The form closes in the last echo. I've read over the HTML and I'm reasonably sure it is valid as it outputs what I expect on the page visually. However, I haven't used arrays and checkboxes with forms, so I'm not sure if I'm using it right here.
Does var_dump($_POST) still show array(0) { }? If so there is still an HTML error somewhere. I would try to simplify the page if you can and see if that helps any.
Yes, it does. There isn't really anywhere to simplify the page, I know that the rest of the code on the page works fine and shouldn't be interfering with the code that is producing the error.
|
1

If you've not checked if POST action is already made so there will be warning / notice on assigning it. try

$stock = isset($_POST['stock']) ? $_POST['stock'] : '';
$amount = isset($_POST['amount']) ? $_POST['amount'] : '';

3 Comments

Although this is a good programming practice, it doesn't actually solve their issue.
Thank you sir :) . I've tried my best to do that, still newbie :)
That's ok. kudos to you for following good programming practices and for trying to help others. :)

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.