0

EDIT: Apparently I have to pay to register another domain on their site to have email capabilities. Sorry for the wasted time, and thanks for the code fixes.

I don't have any experience with php. I am just starting to figure it out. I am trying to get my form to send the entered information to my email. I set an else tag but that is all that happens. If you see any errors please let me know. I really want this to work. See for yourself: Website The codes are:

HTML:

<form action="post_comment.php" method="post" id="commentform">

<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>

<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2"   required="required"><br/><br/>

<label for="comment" id="comment" class="required">Your Message</label><br/>

<textarea name="comment" rows="10" tabindex="4"  required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />
<input id="send" name="send" type="hidden" value="1" />

</form>

PHP:

    <?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['comment'];
    $from = 'From: '. $email; 
    $to = '[email protected]'; 
    $subject = 'WEBSITE';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['send'] == "1") { 
        if (mail($to, $subject, $body, $from)) { 
        echo '<p>Your message has been sent!</p>';
    } else { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
    }}
?>

I get Something went wrong, go back and try again! when I'm trying to submit the form. Updated code.

3
  • well because I have the else tag it just defaults and shows 'Something went wrong...' Commented Oct 17, 2012 at 12:51
  • I edited your question and added the output after going to your website and trying to submit the form. Commented Oct 17, 2012 at 12:52
  • edited my answer; your mail() call's parameters are wrong, specifically $body. Commented Oct 17, 2012 at 13:14

8 Answers 8

4
<form action="post_comment.php" method="post" id="commentform">

rather than method="request"

edited to add http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1

edited (in response to edited question code...) to add http://php.net/manual/en/function.mail.php

your mail() parameters are in the wrong order; should be $to, $subject, $message [, $additional_headers, etc.]

Sign up to request clarification or add additional context in comments.

5 Comments

And then use $_POST['comment'] etc to access the form values!
good spot @nickhar; just saw the error in the form, didn't look that closely at the php...
Ok. It still gives me the same message though.
did you change the $_REQUEST['etc...'] to $_POST like nickhar suggested?
@PedrodelSol , the order of the parameters are fine as far as I'm concern.
3

According to your code:

   if ($_REQUEST['submit']) {               
        if (mail($to, $subject, $body, $from))
        { 
           echo '<p>Your message has been sent!</p>';
        }
        else
        { 
           echo '<p>Something went wrong, go back and try again!</p>'; 
        }
   }

Getting the output of:

Something went wrong, go back and try again!

Means that something went wrong with the mail() function, it returned false , otherwise we would see the Your message has been sent! message.

The problem with the mail() function is that it doesn't show any errors or warning , it just returns false.

How to locate the problem?

Try writing something like:

    if(mail("[email protected]" , "A subject for example" , "the content of this email","From: [email protected]"))
     echo "We are good";
    else
     echo "Something not workin";

If it works , check out the value of any of your posted variables right after declaring them.

   echo $name;
   echo $email;
   echo $message;

Make sure that those variables are not empty and that the $email variable contains a legal and validated email address.

If the basic mail usage didn't work - it's something related to your php settings (php.ini) or a limitation by your server (contact your hosting company).

EDIT1: About your html form , the type attribute of the input fields should be text and not name or email.

Instead of <input type="email"... Write <input type="text"...

7 Comments

so how would I test this? new temporary php and link it to my submit button?
Have a new php file only with the mail function (as I wrote) , then follow the instructions and share with us the results.
I made the php file but what do I do with it? link it to my submit button on my page?
No , just upload it and run it. For instance , bla.php - visit yourdomain.com/path/to/bla.php. Write here what's the output of this url.
Ok. Sent me to a blank page with no email. So... Server limitations? -_-
|
3
<form action="post_comment.php" method="get" id="commentform">

<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>

<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2"   required="required"><br/><br/>

<label for="comment" id="comment" class="required">Your Message</label><br/>

<textarea name="comment" rows="10" tabindex="4"  required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />

</form>

method must be get/post

Comments

3

Your field "submit" is a HTML button to submit the form and will not be added to the form. Add another input field which is not visible:

<input id="send" name="send" type="hidden" value="1" />

and access the field in PHP with:

if ($_REQUEST['send'] == "1") {

I hope this helps you.

2 Comments

ok I added that. The second one replaces if ($_REQUEST['submit']) {
And for sure, not $_REQUEST but $_POST is the right command :)
1

the name of the textarea is comment and you're using it as message in this line,

  $message = $_REQUEST['message'];

Needs to be,

 $message = $_REQUEST['comment'];

Also, the method of the form submission, needs to be POST like this,

<form action="post_comment.php" method="post" id="commentform">

Comments

1

please change form as follows

<form action="post_comment.php" method="post" id="commentform">

In the form you can use only get/post

Comments

1

Try this

<form action="post_comment.php" method="post" id="commentform">

<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>

<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2"    required="required"><br/><br/>

<label for="comment" id="comment" class="required">Your Message</label><br/>

<textarea name="comment" rows="10" tabindex="4"  required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />

and in your post page..

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
    $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: '. $email; 
$to = '[email protected]'; 
$subject = 'WEBSITE';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";


$headers = "From: $from \r\n";
//$headers .= "Reply-To: $visitor_email \r\n";

mail($to, $subject, $body,$headers);
}

Comments

1

-> Use POST instead of REQUEST

-> comment chaged to message

HTML

<form action="post_comment.php" method="post" id="commentform">
<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>
<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2"  required="required"><br/><br/>
<label for="comment" id="comment" class="required">Your Message</label><br/>
<textarea name="message" rows="10" tabindex="4"  required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />
</form>

PHP

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: '. $email; 
    $to = '[email protected]'; 
    $subject = 'WEBSITE';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {               
        if (mail($to, $subject, $body, $from)) { 
        echo '<p>Your message has been sent!</p>';
    } else { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
    }}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.