我在将数据从我的 HTML 表单发送到我的 PHP 页面(通过 JS/AJAX)时遇到问题,同时停留在我的当前页面上。这是我目前拥有的:
HTML 部分:
<!-- Begin the Form -->
<form id="signup-form" method="POST">
<!-- Input of E-Mail -->
<div class="seven columns">
<input type="email" name="email" id="email" placeholder="Your E-Mail">
</div>
<!-- Send Button -->
<div class="five columns">
<button name="submit" type="button" class="button">Subscribe</button>
</div>
<!-- End of the Form -->
</form>
PHP:
<?php
require_once 'MCAPI.class.php';
$apikey = 'XXXXXX';
$list_id ='XXXXXX';
$api = new MCAPI($apikey);
$email = $_POST['email'];
echo "Email: ".$email;
$merge_vars = array(' ');
// By default this sends a confirmation email - you will not see new members
// until the link contained in it is clicked!
$retval = $api->listSubscribe( $list_id, $email, $merge_vars );
if ($api->errorCode){
echo "Unable to load listSubscribe()!\n";
echo "\tCode=".$api->errorCode."\n";
echo "\tMsg=".$api->errorMessage."\n";
} else {
echo "E-Mail submitted to the pxCollective Newsletter!";
}
?>
JS:
<script type="text/javascript" >
$(function() {
$(".submit").click(function() {
var email = $("#email").val();
if(email=='')
{
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}
else
{
$.ajax({
type: "POST",
url: "subscribeEmail.php",
data: email,
success: function(){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
</script>
如果我不尝试使用 JS/AJAX 将它保留在当前页面上,它会很好地定向到 PHP 页面并为用户订阅邮件列表。关于我的问题可能是什么的任何想法?
谢谢!
最佳答案
这一行 data: email,
应该是 data: {email: email},
关于javascript - HTML Form 在不离开当前页面的情况下向 PHP Form 发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22543881/