doupan6648 2014-10-06 15:51
浏览 100
已采纳

提交多个表单相同的jquery脚本

I have multiple forms and I want all of them to be processed by a single jquery script, of course I have php functions that work correctly, I tried them separately.

This is my script:

function proceso_form(type_form, id_div_error){

    var url = "my_url.php?form="+type_form; //functions
    var response = document.getElementById(id_div_error);

        response.innerHTML="<img src='img/loader.gif' style='margin-right: 5px;'/>Loading ..."; //
        response.style.display='block';

        $.ajax({
               type: "POST",
               url: url,
               data: $(this).serialize(), //ID form
               success: function(data)
                {
                    if (data==1){
                        window.location.reload();
                    }else{
                        response.innerHTML=data; // show PHP response.
                    }
                }
        });

        return false;
};

My form looks like this

<form id="contacto" name="contacto" method="post" onsubmit="proceso_form('contacto', 'cargando')">
                <input type="text"  name="name"class="form-control">
                <input type="text"  name="phone" class="form-control">
                <input type="email" name="email" class="form-control">
                <textarea style="height:100px;margin-bottom:0px" name="messaje" class="form-control"></textarea>

                <input style="margin-top:5px" type="submit" class="btn btn-block" value="SEND">
                </form>

I think my problem is that I can't put my script in the onsubmit, but honestly I have no idea.

  • 写回答

2条回答 默认 最新

  • donglu9896 2014-10-06 20:31
    关注

    Your html must look like

    <form id="contacto" name="contacto" method="post" onsubmit="return proceso_form(this, 'cargando')">
    ...
    </form>
    

    And inside the function:

    function proceso_form(form, id_div_error){
    
        var $form = $(form);
        var url = "my_url.php?form="+$form.attr('id'); //functions
        var response = document.getElementById(id_div_error);
    
        response.innerHTML="<img src='img/loader.gif' style='margin-right: 5px;'/>Loading ..."; //
        response.style.display='block';
    
        $.ajax({
               type: "POST",
               url: url,
               data: $form.serialize(), //ID form
               success: function(data)
                {
                    if (data==1){
                        window.location.reload();
                    }else{
                        response.innerHTML=data; // show PHP response.
                    }
                }
        });
    
        return false;
    };
    

    By passing this to the function you passing the whole form reference.

    Hope it will help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号