Jump to content

Recommended Posts

I have this validation for 2 groups of check buttons in a big form, the code for the check buttons is here:

 

<fieldset style="padding: 2">
     <legend>Available Sizes
     </legend>
     <table  border="1" id="Sizes">
        <tr>

        <?php

        $X=0;
        
        while($X!=6)
        {
           $Size = $CBSizes[$X];
           ?>
   <td><?php echo $Size?>:</td>
   <td><input type="checkbox" name="Sizes[]" value="<?php echo $Size?>"></td>
           </tr>
           <tr>
           <?php
           ++$X;
        }
        ?>
     	</tr>
     </table>
    </fieldset>
    <br>
    <br>
    <fieldset style="padding: 2">
     <legend>Available Colours</legend>

     <table border="1"><tr>

      <?php
      //Establish a connection to the Database

      $conn = mysql_connect($Host,$Username,$Password);

      $db = mysql_select_db($Dbname, $conn);

      $sql = "SELECT AvailableColours FROM Colours ORDER BY AvailableColours";
   
      $rs = mysql_query($sql, $conn) or die(mysql_error());

      $NumberOfColours = mysql_num_rows($rs);

      $counter = 0;

      if ($result || (mysql_num_rows($rs) > 0))
      {
         while ($row = mysql_fetch_assoc($rs)) 
         {    
            if($counter%9==0&&$counter!=0) echo "</tr><tr>";
            echo "<td><img src='/images/colours/{$row['AvailableColours']}.gif' /><br />";
            echo "<td><input type='checkbox' name='Colours[]' value='{$row['AvailableColours']}'></td>";
            echo "</td>";
            ++$counter;
         }
      }
      echo "</tr></table>";
     
     mysql_close($conn); 
     ?>
    </legend>
   </fieldset>

 

The validation for this code once submitted is here, it is located above the <html> tags:

 

   $CBSizes = array("Small", "Medium", "Large", "XL", "XXL", "3XL");

   $AColours = $_POST['Colours'];

   $ASizes = $_POST['Sizes'];

   //Available Sizes Section

   if(count($ASizes) > 0)
   {
      foreach($ASizes AS $Sizes)
      {
         $Valid_ASizes=True;
         $AvailableSizes .=$Sizes."?";
         echo $AvailableSizes;
      }
   }
   else 
   {
      $Valid_ASizes=False;
      echo "Please select a size!<br>";
   }
   
//Available Colours Section

   if(count($AColours) > 0)
   {
      foreach($AColours AS $Colours)
      {
         $Valid_AColours=True;
         $AvailableColours .=$Colours."?";
         echo $AvailableColours;
      }
   }
   else 
   {
      $Valid_AColours=False;
      $ValidForm = False;
   }

 

Now what is is supposed to do is output each value into a string that has been concatenated, and each value is separated with a ?. Now what happens with that code is that it seems to repeat all previous values plus the new one each time instead of just adding the new value on top of the previous ones.

 

An example of the values of each are:

 

(6) selections made for Size:

Small?Small?Medium?Small?Medium?Large?Small?Medium?Large?XL?Small?Medium?Large?XL?XXL?Small?Medium?Large?XL?XXL?3XL?

 

(4) Selections made for Colour:

Ash?Ash?ForestGreen?Ash?ForestGreen?Natural?Ash?ForestGreen?Natural?SteelBlue?

 

Can you tell me what is going wrong?

Link to comment
https://forums.phpfreaks.com/topic/85458-solved-problem-with-foreach-loop-in-form/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.