Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-22-2010, 02:46 PM   PM User | #1
sybil6
Regular Coder

 
Join Date: Jul 2006
Posts: 399
Thanks: 33
Thanked 7 Times in 7 Posts
sybil6 can only hope to improve
getting values from radio buttons

hello
i have the following html

Code:
<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

Last edited by sybil6; 06-22-2010 at 02:55 PM..
sybil6 is offline   Reply With Quote
Old 06-22-2010, 02:56 PM   PM User | #2
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
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
Code:
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.
Keleth is offline   Reply With Quote
Old 06-22-2010, 03:05 PM   PM User | #3
sybil6
Regular Coder

 
Join Date: Jul 2006
Posts: 399
Thanks: 33
Thanked 7 Times in 7 Posts
sybil6 can only hope to improve
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 :
Code:
 if(isset($_POST["montant"][0])){
   $amount+=5;
 }
but does not work..
sybil6 is offline   Reply With Quote
Old 06-22-2010, 03:10 PM   PM User | #4
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
you have to use string[] for the name to send the numbers over to php.
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 06-22-2010, 04:10 PM   PM User | #5
sybil6
Regular Coder

 
Join Date: Jul 2006
Posts: 399
Thanks: 33
Thanked 7 Times in 7 Posts
sybil6 can only hope to improve
It looks it is not possible to do this with PHP, can someone help me?
regards
sybil6 is offline   Reply With Quote
Old 06-22-2010, 04:17 PM   PM User | #6
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
With radio buttons, it does not involve the array ...

$value=$_POST['montant'];
$value=$value + 5;
echo $value;
mlseim is offline   Reply With Quote
Old 06-22-2010, 04:18 PM   PM User | #7
Morri
New Coder

 
Join Date: Jun 2010
Location: Germany
Posts: 17
Thanks: 1
Thanked 1 Time in 1 Post
Morri is an unknown quantity at this point
hey,

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

Morri

PS: mlseim was faster
Morri is offline   Reply With Quote
Old 06-22-2010, 04:21 PM   PM User | #8
sybil6
Regular Coder

 
Join Date: Jul 2006
Posts: 399
Thanks: 33
Thanked 7 Times in 7 Posts
sybil6 can only hope to improve
Quote:
Originally Posted by mlseim View Post
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.
sybil6 is offline   Reply With Quote
Old 06-22-2010, 04:23 PM   PM User | #9
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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?
mlseim is offline   Reply With Quote
Old 06-22-2010, 04:26 PM   PM User | #10
sybil6
Regular Coder

 
Join Date: Jul 2006
Posts: 399
Thanks: 33
Thanked 7 Times in 7 Posts
sybil6 can only hope to improve
Quote:
Originally Posted by mlseim View Post
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 is offline   Reply With Quote
Old 06-22-2010, 04:30 PM   PM User | #11
sybil6
Regular Coder

 
Join Date: Jul 2006
Posts: 399
Thanks: 33
Thanked 7 Times in 7 Posts
sybil6 can only hope to improve
Quote:
Originally Posted by DJCMBear View Post
you have to use string[] for the name to send the numbers over to php.
Really? how so?
sybil6 is offline   Reply With Quote
Old 06-22-2010, 04:31 PM   PM User | #12
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.



.

Last edited by mlseim; 06-22-2010 at 04:34 PM..
mlseim is offline   Reply With Quote
Old 06-22-2010, 04:48 PM   PM User | #13
sybil6
Regular Coder

 
Join Date: Jul 2006
Posts: 399
Thanks: 33
Thanked 7 Times in 7 Posts
sybil6 can only hope to improve
Quote:
Originally Posted by mlseim View Post
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
sybil6 is offline   Reply With Quote
Old 06-22-2010, 07:19 PM   PM User | #14
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
Quote:
Originally Posted by Morri View Post
hey,

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

Morri

PS: mlseim 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.

Quote:
Originally Posted by sybil6 View Post
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
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'].

Last edited by Keleth; 06-22-2010 at 07:23 PM..
Keleth is offline   Reply With Quote
Old 06-22-2010, 08:10 PM   PM User | #15
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:37 PM.


Advertisement
Log in to turn off these ads.