Welcome to WebmasterWorld Guest from 54.160.254.203

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Simple Radio Button

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

New User

5+ Year Member

joined:Oct 8, 2007
posts:15
votes: 0


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)

Administrator

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

joined:July 24, 2001
posts:15755
votes: 0


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)

Senior Member

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

joined:Mar 30, 2003
posts:3719
votes: 0


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

dc

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

New User

5+ Year Member

joined:Oct 8, 2007
posts:15
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:July 12, 2007
posts:766
votes: 0


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)

New User

5+ Year Member

joined:Oct 8, 2007
posts:15
votes: 0


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)

Administrator

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

joined:July 24, 2001
posts:15755
votes: 0


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)

New User

5+ Year Member

joined:Oct 8, 2007
posts:15
votes: 0


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)

Administrator

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

joined:July 24, 2001
posts:15755
votes: 0


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)

New User

5+ Year Member

joined:Oct 8, 2007
posts:15
votes: 0


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)

Senior Member

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

joined:Dec 9, 2003
posts:3416
votes: 0


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)

New User

5+ Year Member

joined:Oct 8, 2007
posts:15
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 16, 2003
posts:897
votes: 0


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)

New User

5+ Year Member

joined:Oct 8, 2007
posts:15
votes: 0


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

New User

5+ Year Member

joined:Oct 8, 2007
posts:15
votes: 0


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

Junior Member

5+ Year Member

joined:Mar 31, 2007
posts:85
votes: 0


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)

New User

5+ Year Member

joined:Oct 8, 2007
posts:15
votes: 0


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)

Administrator

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

joined:July 24, 2001
posts:15755
votes: 0


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

New User

5+ Year Member

joined:Oct 8, 2007
posts:15
votes: 0


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.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members