doushi8186 2014-07-07 14:28
浏览 56
已采纳

表单帮助 - 将PHP脚本发送到通过ajax发送的电子邮件数据

Very new to this so apologies is my question is basic but it's stumped me for some hours.

I have a contact form which uses a bit of AJAX to POST to my PHP file. The problem im having is that when i try to get the PHP to email that data to myself, it isn't reading the POST values. The PHP was working when i tested it without the JS/AJAX post method, but in order to stop the page refresh I have gone with the AJAX option. Now that i've added the ajax in, the email result isn't showing up with the data (I can inspect and see that the Form Data is filled, just unclear why it isnt populating in the email) :

Name:   
Email:   
Message:

Any guidance as to why my mail server only receives the text but not the POST

Opening & Closing HTML on form:

<form data-parsley-validate action="./assets/contactform.php" method="POST">
        <div class="formRow">
                    <label for="name" class="obscure">Name</label>
                    <input type="text" name="nameInput" id="name" placeholder="Name" required data-parsley-error-message="Please enter your name"></input>
                </div>
                <div class="formRow">
                    <label for="email" class="obscure">Email</label>
                    <input name="emailInput" id="email" type="email" placeholder="Email" required data-parsley-error-message="Please enter a valid email address"></input>
                </div>
                <div class="formRow">
                    <label for="message" class="obscure">Message</label>
                    <textarea name="messageInput" id="message">Message</textarea>
                </div>
<input type="submit" name="submit"></input>

PHP Script:

<?php
    $to      = 'hello@kymlangford.com';
    $subject = 'Post from your website';
    $message = "Name: " . $_POST['nameInput'] . "
 Email: " . $_POST['emailInput'] . "
 Message: " . $_POST['messageInput'];
    $headers = 'From: hello@kymlangford.com' . "
" .
    'Reply-To: ' . $_POST['emailInput'] . "
" .
    'X-Mailer: PHP/' . phpversion();
    mail($to, $subject, $message, $headers);
    exit;
?>

Java for the form:

$(function() {

$.ajax({
        type: 'POST',
        url: './assets/contactform.php',
        data:  { name : $('input[name=nameInput]').val(), 
                 email : $('input[name=emailInput]').val(),
                message : $('textarea[name=messageInput]').val() 
               },
        dataType: 'json'
})
event.preventDefault();

});

});

--Update 08/07/2014--

Below is the jquery code that solved my problem

// get the data from the form 
$('form').submit(function(event){
    var fdata = $(this).serializeArray();
    fdata.push({ name: 'submit', value: true });
    $.post('./assets/contactform.php', fdata, function (data) { 
        alert('Data Loaded:' + data); });
        console.log(fdata);
        event.preventDefault(); 
    });
});
  • 写回答

1条回答 默认 最新

  • dsykx64220 2014-07-07 14:43
    关注

    I think you can use jquery which is the same like ajax.

    Here is the HTML code

    <html>
    <head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
    $(document).ready(function(){
    
      $('#submit').click(function(){
    
        $.post("sendemail.php", $("#contactform").serialize(), function(response) 
        {
           $('#message').html(response);
        });
    
      return false;
      });
    });
    </script>
    </head>
    <body>
    <p> 
    <form action="" method="post" id="contactform" >
    <label for="name">Name:</label><br />
    <input type="text" name="name" id="name" /><br />
    <label for="email">Email:</label><br />
    <input type="text" name="email" id="email" /><br />
    <label for="emailcontent">Email Content:</label><br />
    <textarea name="emailcontent" id="emailcontent"></textarea><br />
    <input type="button" value="Send Email" id="submit" /><div id="message"> </div>
    </form>
    </p>
    </body>
    </html>
    

    Here is the PHP code, save it with 'sendemail.php' filename

    $name = $_POST['name'];
    $email = $_POST['email'];
    $emailcontent = $_POST['emailcontent'];
    
    $to = 'youremail@domain.com';
    $subject = 'the subject';
    $emailcontent = 'FROM: '.$name.' Email: '.$email.'Content: '.$emailcontent;
    $headers = 'From: youremail@domain.com' . "
    ";
    
    mail($to, $subject, $message, $headers);
    echo "Your email was sent!";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示