Welcome to WebmasterWorld Guest from 54.157.222.62

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Simple Radio Button

   
3:47 pm on Oct 20, 2007 (gmt 0)

5+ Year Member



Hey guys,

I'm trying to create two simple php radio buttons but I'm not having any luck. I'm trying to create the two buttons so that only one choice can be made.

Since, the form I'm using has a specific format, I tried integrating the radio buttons in but when you click on the radio buttons, both selections can be made, and you can't "uncheck" the buttons once they have been made.

The choices are either a "Personal Need" or a "Business Need"

Any help is appreciated

$form['needs1'] = array(
'#type' => 'radio',
'#title' => t('Personal Need'),
'#size' => 20,
'#value' => 0,
'#values' => array( 0 => 'no', 1 => 'yes'),
'#required' => TRUE
);

$form['needs2'] = array(
'#type' => 'radio',
'#title' => t('Business Need'),
'#size' => 20,
'#value' => 0,
'#values' => array( 0 => 'no', 1 => 'yes'),
'#required' => TRUE
);

3:49 pm on Oct 20, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



have you viewed source to see if the html is correct. If you can select both then they aren't being created properly.

my wild guess is that the title (in the above arrays) needs to be the same

5:26 pm on Oct 20, 2007 (gmt 0)

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member



If you want check/uncheck options, shouldn`t you be thinking of a checkbox?

dc

11:48 pm on Oct 21, 2007 (gmt 0)

5+ Year Member



I meant to say, if someone makes a mistake clicking for example "Personal Need", they need to be able to unclick the radio button and the green dot to go away.
9:47 am on Oct 22, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Sounds like what dreamcatcher was talking about.
A radio button is not an on/off, its a select 1 from the following.
So if you want an on/off you want a check box, then they can say yes or no. Based on there response to the check box you can then offer other questions to them.
2:36 pm on Oct 22, 2007 (gmt 0)

5+ Year Member



I still don't think you guys are understanding what i'm trying to do. I want to be able to have the person at the form choose one or the other (NOT BOTH). So if they choose option #1, but they really want option #2, they should be able to click option #2 and the green dot on #1 to go away. Currently, with the code I have, it lets you choose both options which I don't want.
3:17 pm on Oct 22, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



that was what I understood from your original post. Did you check the source as I mentioned.

You could post the html from those 2 radio buttons here

7:57 pm on Oct 22, 2007 (gmt 0)

5+ Year Member



There is no html for the radio buttons since I'm not sure where it would go in the .php file.
11:31 pm on Oct 22, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



what I mean is to view the form in your browser and then view the source of the output page.

If you look at the html then you might be able to better figure out what the PHP is doing and figure out what you did wrong.

7:07 pm on Oct 23, 2007 (gmt 0)

5+ Year Member



Here is the html output as requested..Thanks

<div class="form-item">
<label class="option"><input type="radio" name="needs1" value="" class="form-radio required" /> Personal Need</label>
</div>
<div class="form-item">
<label class="option"><input type="radio" name="needs2" value="" class="form-radio required" /> Business Need</label>
</div>

7:13 pm on Oct 23, 2007 (gmt 0)

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The name of radio buttons need to match for them to "know" who their companions are:

<div class="form-item"> 
<label class="option"><input type="radio" name="needs" value="1" class="form-radio required" /> Personal Need</label>
</div>
<div class="form-item">
<label class="option"><input type="radio" name="needs" value="2" class="form-radio required" /> Business Need</label>
</div>
7:24 pm on Oct 23, 2007 (gmt 0)

5+ Year Member



When I change both $form['needs1'] and $form['needs2'] in the php code to $form['needs'] the only option that shows up now on the form is "Business Need".

In the output, the value still shows up with neither a 1 or 2 even after changing it to a 1 or 2 in the php.

<div class="form-item">
<label class="option"><input type="radio" name="needs" value="" class="form-radio required" /> Business Need</label>
</div>

8:18 pm on Oct 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not exactly familiar with how your method works to create your form but could you do something like this:

$form['needs'] = array(
('#type' => 'radio',
'#title' => t('Personal Need'),
'#size' => 20,
'#value' => 0,
'#values' => array( 0 => 'no', 1 => 'yes'),
'#required' => TRUE ),
('#type' => 'radio',
'#title' => t('Business Need'),
'#size' => 20,
'#value' => 0,
'#values' => array( 0 => 'no', 1 => 'yes'),
'#required' => TRUE)
);

8:41 pm on Oct 23, 2007 (gmt 0)

5+ Year Member



Hi, thanks for the info. I tried what you posted above, but the page breaks.
5:48 pm on Oct 26, 2007 (gmt 0)

5+ Year Member



Is there another way to do this? Thanks
7:17 pm on Oct 27, 2007 (gmt 0)

5+ Year Member



Here's the thing, the two PHP statements you've posted don't really generate any HTML code by themselves, they simply assign values to the $form array, which i presume gets used later on to generate the HTML code for the form. It looks like you're using some type of API (i'm guessing as part of a CMS like Drupal) and without telling us which one, we can't help you much. So... you need to read the API's documentation more carefully and figure out how to define the $form entry for radio buttons correctly. You may also find help more easily on the particular API/CMS's message board.

I'm going to take a stab at it and give you this code:

$form['needs'] = array(
'#type' => 'radio',
'#title' => t('What is this for?'),
'#value' => 'p',
'#values' => array( 'p' => 'Personal Need', 'b' => 'Business Need'),
'#required' => TRUE
);

But your best bet is to read the API's documentation on creating radio buttons.

9:06 pm on Oct 27, 2007 (gmt 0)

5+ Year Member



Hi, yes this is for Drupal.

When i added the code you posted, it only gave me one radio button.

I need to have two buttons with obviously the Personal Need or Business Need.

Thanks

1:42 pm on Oct 28, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



who made the script? don't they have docs available?
3:20 pm on Oct 29, 2007 (gmt 0)

5+ Year Member



It's a custom script fitted around Drupal's code. Drupal has API documentation on radio buttons but it isn't helping much at all.