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