I am writing a PHP program with sticky form. I want to use Text area for the "comments" part, but I couldn't make it work. Can anyone help? thanks.

<html>
<head>
<title>guestbook</title>
<style type="text/css">
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
extract($_REQUEST);
$errors = array();
if(isset($_REQUEST['submit']))
	{
		validate_input();
                if(count($errors) != 0)
                {
                	printForm();
                }
                else
                {
                      saveGuestbook($name, $email, $comments);
	       	      showGuestbook();
                }

	}
	else if($view)
	{
		showGuestbook();
	}
	else
	{
		printForm();
	}
?>
<?php
function validate_input()
{
	global $errors;
        if($_POST["name"] == "")
        {
        	$errors['name'] = "<span class=\"error\">Enter your name.</span>";
        }
         if($_POST["email"] == "")
        {
        	$errors['email'] = "<span class=\"error\">Enter your email.</span>";
        }
         if($_POST["comments"] == "")
        {
        	$errors['comments'] = "<span class=\"error\">Enter your comments.</span>";
        }

}
function showGuestbook()
{
	$theData = file("guestbook.txt");
	foreach($theData as $line)
		{
			$line = rtrim($line);
			print("<hr>");
			list($name, $email, $comments)= split("\t",$line);
			print("Name: $name<br>");
			print("Email: $email<br>");
			print("Comments: $comments<br>");
		}
		print("<hr><p>Back to <a href='guestbook.php'>Guestbook Page</a></p>");
}

function saveGuestbook($n, $e, $c)
{
	$fh = fopen("guestbook.txt", "a");
	$nameData = $n."\t";
	fputs($fh, $nameData);
	$emailData = $e."\t";
	fputs($fh, $emailData);
	$commentsData = $c."\n";
	fputs($fh, $commentsData);
	fclose($fh);
}


function printForm()
{
	global $errors;
       	print<<<FORM
<html>
<head>
<title>guestbook</title>
</head>

<body>
<h2>Please sign our guestbook.</h2>
<form action="guestbook.php" method="post" >

<p>Name: <br />
<input type="text" size="34" name="name" id="name"
FORM;
echo 'value="'.$_POST[name].'"/>';
echo $errors['name'];

print<<<FORM
<br />

<p>Email: <br />
<input type="text" size="34" name="email"  id="email"
FORM;
echo 'value="'.$_POST[email].'"/>';
echo $errors['email'];

print<<< FORM
<br />

<p>Comments:   <br />
<textarea rows="4" cols="23" name="Comments" id="Comments" wrap="physical"
FORM;
echo 'value="'.$_POST[comments].'"></textarea>'; // I think here is the problem 
echo $errors['comments'];

print<<<FORM
<br />
<br />
<input type= "submit" name="submit" value="submit" />
<input type="reset" name="reset" value="reset" />
</form>
<h3> Or feel free to just <a href="guestbook.php?view=view">view</a> our guestbook.</h3></body>
</html>
FORM;
}
?>
</body>
</html>

Recommended Answers

All 5 Replies

PHP variable names are case-sensitive.
Your name and id for the textarea are "Comments" (line 113), but the variable you pass to saveGuestbook (line 21) is $comments . It should be $Comments instead.

PHP variable names are case-sensitive.
Your name and id for the textarea are "Comments" (line 113), but the variable you pass to saveGuestbook (line 21) is $comments . It should be $Comments instead.

Thanks. I have changed line 113 "Comments" to "comments."but it still doesnt work properly. After I press "submit", it appears a page that says the link is borken. Actually if I use "text " instead of "textarea" for comments it works fine. But I need to use "textarea."
here is the code if I change to "text", which works fine:
<p>Comments: <br />
<input type="text" size="34" name="comments" id="comments"
FORM;
echo 'value="'.$_POST[comments].'"/>';
echo $errors;
Can anyone help me change it to textarea? Thanks : )

Thanks. I have changed line 113 "Comments" to "comments."but it still doesnt work properly. After I press "submit", it appears a page that says the link is borken. Actually if I use "text " instead of "textarea" for comments it works fine. But I need to use "textarea."
here is the code if I change to "text", which works fine:
<p>Comments: <br />
<input type="text" size="34" name="comments" id="comments"
FORM;
echo 'value="'.$_POST[comments].'"/>';
echo $errors;
Can anyone help me change it to textarea? Thanks : )

Something I meant to mention before, but forgot. I'm pretty sure you should be surrounding the text inside the brackets for $_POST[] with either single or double quotes. That is, $_POST[comments] should be $_POST, $_POST[name] should be $_POST, and $_POST[email] should be $_POST. You have it right on lines 39, 43, and 47, but further down in printForm() I don't see the quotes.

Then again, I've never used "<<<" to output anything, so I don't know if that has different rules.

<html>
<head>
<title>guestbook</title>
<style type="text/css">
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
extract($_REQUEST);
$errors = array();
if(isset($_REQUEST['submit']))
	{
		validate_input();
                if(count($errors) != 0)
                {
                	printForm();
                }
                else
                {
                      saveGuestbook($name, $email, $Comments);
	       	      showGuestbook();
                }

	}
	else if($view)
	{
		showGuestbook();
	}
	else
	{
		printForm();
	}

function validate_input()
{
	global $errors;
        if($_POST["name"] == "")
        {
        	$errors['name'] = "<span class=\"error\">Enter your name.</span>";
        }
         if($_POST["email"] == "")
        {
        	$errors['email'] = "<span class=\"error\">Enter your email.</span>";
        }
         if($_POST["comments"] == "")
        {
        	$errors['comments'] = "<span class=\"error\">Enter your comments.</span>";
        }

}
function showGuestbook()
{
	$theData = file("guestbook.txt");
	foreach($theData as $line)
		{
			$line = rtrim($line);
			print("<hr>");
			list($name, $email, $comments)= split("\t",$line);
			print("Name: $name<br>");
			print("Email: $email<br>");
			print("Comments: $comments<br>");
		}
		print("<hr><p>Back to <a href='guestbook.php'>Guestbook Page</a></p>");
}

function saveGuestbook($n, $e, $c)
{
	$fh = fopen("guestbook.txt", "a");
	$nameData = $n."\t";
	fputs($fh, $nameData);
	$emailData = $e."\t";
	fputs($fh, $emailData);
	$commentsData = $c."\n";
	fputs($fh, $commentsData);
	fclose($fh);
}


function printForm()
{
	global $errors;
       echo FORM;
		?>
<html>
<head>
<title>guestbook</title>
</head>

<body>
<h2>Please sign our guestbook.</h2>
<form action="guestbook.php" method="post" >

<p>Name: <br />
<input type="text" size="34" name="name" id="name" value="<?=$_POST[name]?>">



<br />

<p>Email: <br />
<input type="text" size="34" name="email"  id="email" value="<?=$_POST[email]?>">

<br />

<p>Comments:   <br />
<textarea rows="4" cols="23" name="Comments" id="Comments" wrap="physical"><?=$_POST[Comments]?>
</textarea>



<br />
<br />
<input type= "submit" name="submit" value="submit" />
<input type="reset" name="reset" value="reset" />
</form>
<h3> Or feel free to just <a href="guestbook.php?view=view">view</a> our guestbook.</h3></body>
</html>

<?
}
?>
</body>
</html>

you enterd html,head,body tags two times. remove it. i did nt chenge it.

Thanks for the replies. The problem is solved. It's because I changed the file name to something else while in the program I remained the name as "guestbook.php". No wonder it didn't work.

Thanks again for all the help. I appreciate that.

Have a nice day!

Ying

Be a part of the DaniWeb community

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