$ _POST变量未通过表单传递

时间:2014-04-12 20:14:15

标签: javascript php

我正在使用PHP在App Engine上托管一个简单的联系表单,尝试将$ _POST变量从表单传递到发送电子邮件的PHP脚本。正如我可以从日志中读到的那样,$ _POST变量似乎没有通过,我很难理解为什么...因此会欣赏另一双眼睛......谢谢。以下是(简化)代码的各个部分:

在index.html的根目录中:

<form method="post" action="#" id="contactform">
<div>
<label for="email">Enter your email</label>
<input type="text" class="input-field" id="email" name="email" value="">
</div>
<a id="button-send" href="#" title="Send Email" class="button" style="width:100%;">Send E-Mail</a>
<div id="success">Your message has been sent successfully!</div>
<div id="error">Unable to send your message, please try later.</div>
</form>

PHP文件,也在根目录:

<?php 
$send_email_to = "test@test.com";
$email_subject = "Email subject line";

function send_email($email,$email_message)
{
// using AppEngine's mail function here
}

function validate($email,$message)
{
// a simple validation function
}

if(isset($_POST['email'])) { 
$email = $_POST['email']; // this doesn't seem to work
}
else
{$email = "email@email.com";} // did this to confirm the $_POST didn't seem to be passed

$return_array = validate($email,$message);

if($return_array['success'] == '1')
{
send_email(,$email,$message);
}

header('Content-type: text/json');
echo json_encode($return_array);
die();
?>

控制错误消息的javascript:

$('#button-send').click(function(event){
    $('#button-send').html('Sending message...');
    event.preventDefault();

    $('html, body').scrollTo( $('#contact'), 'fast' );
    $.ajax({
        type: 'POST',
        url: 'send_form_email.php',
        data: $('#contact_form').serialize(),
        success: function(html) {
            if(html.success == '1')
            {
                $('#button-send').html('Send message');
                $('#success').show();
            }
            else
            {
                $('#button-send').html('Send message');
                $('#error').show();
            }                   
        },
        error: function(){
            $('#button-send').html('Send message');
            $('#error').show();
        }
    });

如果这与App Engine有关,我的app.yaml看起来像这样:

- url: /js
  static_dir: js

- url: /send_form_email.php*
  script: send_form_email.php

- url: .*
  script: index.html

再次感谢 - 我还将完整代码放在我的Google云端硬盘上:https://drive.google.com/file/d/0B4yzSrEzbZ5jbk1oc2RWb2xSRWM/edit?usp=sharing

1 个答案:

答案 0 :(得分:2)

您正在尝试序列化#contact_form,但您有id="contactform"

JSON的MIME类型为application/json,而不是text/json