Jump to content

Default Selection radio button


snrecords

Recommended Posts

Hey again...

 

I have a table where users input a date, time, and quantity for a reservation.  On my page ... because I have a junk calendar and coding, every time you click a different month on the calendar or click the "book now" button without entering all the necessary data,  the page refreshes and erases all the data. 

 

One significant problem is that the times display as radio buttons.  Some tours have one time and some have multiple times.  On the page, if there is only one time, then to make it easier for users to book tours, I inputted CHECKED into the html code.  But unfortunately if the tour has multiple times, then CHECKED doesn't work for the first radio button, it defaults the last radio button because of the php loop.

 

My question is ... how do I always have the first radio button selected as default? 

 

Here's the code so far:

 

<?php
		$times = get_tour_times($tour_id);
//			var_dump($times);
		if (isset($times['0'])){
			echo '<font color="#5A3D1B" ><span style="margin-left: 22px;  font-size:10pt;line-height: 25px; font-family:Palatino Linotype; "><B style="margin-top: 40px;">TIME</B></SPAN></FONT><BR/>';

			foreach ($times as $time)
			{
				echo('<font color="#5A3D1B" ><span style="margin-left: 5px; line-height: 25px;  font-size:10pt; font-family:Palatino Linotype; ">');
				echo('<input type="radio" name="tour_time" value="' . $time[1] . '" Checked>');
				echo($time[0]);
				echo("</SPAN></FONT><BR/>");
			}				

		}else{
			echo '<input type="hidden" name="tour_time_ch" value="--">';
		}


	?>

Link to comment
Share on other sites

why don't you just exclude it from the for loop? just put that specific radio button above the for loop.

 

something like this maybe:

 

<?php
		$times = get_tour_times($tour_id);
//			var_dump($times);
		if (isset($times['0'])){
			echo '<font color="#5A3D1B" ><span style="margin-left: 22px;  font-size:10pt;line-height: 25px; font-family:Palatino Linotype; "><B style="margin-top: 40px;">TIME</B></SPAN></FONT><BR/>';
								echo '<font color="#5A3D1B" ><span style="margin-left: 5px; line-height: 25px;  font-size:10pt; font-family:Palatino Linotype; ">';
				echo '<input type="radio" name="tour_time" value="' . $time[0] . '" Checked>';
                                        echo "</SPAN></FONT>";
			foreach ($times as $time) {
				echo('<input type="radio" name="tour_time" value="' . $time[1] . '">');
				echo($time[0]);
				echo("</SPAN></FONT><BR/>");
			}				

		}else{
			echo '<input type="hidden" name="tour_time_ch" value="--">';
		}


	?>

Link to comment
Share on other sites

So many people forget about simple for loops  ;)

 

this code takes a arg called "tour" if it is not there it defaults to 5 times

 

<?php 
ob_start();
session_start();

function get_tour_times($tour_id){
switch ($tour_id) {
case 1:
	$temparray = array('08:00');
	return $temparray;
	break;
case 2:
	$temparray = array('08:00','10:00');
	return $temparray;
	break;
case 3:
	$temparray = array('08:00','10:00','13:00');
	return $temparray;
	break;
case 4:
	$temparray = array('08:00','10:00','13:00','15:00');
	return $temparray;
	break;

default:
	$temparray = array('08:00','10:00','13:00','15:00','18:00');
	return $temparray;
	break;
}

}

if (isset($_REQUEST['tour'])) {
$tour_id = $_REQUEST['tour'];
} else {
$tour_id = 0;
}

$times = get_tour_times($tour_id);
//			var_dump($times);
		if (isset($times['0'])){
			echo '<font color="#5A3D1B" ><span style="margin-left: 22px;  font-size:10pt;line-height: 25px; font-family:Palatino Linotype; "><B style="margin-top: 40px;">TIME</B></SPAN></FONT><BR/>';

			for ($i=0;$i < count($times);$i += 1)
			{
				$checkval = ($i == 0)?'checked':'';
				echo('<font color="#5A3D1B" ><span style="margin-left: 5px; line-height: 25px;  font-size:10pt; font-family:Palatino Linotype; ">');
				echo '<input type="radio" name="tour_time" value="' . $times[$i] . '" '.$checkval.'>';
				echo($times[$i]);
				echo("</SPAN></FONT><BR/>");

			}


		}else{
			echo '<input type="hidden" name="tour_time_ch" value="--">';
		}


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.