You are here: Dynamic Radio Buttons « PHP Programming Language « IT news, forums, messages
Dynamic Radio Buttons

Posted by Jerim79 on 04/06/07 13:36

My situation is that I have a form that asks the user for a number.
Next, I execute a while loop that displays a group of questions the
amount of times the customer entered. For instance, the loop looks
this:

while ($Number!=0){
<input type="radio" name="Age[]" value="20-30">20-30
<input type="radio name="Age[]" value="30-40">30-40
<input type="radio name"Age[]" value="40-50">40-50
$Number--;
}

Let's say someone entered 3 for $Number. When the loop executes it
will produce:

echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
<input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";

echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
<input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";

echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
<input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";

The problem is that with radio buttons, you can only choose one
option. With this code, it only allows the user to choose one option
from among the 9 listed, instead of one from each set. I am trying to
find a way to dynamically name each set. If I use checkboxes, then it
records the information correctly and correctly writes it to the
database. However, checkboxes don't look good for this application in
my opinion, and there is no way for me to keep someone from checking
more than one box, that I know of. Here is the PHP code I am using to
catch the user's input and write it to a database:

$Age = $_POST['Age'];

for ($i=0; $i < $Number; $i++) {
$query="INSERT INTO table VALUES('$Age[$i])";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

Like I say, that works fine if I use checkboxes, without changing the
HTML name="Age[]". I am looking for a way to define "sets" of radio
buttons with the same name, so that only one from within a set can be
chosen at a time. Is there something such as:

echo "<group name="1">
<input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
<input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
</group>";

echo "<group name=\"2\">
<input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
<input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
</group>";

echo "<group name=\"3\">
<input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
<input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
</group>";

This may be more of a HTML question, but I am open to any solution
that would help. Any way to use PHP to accomplish my goal? Even if
there is a way to dynamically name each set such as:

echo "while ($Number!=0){
<input type=\"radio\" name=\"Age$Number[]\" value=\"20-30"\>20-30
<input type=\"radio\" name=\"Age$Number[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age$Number[]\" value=\"40-50\">40-50
$Number--
} ";

I would just have to create a loop to run through each array when
writing to the database, but that shouldn't be a problem.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация