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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    137
    Thanks
    1
    Thanked 0 Times in 0 Posts

    unable to read checkbox and radio

    i am using a self submitting form

    <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST" id="test2" name="test1">

    i need to do a validation of textfields, checkboxes, radio buttons

    i am able to read, display and validate textfields after the form has been submitted however i am getting an error for

    checkbox and radio buttons.

    code for textfields

    username <input type="text" name="username" value="<?php echo($username) ; ?>" />

    $username = htmlentities($_POST["username"]);

    if($username == "") { $error.=" enter username <br />"; }

    for checkboxes and radio buttons i am getting
    "Notice: Undefined index: checkboxname " for checkbox
    "Notice: Undefined index: radiobuttonname " for radio button

    presently the code for checkbox and radio button is

    <input type="checkbox" name="cbox" value="abc" />

    $deposit = $_POST["cbox"];


    <input type="radio" name="radioname" value="one"> one
    <input type="radio" name="radioname" value="two"> two

    $radioname = $_POST["radioname"];

    i have tried cbox[] radioname[] however i keep getting
    "Notice: Undefined index: cbox " and "Notice: Undefined index: radioname "
    for checkbox and radio button

    please advice.

    thanks.

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    print_r($_POST);
    that should give you information on the cbox and radio

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You should use isset. The checkboxes and radio buttons only get posted if they were checked
    PHP Code:
    // checkboxes should have unique names not the same one.
    if(isset($_POST['cbox']))
    {
    echo 
    'Checkbox cbox posted';
    }
    else
    {
    echo 
    'Checkbox cbox not posted';
    }
    // radio buttons can only one button checked within the same name
    if(isset($_POST['radioname']))
    {
    echo 
    'Radio input radioname posted';
    }
    else
    {
    echo 
    'Radio input radioname not posted';

    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I say there should always be a default on a radio button set... if it's valid to not pick any of the radio options, then there should be a "none" radio button that is the default.

    Also a fun little trick to force the POST to create the checkbox index is to include a hidden form element that is named the same as the checkbox element.

    Code:
    <form name="form1" method="post">
    <input type="hidden" name="chk1" value="no">
    <input type="checkbox name="chk1" value="yes">Check this
    </form>


  •  

    Posting Permissions

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