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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bbob
Forum Commoner
Posts: 57
Joined: Sat Aug 07, 2010 4:46 am

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

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

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

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Bbob
Forum Commoner
Posts: 57
Joined: Sat Aug 07, 2010 4:46 am

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

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

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

Post 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 />';
    }
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply