gpt4 book ai didi

javascript - $_POST 变量未通过表单传递

转载 作者:行者123 更新时间:2023-11-30 17:31:13 24 4
gpt4 key购买 nike

我在 App Engine 上使用 PHP 托管一个简单的联系表单,试图将 $_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

最佳答案

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

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

关于javascript - $_POST 变量未通过表单传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23035597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com