dourenzhou8740
2019-04-11 10:27
浏览 83
已采纳

Ajax没有将变量传递给另一个页面?

I want to save user email to database when he clicks the subscribe button. But when I enter email and click the subscribe button it passes an empty array!

Subscribe form code:

<form id="cx-subscribe-user">
    <input type="text" name="email" id="email" placeholder="Enter Your Email" />
    <button type="submit" id="subscribe-button">Subscribe</button>
</form>

Js code:

$('#cx-subscribe-user').on('click', function (e) {
    e.preventDefault();
    var email = $('#email').val();
    $.ajax({
        type: "GET",
        url: "subscribe-the-user.php",
        processData: false,
        dataType: 'json',
        data: {email: email},
        success: function(data) {
            console.log('saved scucess');
        }
    });
});

In subscribe-the-user.php file I'm trying to var_dump the request but it's printing empty array:

var_dump($_GET);exit;

In var_dump I receive this empty array array(0) {}

图片转代码服务由CSDN问答提供 功能建议

我想在点击订阅按钮时将用户 email 保存到数据库。 但是,当我输入电子邮件并单击订阅按钮时,它会传递一个空数组!

订阅表单代码:

 <  code>&lt; form id =“cx-subscribe-user”&gt; 
&lt; input type =“text”name =“email”id =“email”placeholder =“Enter your Email”/&gt; 
&lt;  button type =“submit”id =“subscribe-button”&gt;订阅&lt; / button&gt; 
&lt; / form&gt; 
   
 
 

Js代码:< /strong>

nn
$('#cx-subscribe-user').on('click',function(e){
 e.preventDefault(); 
  var email = $('#email')。val(); 
 $ .ajax({
 type:“GET”,
 url:“subscribe-the-user.php”,
 processData:false,  
 dataType:'json',
 data:{email:email},
 success:function(data){
 console.log('saved scucess'); 
} 
}); 
}  ); 
   
 
 

subscribe-the-user.php 文件中我正在尝试var_dump请求但它正在打印空数组:< / p>

 <代码>的var_dump($ _ GET);出口; 
   
 
 

var_dump 中,我收到这个空数组 array(0){}

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • drug95107 2019-04-11 10:32
    已采纳

    remove the processData, as documentation states, it will mess up your request

    processData (default: true)

    Type: Boolean

    By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false. https://api.jquery.com/jQuery.ajax/

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanchi6397 2019-04-11 10:34

    You can do that using POST method

    $('#cx-subscribe-user').on('click', function (e) {
        e.preventDefault();
        var email = $('#email').val();
        $.ajax({
            type: "POST",
            processData: false,
            dataType: 'json',
            data: {email: email},
            url: "subscribe-the-user.php",
            success: function(data) {
                console.log('saved success');
            }
        });
    });
    
    评论
    解决 无用
    打赏 举报
  • dpsu84620 2019-04-11 10:34

    Emails need some encoding before send your request

    $.ajax({
        url: "ajax.php",
        type:'get',
        data: { email: encodeURIComponent(email)},
        dataType: 'json',
        success: function(response) {
          //Do Something
        },
        error: function(xhr) {
        //Do Something to handle error
        }
    });
    
    评论
    解决 无用
    打赏 举报
  • doulang9521 2019-04-11 10:34

    The problem is you are using an onclick() event on the <form> object, so as soon as you click in email input field to add some data, form onclick() event is fired and the ajax request will happen without any data in the email field hens submitting an empty email parameter to the PHP.

    Change form onclick() to button click() or form submit()

    $('#subscribe-button').on('click', function (e) {
    

    Also remove processData: false, [based on documentation:- jQuery ajax documentation]

    processData (default: true) Type: Boolean By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题