Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-14-2011, 09:50 PM   PM User | #1
bale
New to the CF scene

 
Join Date: Feb 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
bale is an unknown quantity at this point
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>
bale is offline   Reply With Quote
Old 02-15-2011, 10:50 AM   PM User | #2
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
The return value of isset() is TRUE or FALSE, so something like
PHP Code:
isset($_POST['hearaboutus']) == "Radio" 
will never be true.
venegal is offline   Reply With Quote
Old 02-15-2011, 12:29 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,515
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Tags
php, validation

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:44 PM.


Advertisement
Log in to turn off these ads.