Jump to content
Larry Ullman's Book Forums

Chapter 7 Pursue: Logic In Form Validation Not Working


Recommended Posts

Hi. First off, this is in regards to exercise dealing with using a foreach loop instead of implode(). I was having trouble with it, and of course looked up what was posted here already, but I want to do more. By the I mean I wanted to validate that the form data was not empty, had no numbers, and had a minimum of five words that will of course be alphabetized(though I'd rather being doing some analysis with it for simple patterns like the avg number of characters by word, number of vowels, number of constants, but  let me just be simple about it for now).

 

Here's the code(just a note, to give credit where it's due this is code I've altered that was posted here originally):

$flag = true;
$words_Array = $_POST['words'];
$count1 = count($words_Array);


// turn the incoming string, $_POST['words'], into an array and validate:

if (empty($words_Array) || (is_numeric($words_Array) ) || ($count1 < 5)  ) { 
print "<p>Please enter at least five words and no numbers.</p>";
$flag= false;    
}
   else {
   $words_Array = explode (' ', $_POST['words']); 
   }

//sort the words alphabetically
if ($flag) {
   sort($words_Array);
}

if ($flag) {
 print "<p>An alphabetized version of your list is: ";
   foreach ($words_Array as $key => $value) {
     print "<br />\n $value ";
     }
    }
   print "</p>";


?>

Now, I've run this code and whether I enter five words or four words and a number, it still runs the print statement inside the first if statement "Please enter at least five words and no numbers." So what I'm I missing here logic wise?

 

Also I'm thinking this code could much cleaner without having to be redundant by using three separate if statements, would an if-elseif-else be a good choice/best practice ? Lastly, can validation be done without using a boolean "flag" variable ?

 

Assistance is greatly appreciated.   

 

 

 

 

Link to comment
Share on other sites

If $_POST['words'] is a simple text input, then that's a string, and its count will always be 1, which is why the first conditional is always true. You should do the count after you explode().

 

You can use just one IF and the flag variable is not required. Sometimes it's just helpful.

Link to comment
Share on other sites

You could make the incoming $_POST variable into an array earlier in the logic.

$words_Array = explode (' ', $_POST['words']); 
$count1 = count($words_Array);

if ($count1 < 5) {
	print "<p>Please enter at least 5 words.</p>";
 	$flag= false;
}
foreach ($words_Array as $word) {
	if (is_numeric($word)) {
		print "<p>Please do not enter any numbers.</p>";
		$flag= false;
	}
}

//sort the words alphabetically
if ($flag) {
	sort($words_Array);
 	print "<p>An alphabetized version of your list is: ";
   		foreach ($words_Array as $word) {
     	print "<br />\n $word ";
     }
    print "</p>";
}
Link to comment
Share on other sites

Thanx for the assistance. I've modified the code accordingly, and here's what I got now:

$flag = true;
$words_Array = explode (' ', $_POST['words']);
$count1 = count($words_Array);

if ($count1 < 5) {
 echo "<p>Please enter at least 5 words.</p>";
 $flag = false;
}


foreach ($words_Array as $word) {
  if (is_numeric($word)) {
    echo "<p>Please do not enter any numbers.</p>";
    $flag = false;
   }

  elseif (empty($word)) {
    echo "<p>You have entered an empty space as a word. Please enter at 
    least 5 words.</p>";
    
    $flag = false;
   }
}

if ($flag) {
   sort($words_Array);
   echo "<p>An alphabetized version of your list is: ";
foreach ($words_Array as $word) {
 echo "<br />\n $word ";
 }

echo "</p>";
}

The script works fine for the most part. I noticed that if enter the words in lower case, they come in last. I'm thinking that the ucfirst() function is right for the job, so I'll see how I can fit it in. Also I'm going to see if I can get it down to one IF statement, like Larry mentioned. I appreciate your help margaux. 

Link to comment
Share on other sites

 Share

×
×
  • Create New...