doulan7166 2016-09-27 03:27
浏览 9

添加1个字段断开PHP联系表单

Everything was working fine until I added in the field for the phone number. If I remove it, the email gets sent.

I did type = 'tel' but it didn't work either. I tried any variations I could find to troubleshoot the problem and the only thing that works consistently is removing all traces of the phone field from html and php.

HTML:

<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" enctype="multipart/form-data">
                    <div class="form-group">
                        <input type="text" class="form-control" id="Name" name="name" placeholder="Name" required="required">
                    </div>
                    <div class="form-group">
                        <input type="email" class="form-control" id="Email" name="email" placeholder="Email">
                    </div>                    
                    <div class="form-group">
                        <input type="text" class="form-control" id="Phone" name="phone" placeholder="PhoneNumber">
                    </div>
                    <div class="form-group">
                        <textarea class="form-control" rows="3" name="message" placeholder="Message" required="requried"></textarea>
                    </div>
                    <button type="submit" name="submit" class="btn-block" >Send</button>
</form>

PHP (Snippet):

$name = @trim(stripslashes($_POST['name'])); 
    $clientemail = @trim(stripslashes($_POST['email'])); 
    $phone = (stripslashes($_POST['phone']));
    $message = @trim(stripslashes($_POST['message']));

    $body = 'Name: ' . $name . "

" . 'Email: ' . $clientemail . "

" . 'Phone: ' . $phone . "

" . 'Message: ' . $message;

     $email = new PHPMailer();      

    $email->From      = $clientemail;
    $email->FromName  = $name;
    $email->Subject   = 'Message from Website User';//$subject;
    $email->Body      = $body;  
    $email->AddAddress( 'johndoe@gmail.com' ); //Send to this email

 // EXPLICITLY TELL PHP-MAILER HOW TO SEND THE EMAIL... IN THIS CASE USING PHP BUILT IT MAIL FUNCTION    
        $email->isMail();

    header("Content-Type: application/json; charset=utf-8", true);

 // NOW, TRY TO SEND THE EMAIL ANYWAY:
        try{
            $success    = $email->send();
            $status['type']    = 'success';
            $status['message']  = 'Thank you for contacting us. We will reply as soon as possible.';   
        }catch(Exception $e){
            $status['type']     ='Error';
            $status['message']  ='Couldn\'t send the Email at this Time. Something went wrong';     
        }   

        // SIMPLY, RETURN THE JSON DATA...
 die(json_encode($status));

Here is my JS/AJAX:

   /***************** Contact Form Submission ******************/

$(document).ready(function (e){
$("#main-contact-form").on('submit',(function(e){
    e.preventDefault();
    $('#sendingemail').fadeIn();
        $.ajax({
        url: "../sendemail.php",
        type: "POST",
        data:  new FormData(this),
        contentType: false,
        cache: false,
        processData: false,
        success: function(data){
            console.log(data);
            if (data.type == 'Error') {
                    //alert(data.message);
                  $('#sendingemail').fadeOut();
                  $("#notsent").html(data.message).fadeIn();
                }
            else{
                $('#sendingemail').fadeOut();
                $('#emailsent').fadeIn();
              //alert(data.message);
            }
        },
      error: function(XHR,textStatus,errorThrown) {
            //alert("error");
              console.log(XHR.status + "..." + textStatus + "..." + errorThrown);
              alert(XHR.status);
            alert(textStatus);
            alert(errorThrown);
        }
    }           
 );
}));
});
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?