Jump to content

Simple expression problem


Robby McDonald

Recommended Posts

Ok I decided to brush up on some functions and this one was giving me a hard time. Basically the if condition is printing the statement even though its not what the expression was asking for.

 

I'm positive I am just missing something small, but I can't figure it out.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xm1ns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Counting Words...</title>
</head>
<body>

<?php

function count_words()
{
$totalwords = str_word_count($_POST['total']);
$amountneeded = $_POST['amount'];
$wordsneeded = $amountneeded - $totalwords;
$extrawords = $totalwords - $amountneeded;

if (isset($_POST['submitted']))
{
	if(str_word_count($_POST['totalwords']) <= $_POST['amountneeded'])
	{
		print "\nYou need $wordsneeded more words";
	}
	else
	{
		print "\nYou wrote an extra $extrawords words";
	}
	if(str_word_count($_POST['totalwords']) == $_POST['amountneeded'])
	{
		print "You hit the jackpot and got just as much words as you needed";
	}
}
}

?>

<form action="wordcount.php" method="post">

<p>Words Needed: <input type="text" name="amount" value="" /></p>
<p>Copy and Paste your words:</p> <textarea name="total" rows="5" cols="30"></textarea>
<input type="submit" name="submit" value="Count My Words" />
<input type="hidden" name="submitted" value="true" />

</form>

<?php

count_words();

?>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/226418-simple-expression-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.