Multiple Radio buttions from database


#1

I have this project i am working on for myself and it involves using multiple radio buttions that have 3 radio buttion selections,which are to-do,completed and not-recquired only problem is when i go to view my radio buttions online even thoguh i put my data from database in an array using the id Tasks[] i get all entries being selected i mean for example i have 3 entries

which are

To-Do Completed Not Required Gathering of information related to site content
To-Do Completed Not Required Website Domain Registered or have the name server records updated
To-Do Completed Not Required Client Answered Questionnaire Provided.
To-Do Completed Not Required lient Development Site Setup.

but if i select on gather information to site content and select not required and then go to next line to get domain register or name servers update the selection from the above gets deleted and erased and i want multiple rows to be different so as i go through them i dont have to worry about one row being removed when i select the next one.

	if(isset($_POST['save']))
	{
		$TaskArr=$_POST['Task'];
		print_r($_POST);
		$newvalue = implode(",",$TaskArr);
		echo $newvalue;	
	}
	include("dbconnect.php");
	$cQuery="SELECT * FROM Tasks WHERE TaskGroup='1'";
	$rs=mysqli_query($con,$cQuery);
	if(!$rs)
	{
		echo "Error:".mysqli_error($con);
	}
	else
	{
		$count=$rs->num_rows;
		if($count>0)
		{
			?><form method="post" action=""><div id="Workflow"><?
			while($data=mysqli_fetch_array($rs))
			{
				?>
				<div class="WorkflowRow">
					<div class="WorkFlowToDo">
						<label>
						<div class="WorkFlowToDoTitle"><input type="radio" name="Task[]" value="To-Do" />To-Do</div></label>
					</div> 
					<div class="WorkFlowCompleted">
						<div class="WorkFlowCompletedTitle">
						<label>
						<input type="radio" name="Task[]" value="Completed" />Completed</div></label>
					</div>
					<div class="WorkFlowNotRequired">
						<div class="WorkFlowNotRequiredTitle">
						<label>
						<input type="radio" name="Task[]" value="Not Required" />Not Required</div></label>
					</div>        
						<div class="WorkFlowDesc"><? echo $data['TaskDesc'];?></div>
				</div>
				<?	
			}
			?>
			<input type="submit" name="save" value="Save Tasks" />
			</div></form>
			<?
		}
	}

am i doing somthing wrong to get that problem? or is there somthing missing that i should of added?


#2

Hi @william232,

I think the problem is that the name attribute should go without the square brackets for radio inputs. Square brackets are for checkboxes since they can be formed of multiple values whereas the radios only support one value.
Also not sure about the capitalisation, at least I know by the standard you should have your name attributes in lowercase.

Try the following for all the radios and I believe you should be fine:

<input type="radio" name="task" value="Completed" />Completed</div></label>

Hope it helps,

Andres


#3

Off Topic:

Your HTML is invalid. A <label> element cannot contain a <div>. You also have elements incorrectly nested.


#4

Yes, I think that is the problem.

You can use the brackets on various input types, but only when you want an array. But with radios I think it causes confusion.
Because the name values appear identical, the browser treats them as a radio group.
But as far as the post data and php is concerned the resulting keys are Task[0], Task[1] and Task[2] which are separate entities within an array.


#5

I miss-read the problem and invented a reason for this wrong problem. I assumed this was a problem relating to processing the returned post data (being in the php forum), but this is actually an html problem.

The cause is still the same though. All your radio inputs have the same name value, so a treated as one radio group from which you may select only one.
To have them in groups of three, you will need each group to have its own unique name.
For this you can use arrays in the name, for example putting the row ID into the key or using a counter to increment the keys for each row.
Eg:

<div class="WorkflowRow">
	<div class="WorkFlowToDo">
		<label>
		<div class="WorkFlowToDoTitle"><input type="radio" name="Task[<?= $data['id'] ?>]" value="To-Do" />To-Do</div></label>
	</div> 
	<div class="WorkFlowCompleted">
		<div class="WorkFlowCompletedTitle">
		<label>
		<input type="radio" name="Task[<?= $data['id'] ?>]" value="Completed" />Completed</div></label>
	</div>
	<div class="WorkFlowNotRequired">
		<div class="WorkFlowNotRequiredTitle">
		<label>
		<input type="radio" name="Task[<?= $data['id'] ?>]" value="Not Required" />Not Required</div></label>
	</div>        
		<div class="WorkFlowDesc"><?= htmlspecialchars($data['TaskDesc']) ?></div>
</div>

#6

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.