Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Jul 2006
    Posts
    399
    Thanks
    33
    Thanked 7 Times in 7 Posts

    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 03:55 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jul 2006
    Posts
    399
    Thanks
    33
    Thanked 7 Times in 7 Posts
    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..

  • #4
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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

  • #5
    Regular Coder
    Join Date
    Jul 2006
    Posts
    399
    Thanks
    33
    Thanked 7 Times in 7 Posts
    It looks it is not possible to do this with PHP, can someone help me?
    regards

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    With radio buttons, it does not involve the array ...

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

  • #7
    New Coder
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 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

  • #8
    Regular Coder
    Join Date
    Jul 2006
    Posts
    399
    Thanks
    33
    Thanked 7 Times in 7 Posts
    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.

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    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?

  • #10
    Regular Coder
    Join Date
    Jul 2006
    Posts
    399
    Thanks
    33
    Thanked 7 Times in 7 Posts
    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?

  • #11
    Regular Coder
    Join Date
    Jul 2006
    Posts
    399
    Thanks
    33
    Thanked 7 Times in 7 Posts
    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?

  • #12
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    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 05:34 PM.

  • #13
    Regular Coder
    Join Date
    Jul 2006
    Posts
    399
    Thanks
    33
    Thanked 7 Times in 7 Posts
    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

  • #14
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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 08:23 PM.

  • #15
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •