Php Ajax表单没有提交

时间:2022-12-07 00:07:49

Hey guys I am creating a newsletter sign-up form and trying to submit it with AJAX..

嘿,伙计们,我正在创建一个通讯注册表单,并试图用AJAX提交。

Here is my form:

这是我的形式:

<div id="form-content">
  <form method="POST" id="news-form" name="newsletter">
     <div class="bd-input-2 form-group">
        <input type="email" name="newsletter_email" placeholder="Enter your email address" required />
     </div>             
     <div class="form-group">
        <button type="submit" name="newsletter">Submit</button>
     </div>
   </form>
</div>

And this one is my JS file in same page as form:

这是我的JS文件和表格一样:

$('#news-form').submit(function(e){     
    e.preventDefault();
    $.ajax({
        url: 'newsletter-submit.php',
        type: 'POST',
        data: $(this).serialize()
    })
    .done(function(data){
        $('#form-content').fadeOut('slow', function(){
            $('#form-content').fadeIn('slow').html(data);
            console.log(data);
        });
    })
    .fail(function(){
        alert('Ajax Submit Failed ...');    
    });
});

On console nothing is displaying not even an error just an empty line.

在控制台,没有显示任何东西,甚至一个错误,只是空行。

And my newsletter-submit.php file :

和我的newsletter-submit。php文件:

<?php
if(isset($_POST['newsletter'])){
    $newsletter_email = filter_var($_POST['newsletter_email'],FILTER_VALIDATE_EMAIL);    
    if(filter_var($newsletter_email, FILTER_VALIDATE_EMAIL)){
        $newsletter_email = filter_var($newsletter_email, FILTER_VALIDATE_EMAIL);
        $em_check = sqlsrv_query($con, "SELECT email FROM newsletter_signups WHERE email='$newsletter_email'",array(), array("Scrollable"=>"buffered"));
        $num_rows = sqlsrv_num_rows($em_check); 
        if($num_rows > 0){
            echo "<br/><p style='color: #fff;'>Email exist in our newsletter list.</p>";
        }else{
            $query = "INSERT INTO newsletter_signups (email) VALUES ('{$newsletter_email}')";
            $insert_newsletter_query = sqlsrv_query($con,$query);
            echo '<br/><p style="color: green;">Thank you for sign up in our newsletter</p>';
        }
    }
}
?>

But if I add any code after php tags e.g Hello world that is displayed after the submission.

但是如果我在php标签e后面添加任何代码。在提交后显示的Hello world。

My php code was working before AJAX file

我的php代码在AJAX文件之前就开始工作了

1 个解决方案

#1


3  

Your input field is named newsletter_email and in your php you are checking for isset($_POST['newsletter']) which is always false.

您的输入字段被命名为newsletter (email),在php中,您将检查isset($_POST['newsletter']),这总是错误的。

#1


3  

Your input field is named newsletter_email and in your php you are checking for isset($_POST['newsletter']) which is always false.

您的输入字段被命名为newsletter (email),在php中,您将检查isset($_POST['newsletter']),这总是错误的。