Forums

radio button with input value "Other"

samoht 04 Dec, 2008
hello,

Quick question:
How do I set up an array of radio buttons the last of which will be a input from the user (usually named "Other") ?
GreyHead 04 Dec, 2008
Hi samoht,

You set up the array of radio buttons and add an extra text input field for the 'other' entry. It's a design question whether you include an 'other' button in the array - you will need to if you are setting any default values (having a button pre-checked).

You then need to handle the display logic in the OnSubmit code - that is looking to see if there is an 'other' value entered and using that in your database or email.

Bob
samoht 04 Dec, 2008
Hi Bob,

Currently my array looks like:
	<tr>
		<td  colspan="2" class="noborders">
		<?php 
		// set checked field(s) for checkboxes
		$amounts = array(50,75,100,125,150,200,250,300,500,750,1000); 
		foreach ($amounts as $amount) {
			echo '<span class="cb"><input type="radio" name="checkbox[]" onclick="javascript:document.ChronoContact_newgc.otheramount.disabled=true" value="'.$amount.'" style="border:0;" ';
			if (is_array($_POST['checkbox']) && in_array($amount,$_POST['checkbox']))
				echo 'checked="checked" '; 
			elseif ( !$edit ) 
				echo 'disabled="disabled"'; 
			echo ' /> $'.$amount.'</span>'."\n";
		} 
		?> 
		</td>
	</tr>
	<tr>
		<td  colspan="2"><span>Other Amount:  $</span>
		<input name="otheramount" type="text" value="<?php if(isset($_POST['otheramount'])) echo $_POST['otheramount'];?>" size="4"  <?php echo $readonly;?> onclick="javascript:document.ChronoContact_newgc.checkbox[].disabled=true" />
		<span>.00</span><br />
		<br />
		</td>
	</tr>


But I want to do as you say and add "Other: as my last value but then where would I put the input ??

Also, is it in the On Submit that I put the code to make the input text only accessible when the "Other" radio is active??
Max_admin 05 Dec, 2008
Hi samoht,

you will need a new field in the DB table for the other field, is this what you mean ?

Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 06 Dec, 2008
Hi samoht,

How you add the extra field is up to you. Most often designers add an extra text input with a label that says ' If other, please give details'.

You could add some clever JavaScript so that this field only displays once 'other' is selected; but it probably isn't necessary.

You also have to decide how to deal with the two fields in the back-end. You can simply keep them as two separate fields in the database table, or you could use some PHP to merge them. If you will ever need to re-display the form for editing then I'd keep all the raw data i.e. use two fields; otherwise you will have to write code to undo the merged values to re-create the form data.

Bob
samoht 06 Dec, 2008
Thanks guys!

You put me on the right track for sure.
I decided not to merge the two fields but just to have a separate text field for storage reasons. I made the calculation simplified by conditionals on the text field rather than the radio's. I also threw in some js to disable and clear the text field if any other radio was picked + code to check the "Other" radio if the text field has focus. It seems pretty foolproof and it stores and calculates well.

Thanks again!
This topic is locked and no more replies can be posted.