Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-11-2004, 04:42 AM   #1
Registered: May 2003
Distribution: Ubuntu 6.10 SE, Mac OS 10.4.8
Posts: 370

Rep: Reputation: 30
PHP: referencing form elements with dynamic names

I'm trying to reference data from a form (using method="post"). I have several sets of data in hidden variables, drop-downs, etc. that follow a naming convention (i.e. Amount1, Amount2, Amount3, etc.), and a set of radio-button choices, with values of 1, 2, 3, etc. What I would like to do is get the value from the radio button ($X = $_POST['radiobutton'];) and dynamically select the hidden form data from the corresponding AmountX element. Now, I know how to do this sort of thing with variables, like so:
    $x = 3;
    $test3 = "Hello World";
    $test_id = "test$x";
    echo $$test_id;
I just don't know how to apply this technique, though, when I am trying to reference form elements.
Old 03-12-2004, 05:19 AM   #2
LQ Newbie
Registered: Mar 2004
Posts: 4

Rep: Reputation: 0
Obvious first attempt

Have you tried:

$amount = "Amount$id";
Old 03-12-2004, 08:25 AM   #3
Registered: Feb 2004
Location: MD
Distribution: Fedora 9
Posts: 129

Rep: Reputation: 15
Here's what I would do. This is assuming you know how many input fields there are (if not, you can modify the code to check)

   for($i=0; $i < $numOfFields; $i++)
        echo "Radio Button " . $i . " is " . $_POST['radiobutton' . $i] . "<br>";
        echo "Amount Button " . $i . " is " . $_POST['Amount' . $i] . "<br><br>";        

Old 03-12-2004, 09:14 AM   #4
Registered: May 2003
Distribution: Ubuntu 6.10 SE, Mac OS 10.4.8
Posts: 370

Original Poster
Rep: Reputation: 30
Re: Obvious first attempt

Originally posted by dmazzoni
Have you tried:

$amount = "Amount$id";
Thanks, I actually tried that on my own a few hours ago and it worked. That should have been obvious to me, I must be slipping...
Old 03-17-2004, 10:15 PM   #5
Registered: Feb 2002
Location: Virginia USA
Distribution: Slackware, CentOS, Fedora
Posts: 342

Rep: Reputation: 30
Thank God I found this thread. It help me with my application for my Senior Class seminar.
Thanks elitecodex for your suggestion and Locura for asking questions.

Just thought you guys might want to know that the thread was helpful.

Cheers - Figa
Old 03-18-2004, 10:24 AM   #6
LQ Newbie
Registered: Mar 2004
Posts: 5

Rep: Reputation: 0
maybe even easier for you, might be to turn your strings into arrays. Eg:
<input name="Amount[1]">
<input name="Amount[2]">
<input name="Amount[3]">

it's easy to set, and to read, you just do:
$_POST['Amount'][1] or $_POST['Amount'][2]


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
static names with dynamic ip's linux_console Linux - Networking 2 01-12-2006 04:32 PM
No form output in PHP jabfinger Programming 2 07-15-2005 06:48 PM
PHP form help Zeppelin_Fan Programming 5 03-24-2005 04:47 PM
Simple PHP help with a form :-) 2782d4 Programming 3 12-17-2004 02:53 AM
php/form kev82 Programming 0 02-25-2004 06:21 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:12 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration