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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Validation if submit... checkbox and dropdown together validation

    Im doing validation for checkboxes and dropdowns in this way..

    but this is not workingg


    if (isset($_POST['submit'])) {

    if (isset($_POST['hearaboutus']) == "Radio" && $Drophearaboutus1 == "")
    {
    $hearaboutus_error = 1;
    $problems=1;
    }
    isset($_POST['hearaboutus']) == "Television" && $Drophearaboutus2== "")
    {
    $hearaboutus_error = 1;
    $problems=1;
    }

    }


    Code:
    <form name= form1 action =""> <table width="282">
             <tr> <td width="39%"> <input type="checkbox" name="hearaboutus[]" VALUE="Radio" <? if ($hearaboutus && in_array ("Radio", $hearaboutus)) echo ' checked="checked"'; ?>/>Radio </td>
             <td width="58%" >
             <?php
    				$query = "SELECT source_detail FROM hearaboutus WHERE source = 'Radio'";
    				$result = mysql_query($query, $db) or die(mysql_error());
    				
    				echo '<select name="Drophearaboutus1" style="width:150px;">';
    				echo '<option value="" selected>' .Select. '</option> <br>';
    				
    				while($row = mysql_fetch_assoc($result)){
    					$selected = $Drophearaboutus1 == $row['source_detail'] ? 'selected="selected"' : '';
    						 
    					 echo '<option value="'.$row['source_detail'] .'"  '.$selected.' >'.$row['source_detail'] . '</option><br>';
    				}
    				echo '</select>';
    		?>
             </td></tr>
             <tr><td> <input type="checkbox" name="hearaboutus[]" VALUE="Television" <? if ($hearaboutus && in_array ("Television", $hearaboutus)) echo ' checked="checked"'; ?>/>Television</td>
             <td>
             <?php
    				$query = "SELECT source_detail FROM hearaboutus WHERE source = 'Television'";
    				$result = mysql_query($query, $db) or die(mysql_error());
    				
    				echo '<select name="Drophearaboutus2" style="width:150px;">';
    				echo '<option value="" selected>' .Select. '</option> <br>';
    				while($row = mysql_fetch_assoc($result)){
    					$selected = $Drophearaboutus2 == $row['source_detail'] ? 'selected="selected"' : '';
    					 echo '<option value="'.$row['source_detail'] .'"  '.$selected.'>'.$row['source_detail'] . '</option><br>';
    				}
    				echo '</select>';
    		?>
             </td></tr>
              <tr><td> <input type="checkbox" name="hearaboutus[]" VALUE="Print" <? if ($hearaboutus && in_array ("Print", $hearaboutus)) echo ' checked="checked"'; ?>/>Print</td>
             <td>
             <?php
    				$query = "SELECT source_detail FROM hearaboutus WHERE source = 'Print'";
    				$result = mysql_query($query, $db) or die(mysql_error());
    				
    				echo '<select name="Drophearaboutus3" style="width:150px;">';
    				echo '<option value="" selected>' .Select. '</option> <br>';
    				while($row = mysql_fetch_assoc($result)){
    					$selected = $Drophearaboutus3 == $row['source_detail'] ? 'selected="selected"' : '';
    					 echo '<option>'.$row['source_detail'] . '</option><br>';
    				}
    				echo '</select>';
    		?>
             </td></tr>
           
          
           </table> <input type= "button" name="submit">
    </form>

  • #2
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    The return value of isset() is TRUE or FALSE, so something like
    PHP Code:
    isset($_POST['hearaboutus']) == "Radio" 
    will never be true.

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,395
    Thanks
    61
    Thanked 535 Times in 522 Posts
    I prefer to format my {} symbols differently to most coders but this should work for you.. (I've not tested it though) but first I'll show you what I don't like about your code:

    PHP Code:
    // If your user clicks enter/return in a text box your button won't be submitted
    //Use a hidden form element instead
    if (isset($_POST['submit'])) {

    //isset returns a boolean - not a string that you can compare
    //Also what is $Drophearaboutus1?
    if (isset($_POST['hearaboutus']) == "Radio" && $Drophearaboutus1 == "")
    {
    $hearaboutus_error 1;
    $problems=1;
    }
    //Same as above but you're not even using if?
    isset($_POST['hearaboutus']) == "Television" && $Drophearaboutus2== "")
    {
    $hearaboutus_error 1;
    $problems=1;
    }


    Let me show you how I would do it:
    PHP Code:
    //Check that form is being submitted by checking for hidden element
    if (isset($_POST['submitter']))
       {
       
    //If hearboutus is set AND it compares to Radio
       
    if ((isset($_POST['hearaboutus'])) and ($_POST['hearaboutus'] == "Radio"))
          {
          
    $hearaboutus_error 1;
          
    $problems=1;
          }
          
       
    //Same as above
       
    if ((isset($_POST['hearaboutus'])) and ($_POST['hearaboutus'] == "Television"))
          {
          
    $hearaboutus_error 1;
          
    $problems=1;
          }
       } 
    Note that I dropped $Drophereaboutus as you've not explained where they come from, how they're set or what they're used for so I can't really see what I'm using it for or how to demonstrate it


  •  

    Tags for this Thread

    Posting Permissions

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