简体   繁体   English

单击表单提交后的Ajax请求

[英]Ajax request after form submission in single click

I have a form and in that form there is a QR code.QR code is generated based on form values. 我有一个表格,该表格中有一个QR码.QR码是根据表格值生成的。 When the submit button is pressed, what i need is 当按下提交按钮时,我需要的是

  1. Form values must submit to database 表单值必须提交到数据库
  2. Make ajax request for QR code and update qr code. 向ajax请求QR码并更新二维码。

     function getNewQrcode() { customRequest({ type : "GET", url: ROOT_PATH + "hotel/qrcode", success: function(data) { document.getElementById("qrcode-holder").innerHTML = data; }, error: function(err) { alert("Error" + err.Message) } }); } function formSub(){ $("#hotel_device_settings_form").submit(); } function combinedEvent(){ formSub(); getNewQrcode(); return true } 

    HTML button is HTML按钮是

     <button onclick="combinedEvent();" >SAVE</button> 

Every thing works when i click the button twice.On the first click form is submitting,On the second click QR code is updating. 当我单击按钮两次时,一切正常。在第一次单击表单上提交,在第二次单击QR代码更新。 Is it possible to do it in single click? 可以单击完成吗?

Create a iframe <iframe id="createdIframe" style="display: none;" ></iframe> 创建一个iframe <iframe id="createdIframe" style="display: none;" ></iframe> <iframe id="createdIframe" style="display: none;" ></iframe> and add the form 'target' property: <form target="createdIframe" > . <iframe id="createdIframe" style="display: none;" ></iframe>并添加表单“ target”属性: <form target="createdIframe" >

For example: 例如:

 function getSubmit() { document.forms[0].submit(); $.ajax({ url:'/action', data: { name: 'test' } }) } 
 form { } button { width: 100px; height: 30px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <iframe id="createdIframe" style="display: none;" ></iframe> <form action="/" target="createdIframe"> <input type="text" name="name" /> <br><br> <button onClick="getSubmit()">Submit form</button> </form> 

Make the ajax request twice. 发出两次ajax请求。 First send the data of the form through ajax. 首先通过ajax发送表单数据。 You can convert it to any suitable form (like JSON) and send it to the server. 您可以将其转换为任何合适的形式(如JSON)并将其发送到服务器。 After that make the ajax request again for the QRcode. 之后,再次向ajax请求QRcode。

Example: 例:

<script>
    function formSub(){

        // Convert the attributes accordingly (In JSON or anthing else) to be sent.

        var variable1 = $('#variable1').val();
        var variable2 = $('#variable2').val();

        var dataString = {"variable1" : variable1, "variable2" : variable2};
        var dataJson = JSON.stringify(dataString);

        // Do form Validation accordingly
        if(variable1 == "")
        {
           alert('Please enter the details');
           document.getElementById("variable1").focus();
        }

        // Make the post request to the desired page
        $.ajax({
            type: "POST",
            url: path,
            data : dataJson,
            success: function(result)
            {
                // Return success if the data insertion is successful
                if(result=="success")
                {
                    console.log("success");
                }
            },
            error : function(error)
            {
                console.log("error");
            }
        });
    }


    function getNewQrcode() {
        customRequest({
            type : "GET",
            url: ROOT_PATH + "hotel/qrcode",
            success: function(data) {
                document.getElementById("qrcode-holder").innerHTML = data;
            },
            error: function(err) {
                alert("Error" + err.Message)
            }
        });
    }


    function combinedEvent(){
        formSub();
        getNewQrcode();
        return true
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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