...

View Full Version : getting values from radio buttons



sybil6
06-22-2010, 03:46 PM
hello
i have the following html


<input type='radio' value='3500' name='montant' class='radio_montant' id='35'>
</td>
<td>
<input type='radio' value='4000' name='montant' class='radio_montant' id='40' checked >
</td>
<td>
<input type='radio' value='6000' name='montant' class='radio_montant' id='60'>
</td>
<td >
<input type='radio' value='8000' name='montant' class='radio_montant' id='80'>
</td>

in this html the user can choose how much money he wants to contribute, but how can tell php if the user choose value 40000 for example to add this value to a variable in my script, then if the user chooses another amount add that amount to the variable?
regards

Keleth
06-22-2010, 03:56 PM
I assume by 40,000 you meant 4,000 as you don't have 40k in your code. And as a small note, you should use

checked="checked"
over just checked.

Are you sending the variables by POST or GET?

If you're sending by POST, all forum variables are stored in $_POST as an array by their name (in this case montant) and if by get, they're stored in $_GET.

sybil6
06-22-2010, 04:05 PM
hi, thanks for the checked thing, yes i am sending thru post , i still dont get how to acces the first "montant" radio , then the second etc..
im tring this :

if(isset($_POST["montant"][0])){
$amount+=5;
}
but does not work..

DJCMBear
06-22-2010, 04:10 PM
you have to use string[] for the name to send the numbers over to php.

sybil6
06-22-2010, 05:10 PM
It looks it is not possible to do this with PHP, can someone help me?
regards

mlseim
06-22-2010, 05:17 PM
With radio buttons, it does not involve the array ...

$value=$_POST['montant'];
$value=$value + 5;
echo $value;

Morri
06-22-2010, 05:18 PM
hey,

try

echo $_REQUEST['montant'];you can replace _REQUEST with _GET or _POST, because it is faster

Morri

PS: mlseim (http://www.codingforums.com/member.php?u=8270) was faster ;)

sybil6
06-22-2010, 05:21 PM
With radio buttons, it does not involve the array ...

$value=$_POST['montant'];
$value=$value + 5;
echo $value;
yes but i need to select different values because they are multiple radio button with different values.

mlseim
06-22-2010, 05:23 PM
If you name your radio buttons the same ... name='montant',
only one of them can be selected at any time.

Maybe you want to use checkboxes instead?

sybil6
06-22-2010, 05:26 PM
If you name your radio buttons the same ... name='montant',
only one of them can be selected at any time.

Maybe you want to use checkboxes instead?
ok thanks , now i see this is not possible with PHP , if i want to make it i need to put different names in radio buttons and mimic the multiple radio buttons behavior with javascript.right?

sybil6
06-22-2010, 05:30 PM
you have to use string[] for the name to send the numbers over to php.
Really? how so?

mlseim
06-22-2010, 05:31 PM
You basically want the "aesthetics" of the radio button (how it looks),
but not necessarily the "action" (only selecting one of several).
So you are correct ... give each button their own name and they will act separately.

It's within the PHP form processing that you will then need to deal with that.

That's why I mentioned checkboxes, because you can create an array of them.

Maybe you can use some CSS styling to change how the checkbox looks?


EDIT:
I've never tried this ... not sure what will happen ...

change
name='montant'

to
name='montant[]'

See if it treats the radio buttons as separate entities, and yet utilizing the array.



.

sybil6
06-22-2010, 05:48 PM
You basically want the "aesthetics" of the radio button (how it looks),
but not necessarily the "action" (only selecting one of several).
So you are correct ... give each button their own name and they will act separately.

It's within the PHP form processing that you will then need to deal with that.

That's why I mentioned checkboxes, because you can create an array of them.

Maybe you can use some CSS styling to change how the checkbox looks?


EDIT:
I've never tried this ... not sure what will happen ...

change
name='montant'

to
name='montant[]'

See if it treats the radio buttons as separate entities, and yet utilizing the array.



.

nop i need to mimic the action of radio buttons so this is why i would need JS, but i have seen this done with PHP :confused:

Keleth
06-22-2010, 08:19 PM
hey,

try

echo $_REQUEST['montant'];you can replace _REQUEST with _GET or _POST, because it is faster

Morri

PS: mlseim (http://www.codingforums.com/member.php?u=8270) was faster ;)

Frankly, don't use _REQUEST, as its notoriously insecure. If you know you're getting POST variables, only accept POST. If you're using GET, only accept GET. If someone wants an easy backdoor to your application, they'll try to get past variables like that first.


nop i need to mimic the action of radio buttons so this is why i would need JS, but i have seen this done with PHP :confused:

PHP can't do anything HTML can't do. PHP is a server side processing language that will feed HTML to a user. There might be HTML hacks to do something like what you want, but I'd stick to JS with backup code in case someone doesn't have JS active.

I'm just confused on what you're trying to do.

You have a series of radio buttons... but you want multiple ones selected? If that's what you want, you want check boxes, not radio buttons. If you only want one selected, then you're find and want to check it with $_POST['montant'].

mlseim
06-22-2010, 09:10 PM
Keleth,
I didn't understand it either, which is why I thought maybe
the original poster wanted something to look like radio buttons,
but act like checkboxes ... that apparently isn't the case.

So, I guess we'll have to wait and see what they come up with
in regards to Javascripting ... as I don't understand.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum