I have a problem that I cannot for the life of me find a solution to and since I have received assistance on this forum before I thought I would give it a try here again.
I posted this question earlier but I did not have time to finish it before I had to leave for work. I will paste my code from two programs I am trying to run together to collect and manipulate data entered from a user. The first is an HTML file that is supposed to collect the data and send it to a PHP script to work with. This is the HTML code I have.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Words for Jumble</title>
<h3>Please enter 4 words between 4 and 7 characters long</h3>
<form action="Jumble.php" method="post">
<p>word 1: <input type="text" name="word 1"></p>
<p>word 2: <input type="text" name="word 2"></p>
<p>word 3: <input type="text" name="word 3"</p>
<p>word 4: <input type="text" name="word 4"</p>
<input type="reset" value="Clear Form" />&nbsp;
&nbsp;<input type="submit" name="submit" value="send form" />

this is I believe working correctly except the data does not seem to be getting to the PHP script correctly. This is the PHP script that is supposed to get the data entered into the HTML page and manipulate it but it keeps failing at the if (empty($data)) condition which means the data is not being entered but I know that it is entered because I have entered the data every time I tested this but it is not being passed to the PHP file correctly somehow. Anyway here is the PHP script code and maybe you can see the issue I keep missing.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Word Jumble</title>
   error_reporting(E_ALL & ~E_NOTICE);
   function DisplayError($fieldName, $errorMsg) {
   	echo "Error for \ "$fieldName\": $errorMsg<br \>\n";
	++$errorCount;  }
	function wordCheck($data, $fieldName) {
		global $errorCount;
//this line fails//     if (empty($data)) {
		 	displayError($fieldName, "You must enter a word here between 4 and 7 chracters");
			$retval = "";
		  else {
		  	$retval = trim($data);
			$retval = striplashes($retval);
			if ((strlen(($retval)<4))|| (strlen($retval)>7)) { 
			displayError($fieldName, Must be between 4 ans 7 characters in length");		  }  
		    if (preg_match("/^[a-z]+$/i", $retval)==0){
				displayError($fieldName, Words must be letters only");
			$retval = strtoupper($retval);
			$retval = str_shuffle($retval);
			$words[] = wordCheck($_POST["word 1"], "word 1");
			$words[] = wordCheck($_POST["word 2"], "word 2");
			$words[] = wordCheck($_POST["word 3"], "word 3");
			$words[] = wordCheck($_POST["word 4"], "word 4");
			if ($errorCount>0)
			   echo "Please re-enter data.<br />\n";
			else {
				$wordnum = 0;
				foreach ($words as $Word)
				   echo "Word ", ++$wordnum.": $Word<br />\n";

This is the output I get no matter what I enter:
Error for "word 1": You must enter a word here between 4 and 7 characters
Error for "word 2": You must enter a word here between 4 and 7 characters
Error for "word 3": You must enter a word here between 4 and 7 characters
Error for "word 4": You must enter a word here between 4 and 7 characters
Word 1:
Word 2:
Word 3:
Word 4:

Please help me!! this project is due Friday and I cannot see what mistake I am making here. Thanks in advance for any assistance!!

Recommended Answers

All 5 Replies

I found several problems with your code.

1. The reason why the data is not being passed is that you should not have a space in the field names. Browsers encode it to be a + and so you cannot access it later. Covert this to an uderscore e.g. word_1, word_2 etc.

2. On your HTML file I see a <?php> and </?>. I'm not sure what these tags are for, but I'm pretty sure they are not required.

3. On line 21 and 23 the string is not enclosed with quotes, you have only the end quote and not the beginning quote.

displayError($fieldName, "Must be between 4 ans 7 characters in length");

4. The line 20 has an extra (
if ((strlen(($retval)<4))|| (strlen($retval)>7)) {
you can use if (strlen($retval)<4 || strlen($retval)>7) { instead

Should work now. Check it and let me know.

Thanks for the pointers but I still can't check it because now it is giving me this gem:
Fatal error: Call to undefined function striplashes() in C:\wamp\www\Jumble.php on line 19
I checked on line 19 and it does have this:

$retval = striplashes($retval);

but I am not sure exactly where striplashes() comes from because I was following the text book when I wrote the code and I guess i assumed it was a defined PHP function that I did not recognize, I am new to this really :|, but I guess it is not because the script is calling it a FATAL ERROR:!! If you know could you please tell me why this is happening and if I need to take striplashes() out of the code completely??
Thanks again,

OK. That was one of the things I found before but forgot to tell you about. It's no striplashes but stripslashes(). I think you missed out the s while typing it out.

The slashes (\) are used to escape special characters such as (') and ("). You need to escape them since they are also used for other things such as enclosing strings.

change the code to $retval = stripslashes($retval); for it to work.

Thanks again sudeepjd,
I also realized this after checking it out thoroughly that I had misspelled the function name:sad: and i made the change and it does work right now and so i turned it in early!!:icon_smile:
But as I said thanks again for the time you took with this and take care yo!!:cool:

If you do not have everything you need, could you please mark this thread as solved. You can do this by clicking the Mark Solved link in the top bar. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.