unable to read checkbox and radio

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • runway27
    Banned
    New Member
    • Sep 2007
    • 54

    unable to read checkbox and radio

    i am using a self submitting form
    [php]
    <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"];
    [/php]
    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.

    Use the appropriate code tags when showing any code in this forum! See the Posting Guidelines before you continue - moderator
    Last edited by ronverdonk; Feb 29 '08, 01:01 PM. Reason: code tags!!
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Although I didn't get what you are trying to ask. But I'll tell you a good way to debug such things.

    In the file, whom you have set as the value of action attribute in form tab, comment all the code and add this in the starting (just temporarily).[php]<?php
    echo "<pre>";
    print_r($_POST) ;
    echo "</pre>";
    ?>[/php]

    This may help you solve your problem.

    Comment

    • henryrhenryr
      New Member
      • Jun 2007
      • 103

      #3
      I reckon the error is caused because your form doesn't know if it's been submitted or not...

      Try adding something like:

      [code=html]
      <input type="hidden" name="submitted " value="true" />
      [/code]

      inside the form tags.

      Then at the TOP (ie before the form-display itself) put something like:

      [code=php]
      <?php
      if (isset($_POST['submitted'])) {
      $radioname= $_POST['radioname'];
      //now do something with this value...
      echo $radioname;
      }
      else {
      $radioname=NULL ;
      }
      ?>
      [/code]

      This way if you reference $radioname, you won't get the notice about the missing index when you first load the page (and the $_POST array has nothing in it). You can avoid all notices like this by using "isset " on the variable before using it. IE:

      [code=php]
      $radioname = (isset($_POST['radioname'])) ? $_POST['radioname'] : NULL;
      [/code]

      Something to note too - when you use self-submitting forms, if you hit refresh on your browser, the form will resubmit so normally you have to keep a track of whether it's been submitted - I use sessions, you can use DB too...

      Comment

      Working...