我无法理解我在这个PHP脚本中出错的地方

时间:2017-08-03 10:59:31

标签: php html forms

我有一个普通的HTML表单,可以发送到sendemail.php脚本。

<?php
    header('Content-type: application/json');
    $status = array(
        'type'=>'success',
        'message'=>'Thank you for contact us. We will contact you as soon as possible.'
    );

    $name = @trim(stripslashes($_POST['name'])); 
    $email = @trim(stripslashes($_POST['email'])); 
    $number = @trim(stripslashes($_POST['number']));
    $company = @trim(stripslashes($_POST['company']));    
    $subject = @trim(stripslashes($_POST['subject'])); 
    $message = @trim(stripslashes($_POST['message'])); 

    $email_from = $email;
    $email_to = '';

    $body = 'Name: ' . $name . "\n\n" .'Number: ' . $number . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

    $success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

    echo json_encode($status);

    echo $email;
    die;

    ?>

它正在发送到指定的电子邮件地址,但它没有在电子邮件中显示变量。是否有人能够阐明我出错的地方。

        <form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
            <div class="col-sm-5 col-sm-offset-1">
                <div class="form-group">
                    <label>Name *</label>
                    <input type="text" name="name" id="name" class="form-control" required="required">
                </div>
                <div class="form-group">
                    <label>Email *</label>
                    <input type="email" name="email" id="email" class="form-control" required="required">
                </div>
                <div class="form-group">
                    <label>Phone</label>
                    <input type="number" name="number" id="number" class="form-control">
                </div>
                <div class="form-group">
                    <label>Company Name</label>
                    <input type="text" name="company" id="company" class="form-control">
                </div>                        
            </div>
            <div class="col-sm-5">
                <div class="form-group">
                    <label>Subject *</label>
                    <input type="text" name="subject" id="subject" class="form-control" required="required">
                </div>
                <div class="form-group">
                    <label>Message *</label>
                    <textarea name="message" id="message" required="required" class="form-control" rows="8"></textarea>
                </div>                        
                <div class="form-group">
                    <button type="submit" name="submit" class="floatright btn btn-primary btn-lg" required="required">Submit Message</button>
                    <br><br><Br><br>    
                </div>
            </div>
        </form>

使用上面的表单代码进行编辑

var form = $('#main-contact-form');
form.submit(function(event){
    event.preventDefault();
    var form_status = $('<div class="form_status"></div>');
    $.ajax({
        url: $(this).attr('action'),

        beforeSend: function(){
            form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
        }
    }).done(function(data){
        form_status.html('<p class="text-success">' + data.message + '</p>').delay(3000).fadeOut();
    });
});

在上面的相关JS中添加......

1 个答案:

答案 0 :(得分:0)

  1. 删除任何不必要的东西。
  2. 添加调试语句以检查输入和中间变量
  3. Enable debugging output并停止使用这些@前缀
  4. 再次运行并发布输出
  5. 所以:

    <?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    
    var_dump($_POST);
    $name = $_POST['name']; 
    $email = $_POST['email']; 
    $number = $_POST['number'];
    $company = $_POST['company'];    
    $subject = $_POST['subject']; 
    $message = $_POST['message']; 
    
    $email_from = $email;
    $email_to = '';
    
    $body = 'Name: ' . $name . "\n\n" .'Number: ' . $number . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
    var_dump($body);
    
    $success = mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
    var_dump($success);
    ?>
    

    更新:现在您告诉我们您通过ajax请求发送电子邮件,这一切都很清楚。 Ajax正在向服务器发送GET请求,该请求不包含表单中的任何数据。我对JQuery的理解有限:

    1. data属性添加到ajax调用并为其提供表单数据的json数组
    2. method属性添加到ajax调用并将其设置为POST

      var form = $('#main-contact-form'); form.submit(功能(事件){     event.preventDefault();     var form_status = $('');     $就({         url:$(this).attr('action'),         数据:[/ *你的表格数据在这里* /],         方法:'POST',         beforeSend:function(){             form.prepend(form_status.html('

      电子邮件正在发送......

      ')。fadeIn());         }     })。完成(功能(数据){         form_status.html(''+ data.message +'

      ')。delay(3000).fadeOut();     }); });

    3. 我不太了解JQuery是否有一种方便的方法将表单数据封装到json结构中。但是,通过他们的ID获取你的个人字段并获得他们的价值当然是可能的。