Results 1 to 10 of 10

Thread: IF statement on Radio <input>

  1. #1
    Join Date
    Jul 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default IF statement on Radio <input>

    I have two Radio inputs, with the same name and different values.
    I want to make in PHP an IF statement that will check if one of the Radios has been checked it will show "radio 1 checked"
    else if the other one has been checked it'll show "radio 2 checked"
    i don't know how to make it because the Radios has to be with the same name... how do I call them in their values ?

  2. #2
    Join Date
    Jun 2006
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    is this for a form or are you displaying this information some how "dynamicly"?

  3. #3
    Join Date
    Jul 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    it's for a form...
    but i can't do it in JS if that's what you meant to because i use it for an upload script that written in PHP

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <input type="radio" name="r1" value="1">
    <input type="radio" name="r1" value="2">
    Code:
    <?php echo($_POST['r1'] == 1 ? 'radio 1 checked' : 'radio 2 checked'); ?>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jul 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    it didn't work
    it shows me "radio 2 checked"
    when i press both of the radios
    http://www.eyal.webkit.biz/MsU/test.php

  6. #6
    Join Date
    Jul 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You are supposed to use switch case instead of if statements I guess. Such like this;


    PHP Code:
       $r1=$_POST['r1'];

    switch (
    $r1) {

    case 
    1:
     echo(
    "Selected radio button is: 1");
     break;
    case 
    2:
     echo(
    "Selected radio button is: 2");
     break;


    I hope, it helps.

    edit: thanks for correcting me twey
    Last edited by scaryguy; 07-25-2006 at 11:11 PM.

  7. #7
    Join Date
    Jul 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    blank page..
    Code:
    <?php 
    $_POST['r1']=$r1; 
    
    switch $r1 { 
    
    case 1: 
     echo("Selected radio button is: 1"); 
     break; 
    case 2: 
     echo("Selected radio button is: 2"); 
     break; 
    }  
    ?>
    
    <input type="radio" name="r1" value="1">
    <input type="radio" name="r1" value="2">
    http://www.eyal.webkit.biz/MsU/test.php

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $_POST['r1']=$r1;
    
    switch $r1 {
    
    case 1:
     echo("Selected radio button is: 1");
     break;
    case 2:
     echo("Selected radio button is: 2");
     break;
    }
    Errrr... no. I don't think that'll even run: there need to be brackets around $r1 for the switch statement. If it does, neither case will fire. $r1 is never defined, and so considered null. Since you set $_POST['r1'] to that empty value, $_POST['r1'] is also null after the first line.

    Neoeyal: Note that my code was for a POST form. If you're using a GET form, you should change all instances of $_POST to $_GET.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jul 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks =]
    now another question:
    I have a script for upload files
    how can i make the "resize image" like http://imageshack.us ?
    with the choose box and if the size of the image i chose to upload is less than the size i chose to resize it won't resize the image

    how can I make all of this?

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Haha, now you're getting into stuff I know less about
    I suggest you read the documentation on the gd image-manipulation library, but it's definitely not a task for beginners. djr33's quite good with that stuff; maybe he'll help out if he reads this.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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