jQuery 无法正确发送表单

发布于 2024-12-09 02:56:40 字数 6944 浏览 0 评论 0原文

每次我提交此表单时,由于某种原因,它都会重新加载页面并将所有数据添加为 GET,而不是 POST 到服务器,

我想知道是否有人在以下代码中看到错误。

$("#updatebusiness").live('click', function(event){

        var tradingname = $('#tradingname').attr('value');
        var website     = $('#website').attr('value');
        var address     = $('#address').attr('value');
        var phonenumber = $('#phonenumber').attr('value');
        var email       = $('#email').attr('value');
        var postcode    = $('#postcode').attr('value');
        var state   = $('#state').val();
        var city    = $('#city').attr('value');
        var leadstatus  = $('#leadstatus').val();

        var sendemail   = $('#sendemail').val();
        var whichemail  = $('#whichemail').val();

        var notes       = $("#notes").val();
        var description     = $("#description").val();


        var callback    = $('#callback').val();
        var callbackdate    = $('#callbackyear').val()+"-"+$('#callbackmonth').val()+"-"+$('#callbackday').val();
        var callbacktime    = $('#callbackhour').val()+":"+$('#callbackmin').val();

        var companyname =   $('#companyname').attr('value');
        var acnabn      =   $('#acnabn').attr('value');
        var position    =   $('#position').attr('value');
        var title       =   $('#title').val();
        var firstname   =   $('#firstname').attr('value');
        var lastname    =   $('#lastname').attr('value');
        var industry    =   $('#industry').val();
        var subcategory =   $('#subcategory').val();
        var suburb1     =   $('#suburb1').attr('value');
        var suburb2     =   $('#suburb2').attr('value');
        var suburb3     =   $('#suburb3').attr('value');
        var suburb4     =   $('#suburb4').attr('value');
        var suburb5     =   $('#suburb5').attr('value');
        var suburb6     =   $('#suburb6').attr('value');
        var ms1         =   $('#ms1').attr('value');
        var ms2         =   $('#ms2').attr('value');
        var ms3         =   $('#ms3').attr('value');
        var ms4         =   $('#ms4').attr('value');
        var rcb1        =   $('#rcb1').attr('value');
        var rcb2        =   $('#rcb2').attr('value');
        var rcb3        =   $('#rcb3').attr('value');
        var rcb4        =   $('#rcb4').attr('value');
        var hotofferone =   $('#hotoffer1').attr('value');
        var hotofferrcb =   $('#hotofferrcb').attr('value');

        var hotofferstartdate   = $('#startday').val()+"-"+$('#startmonth').val()+"-"+$('#startyear').val();
        var hotofferfinishdate  = $('#finishday').val()+"-"+$('#finishmonth').val()+"-"+$('#finishyear').val();

        var terminalid  =   $('#terminalid').val();
        var bankname    =   $('#bankname').attr('value');
        var bsb         =   $('#bsb').attr('value');
        var cardtype    =   $('#cardtype').attr('value');
        var cardexpire  = $('#cardmonth').val()+"/"+$('#cardyear').val();

        if($('#terminalverified').is(':checked')) {
        var terminalverified    = "Yes";
        } else {
        var terminalverified    = "No";
        }

        var mobilenumber    =   $('#mobilenumber').attr('value');
        var faxnumber   =   $('#faxnumber').attr('value');
        var membershiptype  =   $('#membershiptype').attr('value');
        var keywords    =   $('#keywords').attr('value');
        var merchantid  =   $('#merchantid').attr('value');
        var bacname     =   $('#bacname').attr('value');
        var bacnumber   =   $('#bacnumber').attr('value');
        var crnumber    =   $('#crnumber').attr('value');
        var cvv         =   $('#cvv').attr('value');
        var incardallocation    =   $('#incardallocation').attr('value');


        var postaladdress   = $("#postaladdress").attr('value');
        var postalcity      = $("#postalcity").attr('value');
        var postalstate     = $("#postalstate").attr('value');
        var postalcode      = $("#postalcode").attr('value');
        var AMStatus        = $("#AMStatus").attr('value');

        if(usertype == 3 || usertype == 1)
        {
        var chosenusername      = $("#chosenusername").attr('value');
        var chosenpassword      = $("#chosenpassword").attr('value');
        }
        else
        {
        var chosenusername      = null;
        var chosenpassword      = null;
        }

        if(usertype == 1)
        {
        var leadowner   = $("#leadowner").val();
        }
        else
        {
        var leadowner   = $userid;
        }
        $.ajax({
            type: "POST",
            url: "system/classes/core.php",
            data: "task=updatebusinesslead&tradingname="+encodeURIComponent(tradingname)+"&website="+website+"&address="+address+"&phonenumber="+phonenumber+"&email="+email+"&postcode="+postcode+"&city="+city+"&state="+state+"&leadstatus="+leadstatus+"&sendemail="+sendemail+"&whichemail="+whichemail+"&callback="+callback+"&callbackdate="+callbackdate+"&callbacktime="+callbacktime+"&userid="+leadowner+"&companyname="+encodeURIComponent(companyname)+"&acnabn="+acnabn+"&position="+position+"&title="+title+"&firstname="+firstname+"&lastname="+lastname+"&industry="+industry+"&subcategory="+subcategory+"&suburbone="+suburb1+"&suburbtwo="+suburb2+"&suburbthree="+suburb3+"&suburbfour="+suburb4+"&suburbfive="+suburb5+"&suburbsix="+suburb6+"&msone="+ms1+"&mstwo="+ms2+"&msthree="+ms3+"&msfour="+ms4+"&rcbone="+rcb1+"&rcbtwo="+rcb2+"&rcbthree="+rcb3+"&rcbfour="+rcb4+"&hotofferone="+hotofferone+"&hotofferrcb="+hotofferrcb+"&hotofferstartdate="+hotofferstartdate+"&hotofferfinishdate="+hotofferfinishdate+"&terminalid="+terminalid+"&bankname="+bankname+"&bsb="+bsb+"&cardtype="+cardtype+"&cardexpire="+cardexpire+"&terminalverified="+terminalverified+"&mobilenumber="+mobilenumber+"&faxnumber="+faxnumber+"&membershiptype="+membershiptype+"&keywords="+keywords+"&merchantid="+merchantid+"&bacname="+encodeURIComponent(bacname)+"&bacnumber="+bacnumber+"&crnumber="+crnumber+"&cvv="+cvv+"&incardallocation="+incardallocation+"&postaladdress="+postaladdress+"&postalcity="+postalcity+"&postalstate="+postalstate+"&postalcode="+postalcode+"&notes="+encodeURIComponent(notes)+"&description="+encodeURIComponent(description)+"&chosenusername="+chosenusername+"&chosenpassword="+chosenpassword+"&AMStatus="+AMStatus+"&usertype="+usertype,
            success: function(data){
                if((data) == 1)
                {
                    loadadminlayout();
                //  alert(data);
                }
                else
                {
                alert(data);    
                }
            }
        });
        //continuebusinesslead();
return false;
});

Everytime I submit this form, instead of it POSTing to the server, for some reason it re-loads the page and adds all the data as a GET

I was wondering if anyone see an error in the following code.

$("#updatebusiness").live('click', function(event){

        var tradingname = $('#tradingname').attr('value');
        var website     = $('#website').attr('value');
        var address     = $('#address').attr('value');
        var phonenumber = $('#phonenumber').attr('value');
        var email       = $('#email').attr('value');
        var postcode    = $('#postcode').attr('value');
        var state   = $('#state').val();
        var city    = $('#city').attr('value');
        var leadstatus  = $('#leadstatus').val();

        var sendemail   = $('#sendemail').val();
        var whichemail  = $('#whichemail').val();

        var notes       = $("#notes").val();
        var description     = $("#description").val();


        var callback    = $('#callback').val();
        var callbackdate    = $('#callbackyear').val()+"-"+$('#callbackmonth').val()+"-"+$('#callbackday').val();
        var callbacktime    = $('#callbackhour').val()+":"+$('#callbackmin').val();

        var companyname =   $('#companyname').attr('value');
        var acnabn      =   $('#acnabn').attr('value');
        var position    =   $('#position').attr('value');
        var title       =   $('#title').val();
        var firstname   =   $('#firstname').attr('value');
        var lastname    =   $('#lastname').attr('value');
        var industry    =   $('#industry').val();
        var subcategory =   $('#subcategory').val();
        var suburb1     =   $('#suburb1').attr('value');
        var suburb2     =   $('#suburb2').attr('value');
        var suburb3     =   $('#suburb3').attr('value');
        var suburb4     =   $('#suburb4').attr('value');
        var suburb5     =   $('#suburb5').attr('value');
        var suburb6     =   $('#suburb6').attr('value');
        var ms1         =   $('#ms1').attr('value');
        var ms2         =   $('#ms2').attr('value');
        var ms3         =   $('#ms3').attr('value');
        var ms4         =   $('#ms4').attr('value');
        var rcb1        =   $('#rcb1').attr('value');
        var rcb2        =   $('#rcb2').attr('value');
        var rcb3        =   $('#rcb3').attr('value');
        var rcb4        =   $('#rcb4').attr('value');
        var hotofferone =   $('#hotoffer1').attr('value');
        var hotofferrcb =   $('#hotofferrcb').attr('value');

        var hotofferstartdate   = $('#startday').val()+"-"+$('#startmonth').val()+"-"+$('#startyear').val();
        var hotofferfinishdate  = $('#finishday').val()+"-"+$('#finishmonth').val()+"-"+$('#finishyear').val();

        var terminalid  =   $('#terminalid').val();
        var bankname    =   $('#bankname').attr('value');
        var bsb         =   $('#bsb').attr('value');
        var cardtype    =   $('#cardtype').attr('value');
        var cardexpire  = $('#cardmonth').val()+"/"+$('#cardyear').val();

        if($('#terminalverified').is(':checked')) {
        var terminalverified    = "Yes";
        } else {
        var terminalverified    = "No";
        }

        var mobilenumber    =   $('#mobilenumber').attr('value');
        var faxnumber   =   $('#faxnumber').attr('value');
        var membershiptype  =   $('#membershiptype').attr('value');
        var keywords    =   $('#keywords').attr('value');
        var merchantid  =   $('#merchantid').attr('value');
        var bacname     =   $('#bacname').attr('value');
        var bacnumber   =   $('#bacnumber').attr('value');
        var crnumber    =   $('#crnumber').attr('value');
        var cvv         =   $('#cvv').attr('value');
        var incardallocation    =   $('#incardallocation').attr('value');


        var postaladdress   = $("#postaladdress").attr('value');
        var postalcity      = $("#postalcity").attr('value');
        var postalstate     = $("#postalstate").attr('value');
        var postalcode      = $("#postalcode").attr('value');
        var AMStatus        = $("#AMStatus").attr('value');

        if(usertype == 3 || usertype == 1)
        {
        var chosenusername      = $("#chosenusername").attr('value');
        var chosenpassword      = $("#chosenpassword").attr('value');
        }
        else
        {
        var chosenusername      = null;
        var chosenpassword      = null;
        }

        if(usertype == 1)
        {
        var leadowner   = $("#leadowner").val();
        }
        else
        {
        var leadowner   = $userid;
        }
        $.ajax({
            type: "POST",
            url: "system/classes/core.php",
            data: "task=updatebusinesslead&tradingname="+encodeURIComponent(tradingname)+"&website="+website+"&address="+address+"&phonenumber="+phonenumber+"&email="+email+"&postcode="+postcode+"&city="+city+"&state="+state+"&leadstatus="+leadstatus+"&sendemail="+sendemail+"&whichemail="+whichemail+"&callback="+callback+"&callbackdate="+callbackdate+"&callbacktime="+callbacktime+"&userid="+leadowner+"&companyname="+encodeURIComponent(companyname)+"&acnabn="+acnabn+"&position="+position+"&title="+title+"&firstname="+firstname+"&lastname="+lastname+"&industry="+industry+"&subcategory="+subcategory+"&suburbone="+suburb1+"&suburbtwo="+suburb2+"&suburbthree="+suburb3+"&suburbfour="+suburb4+"&suburbfive="+suburb5+"&suburbsix="+suburb6+"&msone="+ms1+"&mstwo="+ms2+"&msthree="+ms3+"&msfour="+ms4+"&rcbone="+rcb1+"&rcbtwo="+rcb2+"&rcbthree="+rcb3+"&rcbfour="+rcb4+"&hotofferone="+hotofferone+"&hotofferrcb="+hotofferrcb+"&hotofferstartdate="+hotofferstartdate+"&hotofferfinishdate="+hotofferfinishdate+"&terminalid="+terminalid+"&bankname="+bankname+"&bsb="+bsb+"&cardtype="+cardtype+"&cardexpire="+cardexpire+"&terminalverified="+terminalverified+"&mobilenumber="+mobilenumber+"&faxnumber="+faxnumber+"&membershiptype="+membershiptype+"&keywords="+keywords+"&merchantid="+merchantid+"&bacname="+encodeURIComponent(bacname)+"&bacnumber="+bacnumber+"&crnumber="+crnumber+"&cvv="+cvv+"&incardallocation="+incardallocation+"&postaladdress="+postaladdress+"&postalcity="+postalcity+"&postalstate="+postalstate+"&postalcode="+postalcode+"¬es="+encodeURIComponent(notes)+"&description="+encodeURIComponent(description)+"&chosenusername="+chosenusername+"&chosenpassword="+chosenpassword+"&AMStatus="+AMStatus+"&usertype="+usertype,
            success: function(data){
                if((data) == 1)
                {
                    loadadminlayout();
                //  alert(data);
                }
                else
                {
                alert(data);    
                }
            }
        });
        //continuebusinesslead();
return false;
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

在你怀里撒娇 2024-12-16 02:56:40

您是否尝试过使用 jQuery 的 build in $.post() 方法?

您可以轻松地将代码缩小为:

$("#updatebusiness").live('click', function(event){
$.post("system/classes/core.php", $("#your-form-id").serialize(), function(data) {
if((data) == 1) {
loadadminlayout();
//  alert(data);
}
else
{
alert(data);    
}
});
});

Have you ever tried using jQuery's build in $.post() method?

you could easily shrink the code down to:

$("#updatebusiness").live('click', function(event){
$.post("system/classes/core.php", $("#your-form-id").serialize(), function(data) {
if((data) == 1) {
loadadminlayout();
//  alert(data);
}
else
{
alert(data);    
}
});
});
如若梦似彩虹 2024-12-16 02:56:40

您的 $("#updatebusiness") 是一个普通链接( 用户必须点击吗?如果是,那么您应该添加和 event.preventDefault() 来阻止浏览器去任何地方,或者更确切地说使用 <按钮>相反,

也:尝试 .serialize() 代替!

Is your $("#updatebusiness") an plain link (<a href="#" id="updatebusiness"> the user has to click? If yes then you should add and event.preventDefault() to prevent the browser from going anywhere, or rather use a <button> instead.

also: try .serialize() instead!

深居我梦 2024-12-16 02:56:40

由于您正在执行 POST .ajax 需要您传递的对象的“data”属性。您正在设置发布方法,然后将要发布到的 url 设置为 get。您需要序列化表单并将其分配给 data 属性,或者需要将 ajax 类型更改为“GET”

Since you are doing a POST .ajax is expecting the "data" property of the object you are passing it. You are setting the method to post and then setting up the url you are posting to as a get. You need to serialize your form and assign it to the data property or you need to change your ajax type to "GET"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文