Using radio button in echo

Hey guys so I have this table that is populated by data from database uaing mysql. There are 4 columns: name, unconfirmed, atttending, not attending. The last 3 columns are radio buttons. All the columns in the table are echoed even the radio buttons.

Issue: say there r two rows meaning two names and for the first row attending is clicked and for the second row attending is clicked but then the radio button on the first row is unclicked (like it moved from first row to second row) and well thats not very friendly. This http://jsfiddle.net/kFCnC/ is what it would be like but my rado buttons are echoed. How would I accomplish smtg like that?


<tbody>
<?php
require "connection.php";
$check = mysql_query ("SELECT * FROM contact");
If (mysql_num_rows ($check) > 0)
{
  while ($row = mysql_fetch_array ($check))
   {
    $salutation = $row['salutation'];
    $name = $row ['primary_name'];

     echo "<tr><td> $salutation $name </td><td><input type='radio' name='rsvp'></td><td><input type='radio' name='rsvp'></td><td><input type='radio' name='rsvp'></td></tr>
?>
</tbody>

Not entirely sure what you’re asking, if the question is how to show the current setting then my pseudo-code would be something like this, assuming we’ve got a column called ‘status’ to show whether person is unconfirmed, attending or not attending:


$salutation = $row['salutation'];
$name = $row['primary_name'];
$v1 = '';
$v2 = '';
$v3 = '';
switch($row['status']) {
  case 0: // or whatever value denotes unconfirmed
    $v1 = 'checked';
    break;
  case 1: // attending
    $v2 = 'checked';
    break;
  case 2: // not attending
    $v3 = 'checked';
    break;
  }
 echo "<tr><td> $salutation $name </td><td><input type='radio' name='rsvp' $v1></td><td><input type='radio' name='rsvp' $v2></td><td><input type='radio' name='rsvp' $v3></td></tr>

Obviously there are more elegant ways, probably using an array. If that wasn’t the question, please expand.

The trouble with using radio buttons to display a status like this is that the browser will allow the user to change them, so they’ll expect to be able to save whatever changes they make, or get confused. If you’re intending to allow saving, you’ll have to wrap it in one or more forms, and if it’s a single form you’ll need to have a way to differentiate between the users (rows) for the status update.

Oh, and have a look at mysqli or PDO instead the older mysql functions.

Thanks for the response. Im actually using my phone to ask these questions coz theres no wifi where im at. So copy pasting my code is difficult at the moment. I had to type the ones in the question out. Sorry my question isnt so clear. Im not really good at explaining things.
Like was said I have a table with radio buttons and the user inputs these by clicking on the radio button. Take for example the jsfiddle in the question, its like a survey where user rates the chocolates between 1-3 and the user can rate all chocolates a 3 easy coz of how the html is structured (radio button has respective name and data-col). what im trying to do is something like that where user can click that a person is either unconfirmed, attending or not attending and for example sake say there are 3 names(nadia, noreen, lilo) in my table and all 3 are not attwnding the user would need to click on not attending for all three names but I m not able to accomplish that because my radio buttons are echoed and all have the same name so for example if name: nadia is clicked not attending and then when user wants to click unattending for name: noreen they can but then the previously clicked radio button for nadia is unclicked (unchecked ?) Thats the issue im facing. I hope that explaination is good.

As droopsnoot already mentioned, it would be better to have a single column named status to handle this with values( 0, 1 or 2). If you wish you could store full named values (“unconfirmed”, “attending”, “not attending”) and modify the $status == $v on the $checked line and the checkbox value value=‘" . $v . "’. Also mentioned was wrapping these inputs in a form, and the need to identify row. By adding after the name rsvp, this name becomes an array for each loop in the form and would need to be handed as an array when processing. Because we need a record ID for making update, it is easiest to add this inside those . Here’s a sample.

<?php
require "connection.php";

// Processing
if(isset($_POST['submit'])){
	foreach($_POST['rsvp'] as $id => $value):
		$id    = mysql_real_escape_string($id);
		$value = mysql_real_escape_string($value);
		$sql = "UPDATE contact SET status = '$value' WHERE id = '$id'";
		//testing
		//echo $sql;
		mysql_query($sql);
	endforeach;
}
$display = "";
$check = mysql_query ("SELECT * FROM contact");
if (mysql_num_rows($check) > 0)
{
	while ($row = mysql_fetch_array ($check))
	{
		$id = $row['id'];
		$salutation = $row['salutation'];
		$name = $row ['primary_name'];
		$status = $row ['status'];
		
		$status_array = array("unconfirmed", "attending", "not attending");
		$display .="<tr>
		<td> $salutation $name </td>\\r";
		foreach($status_array as $k => $v):
		
			$checked = ($status == $k ? ' checked="checked"' : '');	 
			$display .="<td><input type='radio' name='rsvp[" . $id . "]' value='" . $k . "'" . $checked . " />" . $v . "</td>\\r"; 
		
		endforeach;
		$display .="</tr>\\r";
	}
}	 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
	<form action="" method="post">
		<table>
			<tbody>
			<?php
			echo $display;	 
			?>
			<tr>
				<td colspan="4"><input type="submit" name="submit" value="Update" /></td>
			</tr>
			</tbody>
		</table>
	</form>
</body>
</html>

I recommend learning and using PDO as a connection method.

By adding after the name rsvp, this name becomes an array for each loop in the form and would need to be handed as an array when processing. Because we need a record ID for making update, it is easiest to add this inside those


echo
"<tr>
<td>$salutation $name</td><td>Unconfirmed<input type='radio' name='rsvp[1]' class='radio' data-col='1' value='unconfirmed'></td><td>Attending<input type='radio' name='rsvp[2]' class='rad' data-col='2' value='attend'></td><td>Not Attending<input type='radio' name='rsvp[3]' class='radi' data-col='3' value='notattend'></td>
</tr>";

name=‘rsvp[1]’, name=‘rsvp[2]’, name=‘rsvp[3]’

is that what you meant?

oh and this is what the table looks like:

issue:
according to the picture the first name : Xaviera was clicked unconfirmed and second name : Noor Asma i wanted to click unconfirmed but then if i did then the unconfirmed for Xaviera would disappear(as in the choice moved from xaviera to asma). ALSO if u notice first name Xaviera has both unconfirmed and not attending clicked and well, this isn’t good.

so if i can’t correctly choose the radio button there’s no point in trying to save the choices in the first place.

ps: i apologize if this is the wrong forum to post this sort of question. should i have posted it in javascript forum?

Did you look at the code I posted? The number I put within the brackets is the record ID number so you would have an ID for making the update. So all the rsvp numbers would be the same in a given loop for the radio button set. Just to note, if you left those brackets empty, you would still have a unique array of values on POST with numeric keys, for example

rsvp[0] =>  'unconfirmed',
rsvp[1] =>  'attend',
rsvp[2] =>  'notattend',
rsvp[3] =>  'unconfirmed'

The problem remains in not having an ID to update records. The best approach is to add the ID number.

AN alternative would be to leave the rsvp blank and add a hidden input field to pass the ID. It would also need to be built as an array, e.g. name=“record_number
Processing would need to be adjusted for this.

oh… right. okay. sorry drummin. i saw the code but in a small screen i couldn’t decipher it properly. ill come back soon when i get internet to my laptop. sorry for the stupidity.

i did see theres a “.” next to “=” whats the dot for? i dont know how to phrase that question in google.

That’s “concatenation”

oh, thank you. i think thats the first ive seen/heard it.

Hi drummin. so i got to working with the coding you had posted last night. i learned some new things thank you :slight_smile:

Hi guys, coming back to this, was busy with other parts of this project. is it possible to count which and how many radio buttons are checked with how the is structured?

Post your current code and are you talking about in the form or the processing section?

this is the table:


<tbody>
   <table class="rsvp-table">
      <thead>
         <tr>
            <th>Guests</th>
	    <th>Status</th>
	</tr>
     </thead>
     <tbody id="rsvp-body">

     </tbody>
   </table>
</tbody>

which is populated by the names and the radio buttons using ajax to call the names so this is the php file:


<?php
    $t = $_GET['t'];

    require "connection.php";
    $check = $dbh->prepare("SELECT guest_id,guest_name,status FROM guest WHERE event_id = :t ");
    $check->bindParam(':t', $t);
    $check->execute();
    if ($check->rowCount() > 0)
    {
    	$check->setFetchMode(PDO::FETCH_ASSOC);
        while ($row = $check->fetch())
        {
            $id = $row['guest_id'];
            $name = $row['guest_name'];
            $status = $row['status'];
            $status_array = array("unconfirmed", "attending", "not attending");

        echo "<tr>";
        echo "<td>$name</td>";
        foreach($status_array as $k => $v):

            $checked = ($status == $k ? ' checked="checked"' : '');
            echo "<td><input type='radio' name='rsvp[" . $id . "]' value='" . $k . "'" . $checked . " />" . $v . "</td>";

        endforeach;
        echo "</tr>";
        }
    }
    else
    {
    	echo "<tbody><tr><td>No Guests invited.</td><td></td></tr>";
    }
?>

what i was thinking is having these text boxes at the bottom of the table and it auto counts when a radio button is checked using js, something like this:


<label>Total Guests : </label><input type="text" name="total-guest" readonly>
<label>Unconfirmed : </label><input type="text" name="unconfirmed" readonly>
<label>Attending : </label><input type="text" name="attending" readonly>
<label>Not Atending : </label><input type="text" name="not-attending" readonly>

and i guess maybe those could be saved as record

what do u think @Drummin?

Untested but I think you could do something like this, incrementing the count for each status as you go through the loop.

<?php
 require "connection.php";
    $check = $dbh->prepare("SELECT guest_id,guest_name,status FROM guest WHERE event_id = :t ");
    $check->bindParam(':t', $t);
    $check->execute();
    if ($check->rowCount() > 0)
    {
    	$check->setFetchMode(PDO::FETCH_ASSOC);
		$Guests = 0;
		$Unconfirmed = 0;
		$Attending = 0;
		$NotAttending = 0;
        while ($row = $check->fetch())
        {
            $id = $row['guest_id'];
            $name = $row['guest_name'];
            $status = $row['status'];
            $status_array = array("unconfirmed", "attending", "not attending");
			
			//Get counts
			$Guests++;
			switch ($status) {
			case 0:
				$Unconfirmed++;
			break;
			case 1:
				$Attending++;
			break;
			case 2:
				$NotAttending++;
			break;
			}
        
        echo "<tr>";
        echo "<td>$name</td>";
        foreach($status_array as $k => $v):
        
            $checked = ($status == $k ? ' checked="checked"' : '');     
            echo "<td><input type='radio' name='rsvp[" . $id . "]' value='" . $k . "'" . $checked . " />" . $v . "</td>"; 
        
        endforeach;
        echo "</tr>";
        }
		echo "<tr><td><label>Total Guests : </label></td><td><input type='text' name='total-guest' value='" . $Guests . "' readonly></td></tr>
		<tr><td><label>Unconfirmed : </label></td><td><input type='text' name='unconfirmed' value='" . $Unconfirmed . "' readonly></td></tr>
		<tr><td><label>Attending : </label></td><td><input type='text' name='attending' value='" . $Attending . "' readonly></td></tr>
		<tr><td><label>Not Atending : </label></td><td><input type='text' name='not-attending' value='" . $NotAttending . "' readonly></td></tr>\\r";
    }
    else
    {
    	echo "<tbody><tr><td>No Guests invited.</td><td></td></tr>";
    }
?>

NOTE: Good to see you switched to PDO!!!

what does this part mean?


        $Guests = 0;
        $Unconfirmed = 0;
        $Attending = 0;
        $NotAttending = 0;

They are outside the while.

Do you not want the incrementing to begin from 0?

You set the variables to a number then increment with ++ inside the loop for matching values.

will that effect the value of the radio buttons? as in say there are two guests for Event A: Alex and Piper. previously Alex was already clicked for attending while Piper is still unconfirmed, so when next time the user wants to lookup the rsvp for Event A alex’s status is still attending and piper is still unconfirmed. will that declaration $NotAttending = 0 and so on effect the status of the guests? oh and TIA for all the help @Drummin

Depending on the results returned from
$check = $dbh->prepare("SELECT guest_id,guest_name,status FROM guest WHERE event_id = :t ");
the code puts together an array.
Can you use that?