javascript - HTML Form 在不离开当前页面的情况下向 PHP Form 发送数据

标签 javascript php html

我在将数据从我的 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/

相关文章:

html - 如何在 Angular svg map 上选择区域?

php - 删除 str_word_count 的一些引号

javascript - 根据设备方向更改网格

javascript - 选中复选框时如何向文本区域添加值

javascript - 尝试从排列的 JSON 键中删除引号

javascript - 如何使用静音 Prop 取消静音 html5 视频

javascript - 从 Typescript 中的模块导出所有定义的速记

php - 将记录移动到新表并相应更新原始 ID

php - 将遗留应用程序移动到 Symfony2 : How to execute native php scripts in a controller?

javascript - 如何从javascript获取Bootstrap 3动态下拉模式对话框中的选定项目