checkboxes help needed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Challenger
    New Member
    • Apr 2006
    • 11

    checkboxes help needed

    Hi I just started to try and learn PHP, so sorry if this is a simple thing to do.

    I have been try to use checkboxes on a website that I am making, but the only problem is that for the life of me i can't seem to check which onces have been checked. Also to make things more difficult for what I am doing i don't actually know how many checkboxes that there will be until run time. So any help would be nice.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    How can you expect to help out when we cannot see anything of what you have done so far?

    So post the code you have made sofar and we will look at it. When you post coe, show it within [php] tags! See the Posting Guidelines for the rules on posting code.

    Ronald :cool:

    Comment

    • Challenger
      New Member
      • Apr 2006
      • 11

      #3
      here is the code for what i have so far. I have tried a few things so there is code at the top that is just there to see if different things would work. Every thing else work so far, all that is stopping me from finishing this part is those checkboexes.


      [PHP]

      <?php
      session_start() ;
      ?>
      <html>
      <body>
      <?php
      include("menu.p hp");


      if ((($_POST["name"]=="aaron")&&($_ POST["pass"]=="hi"))||($_SE SSION['name2']=="scary"))
      {
      $count=($_SESSI ON['result2']);
      $comdel=$POST["del[]"];
      echo $comdel[1];
      //echo $count;
      for($s=0;$s<$co unt;$s++)
      {
      echo " Hi";
      echo $comdel[$s];
      echo $_POST["del[$s]"];
      if (($_POST["del".$s])=="hi")
      {
      echo "Delete";
      }
      }

      $hi=$_POST['del'];
      foreach ($del as $statename)
      {
      echo "$statename is checked";
      }


      foreach(($_POST['del[]']) as $myKey)
      {
      echo " Hi2";
      echo $myKey."<br />";
      }

      echo "The latest comments :<br />";
      $comen=(scandir ("comments",1)) ;
      $result = count($comen);
      $result=$result-2;
      $_SESSION['result2']=($result);

      echo "<table border='1'>";
      echo "<form action='comment s.php' method='post'>" ;

      $del=array_fill (0,$result,"");

      for ($i=0; $i<($result); $i++)
      {
      echo "<tr align='left'><t h>";
      echo "Delete<inp ut type='checkbox' name='del[]' value='hi'.$i />";
      echo "</th><th>";

      if(file_exists( "comments/$comen[$i]"))
      {
      $file = fopen("comments/$comen[$i]", "r+");
      //Output a line of the file until the end is reached
      while(!feof($fi le))
      {
      echo fgets($file). "<br />";
      }
      fclose($file);
      }
      else
      {
      echo ("There are no new comments");
      }

      echo "</th></tr>";

      }
      //echo "</table>";
      echo "<input type='submit' value='Delete' />";
      echo "</form>";
      }
      else
      {
      echo "incorrect username or password ";
      echo "<a href='/scary_little_ry n/Info/index.php'>Retu rn to login</a>";
      }
      ?>

      </body>
      </html>

      [/PHP]

      hope that helps

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hi.

        I have a simple example for you that I hope can help you.

        The code prints out check-boxes using an array I created, and then checks if any of the boxes have been posted.

        [PHP]
        <?php
        // Create an array with each box's index and label
        $boxes = array(
        array(1, "Box 1"),
        array(2, "Box 2"),
        array(3, "Box 3"),
        array(4, "Box 4"),
        array(5, "Box 5")
        );

        // Create the form
        echo '<form action="#" method="post">' ;
        foreach($boxes as $box)
        {
        echo '<input type="checkbox" name="cb-'. $box[0] .'" value="'. $box[0] .'" /> '. $box[1] ."<br />";
        }
        echo '<br /><input type="submit" name="submit" value="Submit" />
        </form>';

        // Check each value to see if it has been posted
        if(isset($_POST['submit']))
        {
        // Print the header
        echo "<p>Selecte d boxes:<br />";

        foreach($boxes as $box)
        {
        // Check if the box has been posted
        if(isset($_POST["cb-". $box[0]]))
        {
        echo " - ". $box[1] ."<br />";
        }
        }
        }
        ?>
        [/PHP]

        Comment

        • Challenger
          New Member
          • Apr 2006
          • 11

          #5
          Just a question quick question is it possible for the following part to be done using a loop? So that it can be as big or as small as the situation required.

          [php]
          $boxes = array(
          array(1, "Box 1"),
          array(2, "Box 2"),
          array(3, "Box 3"),
          array(4, "Box 4"),
          array(5, "Box 5")
          );
          [/php]

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Sure.

            You can create the array any way you like.

            You could do this, for example:
            [PHP]
            $boxes = array();
            for($x = 0; $x < 10; $x++)
            {
            $boxes[] = array($x, "Box ". $x);
            }
            [/PHP]

            Comment

            • Challenger
              New Member
              • Apr 2006
              • 11

              #7
              Thank you very much that worked like a charm

              Comment

              Working...