Solved

Radio button and array (name=grade[])

Posted on 2007-12-01
10
1,948 Views
Last Modified: 2013-12-12
In php when I want to pass variables as arrays we append square bracket [] to the name of the input like so

echo '<input type=hidden name=year[] value="' . $datarow['date'] . '">';
echo '<input type=hidden name=mint_mark[] value="' . $datarow['mint_mark'] . '">';
echo '<input type=hidden name=denomination[] value=" ' . $datarow['denomination'] . '">';

on the receiving end we go thru the arrays like this

$id = $_POST['Record_id'];

                      for ($i = 0; $i < sizeof($id); $i++) {
                       

$year = $_POST['year'][$i];
$mint_mark = $_POST['mint_mark'][$i];
$denomination = $_POST['denomination'][$i];

This work well except with radio button, I have

echo "   <td><input type=radio name=grade[] value=G_4 size=2></td>";
echo "   <td><input type=radio name=grade[] value=VG_8 size=2></td>";
echo "   <td><input type=radio name=grade[] value=F_12 size=2></td>";
echo "   <td><input type=radio name=grade[] value=VF_20 size=2></td>";
echo '   <td><input type=radio name=grade[] value="EF 40" size=2></td>';
echo "   <td><input type=radio name=grade[] value=AU_50 size=2></td>";
echo "   <td><input type=radio name=grade[] value=MS_60 size=2></td>";
echo "   <td><input type=radio name=grade[] value=MS_63 size=2></td>";
echo "   <td><input type=radio name=grade[] value=MS_65 size=2></td>";
echo "   <td><input type=radio name=grade[] value=MS_67 size=2></td>";
echo "   <td><input type=radio name=grade[] value=PL_65 size=2></td>";
echo "   <td><input type=radio name=grade[] value=PRF_65 size=2></td></tr>";

and on the receiving end I have

$grade = $_POST['grade'][$i];

which give me a notice error and I get only the last selection entered in the db

Notice: Undefined offset: 1

What is wrong with this.
0
Comment
Question by:gamebits
  • 5
  • 5
10 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 20388370
You shouldn't be passing values as arrays unless you have a need to pass multiple values, as in a multiple select list. Radio buttons with the same name are designed to only submit a single value out of all the ones chosen. If you need multiple selections, consider switching to input type checkbox.
0
 
LVL 28

Author Comment

by:gamebits
ID: 20388406
I see your point, my problem is I need to pass multiple values, let me explain

I have a form where the user select a certain number of coins to add to his database, this bring another page where he can add some details to the coins he previously selected (like date purchased, price, grade etc.) so basically the same form is displayed n number of time.

So let say the form is displayed 5 times I need the user to select a grade (and only one grade) for each coin, hence the use of radio button if I use checkboxes than the user would be able to select more than one grade for each coin (I know it wouldn't make sense to do that but I'm trying to build the application fool proof) a lot of coin collectors are older people and getting them to use computer is a challenge by itself.
0
 
LVL 36

Accepted Solution

by:
Zyloch earned 500 total points
ID: 20388428
I do not know if there is a simple HTML workaround. Instead, you can use a counter to give each set of radio buttons a name along the lines of grade##

Then, in parsing the form, use a while loop to check the existence of $_POST['grade' . $i] where $i is your counter. You may want to use this convention with other fields associated with each set of radio buttons as well.
0
 
LVL 28

Author Comment

by:gamebits
ID: 20388437
May I ask how you would set a counter to do that?
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 20388452
How are you printing out your form?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 28

Author Comment

by:gamebits
ID: 20388488
I use

$id = $_POST['Record_id'];

                      for ($i = 0; $i < sizeof($id); $i++) {

                                                    //the form

                                }

so depending of Record_id I know how many time to display the form this information come from the previous page where the user select from a table (using checkbox) which coins he wants to add
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 20388495
If I understand correctly, each iteration of the for loop is a self-contained unit. If that's the case, you can just use $i as the counter. (Is $_POST['Record_id'][$i] the number of radio buttons to display?) Sorry, I'm a little confused.
0
 
LVL 28

Author Comment

by:gamebits
ID: 20388550
May be a quick look at the website will help

http://www.worldcoinsdatabase.com/NEW_WCDB/topmenuswitcher.php

From the left menu click Search Database -> Canada -> Circulation Coins (1870 - present)

Choose a denomination than Query Database

Tick a few checkboxes and click the Add It button.
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 20388569
I think I understand. Use $i as the counter and append it to the name of each form element as you echo it out.
0
 
LVL 28

Author Comment

by:gamebits
ID: 20388664
Thank You.

Gamebits
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now