Jump to content

[SOLVED] Infuriating Form Validation


baloneysammitch

Recommended Posts

I have no clue what my problem is.  I think it is late and I'm going insane... someone with a fresh set of eyes help me out here?

 

So here's the problem:

I'm trying to make sure the user checks at least ONE of two checkboxes using a PHP validation system.  By my logic, the function should check to see if both value are empty to return an error, like so:

 

if (!$box1 && !$box2)
{
$error = true;
$message = "Form not submitted: Please make sure correct information is provided.";
}

 

I soon realized this does not work and tried a new approach:

 

if (($box1 == '') || ($box2 == ''))
{
$error = true;
$message = "Form not submitted: Please make sure correct information is provided.";
}

 

aaaaaaand this:

 

if (($box1 != 'on') && ($box2 != 'on'))
{
$error = true;
$message = "Form not submitted: Please make sure correct information is provided. What happened to my $box1 and $box2 !?";
}

 

This also does not work.  I've tried so many variations of this but with no luck.  Strangely, I've echoed the variables in the error message for troubleshooting purposes, and they seem to change based on the math of validation I use... I have no idea what is going on.

 

I only pulled the relevant code out of my much larger form.  I hope someone can make sense of this:

 

<?php

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

// Error checking
$message = "";
$error = false;

	for ($i = 0; $i < 8; ++$i)
	{
		$lodgingthu = $_POST["lodgingthu$i"];
		$lodgingfri = $_POST["lodgingfri$i"];

		$lodgingthuflags[$i]  = $lodgingthu;
		$lodgingfriflags[$i]  = $lodgingfri;

		if (!$lodgingthu && !$lodgingfri)
		{
			$error = true;
			$message = "Form not submitted: Please make sure lodging information is provided.";
		}
	}

}

?>

<form name="mainform" action="test.php" method="post">

<?php

if ($message)
{
echo "<center><span align=\"center\" style=\"color: red;\">" . $message . "</span></center><br>";
}

?>

<table align="center">
	<tr>
		<td></td>
		<td align="center" valign="bottom" style="font-weight: bold;">Thu 4/19</td>
		<td align="center" valign="bottom" style="font-weight: bold;">Fri 4/20</td>
	</tr>

<?php

for ($i = 0; $i < 8; ++$i)
{
	echo "<tr>";

		// line number
		echo "<td>" . ($i + 1) . ". ";
		echo "</td>";

			$temp = $lodgingthuflags[$i];
			$checked = "";
			if ($temp == "on")
			 $checked = "checked";

		echo "<td  valign=\"top\" align=\"center\"><input type=\"checkbox\" name=\"lodgingthu$i\" $checked";

			$temp = $lodgingfriflags[$i];
			$checked = "";
			if ($temp == "on")
			 $checked = "checked";

		echo "<td  valign=\"top\" align=\"center\"><input type=\"checkbox\" name=\"lodgingfri$i\" $checked";

	echo "</tr>";
}
?>
<tr>
	<td colspan="3" align="center">
		<input type="submit" name="submit" onMouseOver="formRollover('submitButt')"  onMouseOut="restoreFormRollover('submitButt')" id="submitButt" value="Submit" class="submit">
	</td>
</tr>
</table>

</form>

Link to comment
Share on other sites

I would use an array of form elements this way the boxes that are checked are only passed to the script. So if you want 1, 2, 3 checkboxes checked and only (2) were sent to the script you know the ones that were not checked! You can loop the the ones that were checked to find out which one wasn't checked!

 

Quick Example...

 

<?php

$options = 4;

if ( ! empty ( $_POST['send'] ) )
{
// boxes that must be checked

$boxes = array ( 1 => 1, 2 => 2, 3 => 3, 4 => 4);

if ( ! empty ( $_POST['boxes'] ) )
{
	foreach ( $boxes AS $checkbox )
	{
		if ( isset ( $_POST['boxes'][$checkbox] ) )
		{
			unset ( $boxes[$checkbox] );
		}
	}

	if ( empty ( $boxes ) )
	{
		echo "Nice, all boxes were checked. <a href='" . $_SERVER['PHP_SELF'] . "'>Return</a> to the form!";
		exit ();
	}
}
}
?>
<html>
<head>
	<title>Untitled Document</title>
</head>
<body>
<div>
	<form action='<?php echo $_SERVER['PHP_SELF'];?>' method='post'>
		<input type='hidden' name='send' value='1' />

<?php
if ( isset ( $boxes ) )
{
foreach ( $boxes AS $not_checked )
{
	echo "\t\t\t<p>Check Box (" . $not_checked . ") was not checked!</p>\r\n";
}
}
?>
	Check Some Boxes
	<br />
	<br />
<?php
for ( $x = 1; $x <= $options; $x += 1 )
{
echo "\t\t\tCheck Box (" . $x . ") <input type='checkbox' name='boxes[" . $x . "]' " . ( isset ( $_POST['boxes'][$x] ) ? "checked='checked' " : null ) . "/>\r\n<br />\r\n";
}
?>
		<input type='submit' name='submit' value='Send' />
	</form>
</div>
</body>
</html>

Link to comment
Share on other sites

Thanks a ton everybody.  I slept on it and watched Ghost Rider (hilariously bad/awesome movie)... when I came back I found exactly what I needed here.

 

I feel silly for not knowing checkboxes returned "null" instead of "off."  I eventually went with this which works fine:

 

if (empty($box1) && empty($box2))

 

printf: Thanks for the code!  I like how simply you made sure the users options stayed there after the post and will be sure to use other bits as well!

 

Now I just need to figure out how to mark this post as solved.

Link to comment
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.