I would be very appreciative of anyone who could show me a better way to do what I did. Complete newbee here. I have a comment form sidebar of blog. I wanted to Name & Comment fields required and give error msgs when empty upon submit. AND wanted to make sure the comment wasn't lost in the event of an error. Here's what I did:
[PHP]
// Check to make sure Name & Comment are NOT empty
$name = trim($_POST['name']);
if (empty($name)) {
$nameerror['name'] = 'Please enter your name';
}
$comment = trim($_POST['comment']);
if (empty($comment )) {
$commenterror['comment'] = 'Please enter a comment';
}
// If comment submitted & post exists
//AND no error, then add comment to db
if (isset($_POST["postcommen t"]) != ""
&& (!isset($nameer ror) && (!isset($commen terror)))) {
$posttitle = addslashes(trim (strip_tags($_P OST["posttitle"])));[/PHP]
Of course, I'm leaving off the other variables for space considerations. ..
Then in my form I did the following....an d by some miracle it worked:
[PHP]form action="<?=$_SE RVER["PHP_SELF"]?>" method="post" id="addcomment" >
<input type="hidden" name="post_id" value="<?=$post _id ?>" />
<input type="hidden" name="posttitle " value="<?=$titl e ?>" />
<h3>Add a comment</h3>
<?php
if (isset($message )) {
echo "<p class='message' >".$_POST["message"]."</p>";
}
?>
<p>Name: (Required) <!-- Error msg inserted if NAME empty -->
<br />
<?php if (isset($_POST["postcommen t"]) != "" && (isset($nameerr or))) { ?>
<span class="warning" ><?php echo $nameerror['name']; ?></span>
<?php } ?><input name="name" type="text" <?php if(isset($comme nterror)
&& (isset($_POST["postcommen t"]) != "" && (!isset($nameer ror))))
{echo "value='$name'" ;} else {echo "value=''"; } ?> /></p>[/PHP]
And I did the same for the Comment part. It seems so complicated for a simple concept. Does anyone have a better, more concise way or suggestion? Thanks a lot.
[PHP]
// Check to make sure Name & Comment are NOT empty
$name = trim($_POST['name']);
if (empty($name)) {
$nameerror['name'] = 'Please enter your name';
}
$comment = trim($_POST['comment']);
if (empty($comment )) {
$commenterror['comment'] = 'Please enter a comment';
}
// If comment submitted & post exists
//AND no error, then add comment to db
if (isset($_POST["postcommen t"]) != ""
&& (!isset($nameer ror) && (!isset($commen terror)))) {
$posttitle = addslashes(trim (strip_tags($_P OST["posttitle"])));[/PHP]
Of course, I'm leaving off the other variables for space considerations. ..
Then in my form I did the following....an d by some miracle it worked:
[PHP]form action="<?=$_SE RVER["PHP_SELF"]?>" method="post" id="addcomment" >
<input type="hidden" name="post_id" value="<?=$post _id ?>" />
<input type="hidden" name="posttitle " value="<?=$titl e ?>" />
<h3>Add a comment</h3>
<?php
if (isset($message )) {
echo "<p class='message' >".$_POST["message"]."</p>";
}
?>
<p>Name: (Required) <!-- Error msg inserted if NAME empty -->
<br />
<?php if (isset($_POST["postcommen t"]) != "" && (isset($nameerr or))) { ?>
<span class="warning" ><?php echo $nameerror['name']; ?></span>
<?php } ?><input name="name" type="text" <?php if(isset($comme nterror)
&& (isset($_POST["postcommen t"]) != "" && (!isset($nameer ror))))
{echo "value='$name'" ;} else {echo "value=''"; } ?> /></p>[/PHP]
And I did the same for the Comment part. It seems so complicated for a simple concept. Does anyone have a better, more concise way or suggestion? Thanks a lot.
Comment