Page 1 of 1

How to attach ID(from data in DB) into a textfield?

Posted: Wed Oct 20, 2010 5:03 am
by Bbob
Hi

Im trying to attach an "id" into a textfield?


I plan to do a form like this

ItemID | Name | Price
1 | pen | <textfield>
2 | paper | <textfield>
3 | ink | <textfield>
<button>


I was hoping if I could attach the itemid with the textfield, it would be easier to distinguish when Im about to insert the values from the textfield in the database

Re: How to attach ID(from data in DB) into a textfield?

Posted: Wed Oct 20, 2010 6:40 am
by s.dot
If you are using a single form for each item.. Just pass the item ID along in a hidden input field.

If you are using a single form for multiple items then you will have to make your form inputs like this..
[text]<input type="text" name="price[itemIDhere]:">[/text]

Then PHP will parse your form into a multidimensional $_POST array for you to deal with.

Re: How to attach ID(from data in DB) into a textfield?

Posted: Thu Oct 21, 2010 5:45 am
by Bbob
Thanks for the reply :D


When it is sent to the PHP script that handles validation, how do I "cut" the array so I could validate each item entered?

Re: How to attach ID(from data in DB) into a textfield?

Posted: Fri Oct 22, 2010 12:22 am
by s.dot
do a print_r($_POST) to see what the post array looks like.

Actually, I would set up the form like this though

Code: Select all

<input type="text" name="item[1][price]">
<input type="text" name="item[1][name]">

<input type="text" name="item[2][price]">
<input type="text" name="item[2][name]">

<input type="text" name="item[3][price]">
<input type="text" name="item[3][name]">
Where 1, 2, and 3 are the respective item ID numbers and price and name are the fields you are passing. Adjust it to fit your situation.

Then, to analyze the post input, you could do

Code: Select all

<?php

if (!empty($_POST['item']))
{
    foreach ($_POST['item'] AS $itemID => $details)
    {
        echo 'Item ID: ' . $itemID . ' Price: ' . $details['price'] . ' Name: ' . $details['name'] . '<br />';
    }
}