Help with Comment Box using php

Thanks!!
I found a url that allows messages to go through: http://antarctica-ufo.com/send_mail.php?comment=1&name=1
But I cannot figure out where to put that code you gave me. I added something else to the .php too $minlen = $_REQUEST[‘minlength’];
I don’t know if that is right or not (it didn’t work without that either).
so here is my php:

<?php
$webmaster_email = "me@antarctica-ufo.com";
$thankyou_page = "thank_you.html";
$name = $_REQUEST['name'] ;
$minlen = $_REQUEST['minlength'];
if (empty($name)) {
	 $name='(( No Name ))';
}
$comment = $_REQUEST['comment'] ;
if(strlen(trim($_POST['comment'])) < $minlen) { $errors[] = "The comment must be at least $minlen characters." ;}
$msg = "\r\nName: " . $name . "\r\n\r\nComment: " . $comment . "\r\n";
	mail( "$webmaster_email", "Comment on Antarctica-UFO.com", $msg );
	header( "Location: $thankyou_page" );
?>

I know I am missing something but I can’t figure out what.

Replace

$minlen = $_REQUEST['minlength'];

with

$minlen = 10;

Also you need another if to see if there are any errors, and if there are do not send the email (or the other way around, only send the email if there are no errors).

Thanks, I tried for about an hour and now it doesnt take me to either the error page or the thank you page.
here is what I came up with and it is not working:

<?php
$webmaster_email = "me@antarctica-ufo.com";
$thankyou_page = "thank_you.html";
$error_page = "error.html";
$feedback_page = "http://antarctica-ufo.com";
$name = $_REQUEST['name'] ;
$minlen = 10;
$comment = $_REQUEST['comment'] ;
$msg = "\r\nName: " . $name . "\r\n\r\nComment: " . $comment . "\r\n";

if (empty($name)) { 
	$name='(( No Name ))'; 
 }
 
if (!isset($_REQUEST['email_address'])) {
header( "Location: $feedback_page" );
}

if(strlen(trim($_POST['comment'])) < $minlen) { 
$errors[] = "The comment must be at least $minlen characters." ;
  header( "Location: $error_page" );
}
	
elseif {	
	mail( "$webmaster_email", "Comment on Antarctica-UFO.com", $msg );
	header( "Location: $thankyou_page" );
}
?>

Sorry, that was just an example code snippet, not a complete solution to fit into you script.
The $minlen variable needs to be defined beforehand, you could just use 10 in its place, but using a variable can make it easier to make changes, particularly if the value is used elsewhere.

The $errors array is a method I like to use in form validation. Again it’s best to pre-define the variable as an empty array at the start of validation:-

$errors = [];

Then when any validation error is detected, you add that to the array:-

if( something wrong ) { $errors[] = "Some error message for the user" ;}

Then at the end of validation, check if there are any errors recorded in the array:-

if(!count($errors)){  // The not (!) means no errors to count
    // All is well with the form, proceed to do as you will with the validated data
}
else{
        // There are errors, present the form again and list the errors
}

That is because you are checking $_REQUEST rather than $_POST specifically. Since the form specifies the post method, use post in processing the data. Request will look at either get or post data.
Processing of a post form should be within such a test:-

if($_SERVER['REQUEST_METHOD'] === 'POST'){
    // Process form data here.
}

Is that valid syntax? If you’re using elseif, surely there needs to be another clause on the end. As above, though, what you really need here is else.

Also, there’s no point checking to see if the name field is empty and sticking something in it after you’ve already appended it into your email message.

mail( "$webmaster_email", "Comment on Antarctica-UFO.com", $msg );

Why is the to-address inside quotes here? There is no need for it. It won’t stop it working, but they are not required here.

I am so thankful I got the form working properly.
I am sorry for needing so much help, but before this, I had no idea about php or even css and more. I only know html. I am so very thankful I got all this help and it works, I clearly couldn’t do it without you! :smiley:
2 things

  • how to list the errors? I could make a different error page for each error, but that doesn’t list them like I assume you could help with?
  • how to change $minlen to read the html minlength of the comment textarea in the form?
<?php
$webmaster_email = "me@antarctica-ufo.com";
$thankyou_page = "thank_you.html";
$error_page = "error.html";
$feedback_page = "index.htm";
$name = $_REQUEST['name'] ;
$minlen = 10;
$comment = $_REQUEST['comment'] ;
if (empty($name)) { $name='(( No Name ))';  }
$msg = "\r\nName: " . $name . "\r\n\r\nComment: " . $comment . "\r\n";
 
if (!isset($_REQUEST['email_address'])) {
$errors[] = "User tried to access send_mail.php script directly" ;
header( "Location: $feedback_page" );
}

if(strlen(trim($_POST['comment'])) < $minlen) { 
$errors[] = "The comment must be at least" . $minlen . "characters." ;
  header( "Location: $error_page" );
}
	
else {	
	mail( "$webmaster_email", "Comment on Antarctica-UFO.com", $msg );
	header( "Location: $thankyou_page" );
}
?>

Thanks again guys!! :smiley:

What I would generally do in a form is have all the processing before the HTML form. That may seem backwards, but it does make sense.
The form processing code goes within the condition I showed earlier:-

So it only fires on form submission, not when the user initially load the page with the form.

At the end of the form processing we check for errors:-

In the first if block (no errors) your email gets sent and the user is sent to the “Thank You” page, the script is exited at that point.

header( "Location: $thankyou_page" );
exit;

In the else block (errors were found) the script carries on to the following HTML part that contains the form.
In the HTML you can list the errors to bring them to the attention of the user.

<?php if(count($errors)) : ?>
<p class="alert">Your form submission contained errors:-</p>
<ul>
<?php foreach($errors as $e) :?>
<li><?= $e ?></li>
<?php endforeach ?>
</ul>
<?php endif ?>

All errors are shown and the user gets to try again with the form.

With your form code before the HTML $minlen can be defined already (along with other stuff) when the HTML is presented:-

<textarea name="comment" minlength="<?= $minlen ?>" maxlength="<?= $maxlen ?>" 

This would mean defining these variables before the Request Method condition.

I am just mind boggled at “have all the processing before the HTML form” lol
I guess I thought I could do more than I can.
I wish I could list the errors in the html after the php but I am forced to give up.
I know you’re not gonna design the site for me, but at least I got a working form thanks to a lot of people here!! Thanks guys! You are all very awesome!! :smiley:

In case any are interested in the story on my site, I will be working on the story more in the future. It is just so complicated and no matter what I write I just fail trying to jump right in. This is no laughing matter though, I swear on my family jewels it’s real!!! :stuck_out_tongue:

I just wanted to thank whoever kept trying to use the php url to send a comment with only 1 character!

Without you, I wouldn’t have made such a cool error page :slight_smile: haha
I am so happy with my site. I might come back for help with a database in a new topic someday soon.
Thanks guys for everything!

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.