homepage Welcome to WebmasterWorld Guest from 54.197.211.197
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Simple Radio Button
sfg789

5+ Year Member



 
Msg#: 3482753 posted 3:47 pm on Oct 20, 2007 (gmt 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
);

 

jatar_k

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



 
Msg#: 3482753 posted 3:49 pm on Oct 20, 2007 (gmt 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

dreamcatcher

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



 
Msg#: 3482753 posted 5:26 pm on Oct 20, 2007 (gmt 0)

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

dc

sfg789

5+ Year Member



 
Msg#: 3482753 posted 11:48 pm on Oct 21, 2007 (gmt 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.

PHP_Chimp

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3482753 posted 9:47 am on Oct 22, 2007 (gmt 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.

sfg789

5+ Year Member



 
Msg#: 3482753 posted 2:36 pm on Oct 22, 2007 (gmt 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.

jatar_k

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



 
Msg#: 3482753 posted 3:17 pm on Oct 22, 2007 (gmt 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

sfg789

5+ Year Member



 
Msg#: 3482753 posted 7:57 pm on Oct 22, 2007 (gmt 0)

There is no html for the radio buttons since I'm not sure where it would go in the .php file.

jatar_k

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



 
Msg#: 3482753 posted 11:31 pm on Oct 22, 2007 (gmt 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.

sfg789

5+ Year Member



 
Msg#: 3482753 posted 7:07 pm on Oct 23, 2007 (gmt 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>

whoisgregg

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



 
Msg#: 3482753 posted 7:13 pm on Oct 23, 2007 (gmt 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>

sfg789

5+ Year Member



 
Msg#: 3482753 posted 7:24 pm on Oct 23, 2007 (gmt 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>

too much information

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3482753 posted 8:18 pm on Oct 23, 2007 (gmt 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)
);

sfg789

5+ Year Member



 
Msg#: 3482753 posted 8:41 pm on Oct 23, 2007 (gmt 0)

Hi, thanks for the info. I tried what you posted above, but the page breaks.

sfg789

5+ Year Member



 
Msg#: 3482753 posted 5:48 pm on Oct 26, 2007 (gmt 0)

Is there another way to do this? Thanks

borntobeweb

5+ Year Member



 
Msg#: 3482753 posted 7:17 pm on Oct 27, 2007 (gmt 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.

sfg789

5+ Year Member



 
Msg#: 3482753 posted 9:06 pm on Oct 27, 2007 (gmt 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

jatar_k

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



 
Msg#: 3482753 posted 1:42 pm on Oct 28, 2007 (gmt 0)

who made the script? don't they have docs available?

sfg789

5+ Year Member



 
Msg#: 3482753 posted 3:20 pm on Oct 29, 2007 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved