简体   繁体   中英

PHP can't get AJAX data

i'm trying to send variable data to my database.php file but php can't get it

the Validate() method works when i click on my submit button which has an onlick event.

here's my Validate() function

function Validate()
        {
            var frm=jQuery('#AdminForm');
            var valid="false";

             jQuery.ajax({  
                type: frm.attr('method'),           
                url: frm.attr('action'),                
                data: 'var='+valid,
                success: function(data) 
                {
                    alert(data);
                }
                error: function(xhr, ajaxOptions, thrownError)
                {
                    alert("error: "+thrownError);
                }
            });

        }

here's my php file

<?php
   echo $_POST["var"];
?>

but php leads to an error which says: undefined index var

please help me to fix this.

根据您的说法,您正在为表单使用“ post”方法,但是您在onclick的“ submit”按钮上调用了Validate函数,最好将按钮的类型更改为“ button”,或者通过以下方式停止表单的默认行为:添加onsubmit =“ return false;”

You are submitting your form via GET which is default for jQuery ajax, so you will need to check for

$_GET['var']

Other way would be to change your ajax call to

jQuery.ajax({  
                type: frm.attr('method'),     // make sure this is post      
                url: frm.attr('action'),                
                data: {
                    var : valid
                }
                success: function(data) 
                {
                    alert(data);
                }
                error: function(xhr, ajaxOptions, thrownError)
                {
                    alert("error: "+thrownError);
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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