contact-form.php无效

时间:2016-07-31 10:26:37

标签: php forms

我的php联系表单没有向我发送任何电子邮件。请帮帮我。

表单代码:

                        <form action="php/contact-form.php" id="contact-form">
                      <div class="alert alert-success hidden" id="contact-alert-success" dir="rtl"> <strong>זה עבד!</strong> תודה שיצרת קשר. נחזור אליך מיד! </div>
                      <div class="alert alert-danger hidden" id="contact-alert-error" dir="rtl"> <strong>שגיאה!</strong> משהו השתבש בשליחת ההודעה. </div>
                      <div class="form-group" dir="rtl">
                        <label class="sr-only" for="name"></label>
                        <input dir="rtl" type="text" class="form-control" value="" placeholder="שם" data-msg-required="נא להזין שם." name="name" id="name">
                      </div>
                                                <div class="form-group" dir="rtl">
                        <label  class="sr-only" for="phone"></label>
                        <input type="text" value="" placeholder="טלפון" data-msg-required="נא להזין טלפון." data-msg-email="נא להזין טלפון תקין." class="form-control" name="phone" id="phone"  maxlength="10">
                      </div>
                      <div class="form-group" dir="rtl">
                        <label class="sr-only" for="email"></label>
                        <input type="email" value="" placeholder="דואר אלקטרוני" data-msg-required="נא להזין דואר אלקטרוני." data-msg-email="נא להזין כתובת תקינה." class="form-control" name="email" id="email">
                      </div>
                      <div class="form-group" dir="rtl">
                        <label class="sr-only" for="message"></label>
                        <textarea dir="rtl" placeholder="נא להזין את ההודעה..." data-msg-required="יש להזין הודעה." rows="6" class="form-control" name="message" id="message"></textarea>
                      </div>
                      <input type="submit" value="שלח הודעה" class="btn btn-default btn-block" dir="rtl" data-loading-text="טוען...">
                    </form>

contact-form.php文件是

<?php
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));

header('Content-type: application/json');

// Enter your email address
$to = 'omanbbb@gmail.com';


if($to) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];

    $fields = array(
        0 => array(
            'text' => 'Name',
            'val' => $_POST['name']
        ),
        1 => array(
            'text' => 'Email address',
            'val' => $_POST['email']
        ),
        2 => array(
            'text' => 'Message',
            'val' => $_POST['message']
        ),      
        3 => array(
            'numbers' => 'Phone',
            'val' => $_POST['phone']
        )
    );

    $message = "";

    foreach($fields as $field) {
        $message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
    }

    $subject = "New message from: $name";

    $headers = '';
    $headers .= 'From: ' . $name . ' <' . $email . '>' . "\r\n";
    $headers .= "Reply-To: " .  $email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

    if (mail($to, $subject, $message, $headers)){
        $arrResult = array ('response'=>'success');
    } else{
        $arrResult = array ('response'=>'error');
    }

    echo json_encode($arrResult);

} else {

    $arrResult = array ('response'=>'error');
    echo json_encode($arrResult);

}
?>

非常感谢。

1 个答案:

答案 0 :(得分:0)

您没有在表单中包含该方法,例如:

<form action="php/contact-form.php" id="contact-form" method="post">

这将允许您的contact-form.php访问post超级全局数组中表单输入的值。