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 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2003
    Location
    Mexico City
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Recovering checkboxes, radio buttons and selects

    hi

    ok, I have this problem now:
    in my auto-processed page I have this structure to recover the text's and textarea's values if the formats that i set for these values isn't correct:

    PHP Code:
    <?
    function form()
    {
    ?>
    <form action="<? $_SERVER['PHP_SELF'?>" method="POST">
    Name: <input type="text" name="name" value="<? echo $_POST['name']; ?>">
    Address: <input type="text" name="addr" value="<? echo $_POST['addr']; ?>">
    Comments: <textarea name="comm" rows="10" cols="40"><? echo $_POST['comm']; ?></textarea>
    <input type="submit" name="submit">
    <?
    }

    if(isset(
    $_POST['submit']))
    {
     
    //processing stuff
    }
    else
     
    form();
    ?>
    So, if a user types a name, address or comments in a wrong format, the page reloads and displays again the same form with a message saying "please type the name in a valid format", and for example if the user typed "9034@@" in the "name" field, the page recovers that value and displays it again IN THE TEXT OR TEXTAREA.

    My question is: how can I do the same with checkboxes, radiobuttons and selects??

    Hope u can help me

  • #2
    New Coder
    Join Date
    Sep 2003
    Location
    Cardboard Box
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can use if statements or loops for bigger lists

    eg. if the radio button is ticked then print the input type as checked else print the input type as not checked.

    Select statements are easy to set up using a for or while loop and arrays containing the info for each option

    with checkboxes remember to set the type name as an array so that you can access each option within the group.

    hope that makes sense, if not i can russle up some examples for you.

  • #3
    New Coder
    Join Date
    Jul 2003
    Location
    Mexico City
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hope that makes sense, if not i can russle up some examples for you.
    Could you?

  • #4
    New Coder
    Join Date
    Sep 2003
    Location
    Cardboard Box
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, first a select list (the text is in german before you ask!)

    PHP Code:
    <select name="title">
    <?php

      $titlef 
    = array("Herr""Frau""Fam.""Mr""Mrs""Ms""Miss");

      for (
    $i=0$i<7$i++) {
          if (
    $titlef[$i] == $title) {
              print 
    "<option value='".$titlef[$i]."' selected>".$titlef[$i]."</option>";
          } else {
              print 
    "<option value='".$titlef[$i]."'>".$titlef[$i]."</option>";
          }
      }

    ?>
    </select>
    I've built the array by hand, could be an easy way of doing this especially with large arrays, haven't looked into that yet.

    radio buttons

    PHP Code:
          if ($row['weight'] == "Items") {
              print 
    "  <input type='radio' name='weight_".$i."' value='Items' checked /> Items\n";
              print 
    "  <input type='radio' name='weight_".$i."' value='Kilograms' /> Kilograms\n";
          } else {
              print 
    "  <input type='radio' name='weight_".$i."' value='Items' /> Items\n";
              print 
    "  <input type='radio' name='weight_".$i."' value='Kilograms' checked /> Kilograms\n";
          } 
    I only had two so it was quick and easy, more options will possibly need an array built again, but i'm sure you get the idea.

    And checkboxes are on a similar wavelength too. You just have to run through the array submitted and if it's in the array then it's checked, if it isn't then it's not.

    hth

  • #5
    New Coder
    Join Date
    Jul 2003
    Location
    Mexico City
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Me again

    Thanx!! it helped me for simple selects and radio buttons.

    But I still have problems with checkboxes, because im getting that values from a query like this

    PHP Code:
    while($row=mysql_fetch_array($result))
    {
        echo 
    "<tr>\n";
        echo 
    "<td>".$row['name']."</td><td><input type=\"checkbox\" name=\"users[]\" value='".$row['id']."'></td>\n";
        echo 
    "</tr>\n";

    I had this idea: Inside the form function:

    PHP Code:
    if(count($_POST['users'])>0)
    {
    //here will be the validation
    }
    else
    {
      while(
    $row=mysql_fetch_array($result))
      {
          echo 
    "<tr>\n";
          echo 
    "<td>".$row['name']."</td><td><input type=\"checkbox\" name=\"users[]\" value='".$row['id']."'></td>\n";
          echo 
    "</tr>\n";
      }

    My question is: How can I build the array with the query results (name and id) to compare it later?? and how will the 'if' statement would look like ??

    Help plz

  • #6
    New Coder
    Join Date
    Sep 2003
    Location
    Cardboard Box
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm problem is that only the checkboxes that are submitted are passed through the POST.

    so if you had say 5 checkboxes and ticked nos 3 and 5, on the POST it would have two array elements and consider them 0 and 1, not 2 and 4 as they should be. basically it doesn't pass through empty checkboxes.

    A way around this is to name each checkbox uniquely. If you have a set amount of checkboxes then you have a lot more control over it of course, but you could still use a loop and use a unique loop number each time to do so. Then the checkbox would be similar to the radio button.

    ie. have an incrementing variable in the loop ($i) and then append it to the end of the checkbox name.

    hth

  • #7
    New Coder
    Join Date
    Jul 2003
    Location
    Mexico City
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so if you had say 5 checkboxes and ticked nos 3 and 5, on the POST it would have two array elements and consider them 0 and 1, not 2 and 4 as they should be. basically it doesn't pass through empty checkboxes.
    Yeah!! that's exactly what it does.... I realized it by printing the variables and arrays...

    I'm gonna name uniquely each checkbox (as it seems to be the only way to solve this).

    Thank you so much.


  •  

    Posting Permissions

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