简体   繁体   English

HTML Form 发送数据到 PHP Form 不离开当前页面

[英]HTML Form sends data to PHP Form without leaving the current page

I'm having issues with sending data from my HTML Form to my PHP page (via JS/AJAX) while staying on my current page.我在将数据从我的 HTML 表单发送到我的 PHP 页面(通过 JS/AJAX)时遇到问题,同时停留在我的当前页面上。 Here is what I currently have in place:这是我目前所拥有的:

HTML Section: 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:

<?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: 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>

If I don't try to keep it on the current page with JS/AJAX it'll direct to the PHP page just fine and subscribe the user to the mailing list.如果我不尝试使用 JS/AJAX 将其保留在当前页面上,它将直接指向 PHP 页面,并将用户订阅到邮件列表。 Any ideas as to what my issue may be?关于我的问题可能是什么的任何想法?

Thanks!谢谢!

这一行data: email,应该是data: {email: email},

try this尝试这个

<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: email}, // don't data: email,
                    success: function(){
                        $('.success').fadeIn(200).show();
                        $('.error').fadeOut(200).hide();
                    }
                });
            }
            return false;
        });
    });
</script>

You're using $('.submit').click() , but there is no element with class submit !您正在使用$('.submit').click() ,但没有类submit元素! Your submit button is class button instead.您的提交按钮是类button

You should use $('#signup-form').submit() instead and change your <button> to an <input type="submit"> .应该改用$('#signup-form').submit()并将您的<button>更改为<input type="submit">

Try尝试

$.ajax({
type: "POST",
url: "subscribeEmail.php",
data: {email:email},
success: function(){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});

It should be "email="+email应该是“email=”+email

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM