Jump to content

Getting the input field of only one item

Recommended Posts

Hey guys,


first i want to apologize for my english and hope you still understand the meaning of it.


I list items that you can buy and also choose the number, how many items you want to buy. Code:

foreach ($furnis as $f) {
     echo "<h3>$f->name</h3>";
     echo "<h5>".number_format($f->price, 0, ",", ".")." Taler</h5>";
     echo "$f->quantity x";
      if($money >= $f->price) {
      echo "<input name='quantity' type='number' min='1' max='$f->quantity'>";
      echo "<button type='submit' name='buy' value='$f->id'>Buy</button>";
      }else {
        echo "<button>Not enough money</button>";

To get the aricle, which is clicked is easily done with getting the ID:

$id = input($_POST['buy']);

Now my question is, how to get the quantity (inputfield) right?

When I try the same:

$quantity = input($_POST['quantity']);

I do not get the quantity. Of course I get it, if only one article is displayed, but there are many more. So instead of getting the quantity of the article the person clicked on, i get a random quantity from an other article.


How can I do it right?

Thanks for any help

Share this post

Link to post
Share on other sites

Create an array for the quantity inputs using the id as the key.


<input name="quantity[$f->id]" type="number" >
You can then access individual input


$quantity = $_POST['quantity'][$id]

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.