0

I have a problem with the form I'm trying to create. Basically, it does not allow me to send the email to the recipient, even though the PHP code is correct. Could it be the problem with TRIM PHP code?

<?php
    if ($_POST['submit']) {
        if (empty($_Post['name'])  || 
            empty($_POST['email']) || 
            empty($_POST['comments'])) {

            $error = true;
        } 
        else {

            $to = "[email protected]";

            $name = trim($_POST['name']);
            $email = trim($_POST['email']);
            $comments = trim($_POST['comments']);

            $subject = "Contact Form";

            $messages =  "Name: $name \r\n Email: $email \r\n Comments: $comments";
            $headers = "From:" . $name;
            $mailsent = mail($to, $subject, $message, $headers);

            if ($mailsent) {
                $sent = true;
            }
        }
    }
?>

My HTML is:

<?php if($error == true){ ?>

    <p class="error">Text</p>

<?php } if($sent == true) { ?>

    <p class="sent">Text</p>

<?php } ?>

<div id="form">
    <form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <fieldset>
            <h4>Contact Me!</h4>
            <label for="name">Name:</label>
                <input type="text" name="name" id="name"/>
                <label for="email"/>Email:</label>
                <input type="text" name="email" id="email"/>
                <label for="comments" id="comments">Comments:</label>
                <textarea name="comments" id=""></textarea>
                <fieldset>
                    <input class="btn" type="submit" name="submit"   class="submit" value="Send email"/>
                    <input class="btn" type="reset" value="Reset"/>
                </fieldset>
        </fieldset>
    </form>
7
  • 1
    What's the value of $mailsent after you try and send? try var_dump($mailsent) and see what the result is. Also, are you sure that the mail isn't going into a junk folder? Commented Aug 28, 2012 at 11:26
  • 1
    There should be some php error message too when you try to submit. Paste that here . Commented Aug 28, 2012 at 11:26
  • 1
    You have created a variable called $messages but are passing $message into the mail function. Is this how it is in your code or is that a typo only in the question? Commented Aug 28, 2012 at 11:27
  • 1
    <input type="submit"> will not submit itself even if you give it a name. So your $_POST["submit"] will always be empty. Commented Aug 28, 2012 at 11:28
  • 1
    So basically, OP's code isn't correct and he/she hasn't turned notices on. Commented Aug 28, 2012 at 11:29

2 Answers 2

2

Try this (I had to fix a number of things):

<?php
    $error = false;
    $sent = false;

    if(isset($_POST['submit'])) {
        if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) {
            $error = true;
        }
        else {
            $to = "[email protected]";

            $name = trim($_POST['name']);
            $email = trim($_POST['email']);
            $comments = trim($_POST['comments']);

            $subject = "Contact Form";

            $message =  "Name: $name \r\n Email: $email \r\n Comments: $comments";
            $headers = "From:" . $name;
            $mailsent = mail($to, $subject, $message, $headers);

            if($mailsent) {
                $sent = true;
            }
        }
    }
?>

<?php if($error == true){ ?>
<p class="error">Text</p>
<?php } if($sent == true) { ?>
<p class="sent">Text</p>
<?php } ?>
<div id="form">
    <form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <fieldset>
            <h4>Contact Me!</h4>
            <label for="name">Name:</label>
                <input type="text" name="name" id="name"/>
                <label for="email"/>Email:</label>
                <input type="text" name="email" id="email"/>
                <label for="comments" id="comments">Comments:</label>
                <textarea name="comments" id=""></textarea>
                <fieldset>
                <input class="btn" type="submit" name="submit"  class="submit" value="Send email"/>
                <input class="btn" type="reset" value="Reset"/>
                </fieldset>
        </fieldset>
    </form>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the effort
1
<script>
    window.scroll(0,0);
    $('#table_content').dataTable( {
        "bJQueryUI": true,
        "bProcessing": true,
        "sPaginationType": "full_numbers",
        "sAjaxSource": "../gym_facility_v0/load_facility.php"
    });

    $("#men a").click( function (){
        var link = $(this);

        $.ajax({ url: "../"+link.attr("href"),
            dataType: 'html',
            data: {post_loader: 1},
            success: function(data){
                $("#content").html(data);
            }});
            return false;
    });
</script>

<div class="title"><h5> Gym Facility</h5></div>

<div class="table">
    <div class="head"  id="men"><h5 class="iAdd"><a class="open-add-client-dialog" href="gym_facility_v0/form_facility.php"><i class="icon-plus"></i>Add Facility</a></h5></div>
        <div class="dataTables_wrapper" id="example_wrapper">
        <div class="">
        <div class="dataTables_filter" id="example_filter">

            <!--<label>Search: <input type="text" placeholder="type here...">
            <div class="srch">
            </div>
            </label>-->

        </div>
    </div>
    <table cellpadding="0" cellspacing="0" border="0" class="display" id="table_content">
        <thead>
            <tr>
            <th class="ui-state-default" rowspan="1" colspan="1" style="width: 2%;">
            <div class="DataTables_sort_wrapper">S.No
            </div></th>
            <th class="ui-state-default" rowspan="1" colspan="1" style="width: 227px;">
            <div class="DataTables_sort_wrapper">Gym Facility</div></th><th class="ui-state-default" rowspan="1" colspan="1" style="width: 130px;">
            <div class="DataTables_sort_wrapper"> Action</div></th></tr>
        </thead>

        <tbody><tr class="gradeA odd">
                   <td colspan="5" class="gradeA">Loading data from server</td>
               </tr>
        </tbody>
    </table>

</div><!-- End of .content -->

2 Comments

It will probably help the questioneer if you could elaborate on what your code does, why it helps with the problem and how it should be implemented.
Please improve your answer with some additional description, your currently answer might be unclear for future readers

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.