使用 Ajax 将 onBlur 事件发布到 php 脚本

发布于 2024-11-30 07:16:46 字数 154 浏览 3 评论 0原文

我正在努力降低客户的购物车放弃率。我希望能够在每次 onblur 触发时将表单数据从注册表单的各个输入字段发布到 php 脚本。

表单字段需要保留访问者输入的值,并且页面不应刷新,因此我认为 Ajax 是最好的解决方案。任何执行此操作的示例代码将不胜感激。

谢谢

I'm working on lowering shopping cart abandonment rates for a client. I'd like to be able to post form data from individual input fields of a registration form to a php script, every time onblur fires.

The form fields would need to keep the value entered by the visitor and page shouldn't refresh so I think Ajax is the best solution. Any example code that does this would be appreciated.

Thanks

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

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

发布评论

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

评论(1

忘羡 2024-12-07 07:16:46

像这样的东西可能对您有用:

<html>
    <head>
        <script type="text/javascript">
            function sendData(obj) {
                var xmlhttp;

                if (window.XMLHttpRequest) {
                    xmlhttp=new XMLHttpRequest();
                } else {
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }

                xmlhttp.onreadystatechange=function() {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                        // Do stuff when script returns
                        alert(xmlhttp.responseText);
                    }
                }

                xmlhttp.open("GET","script.php?" + obj.name + "=" + obj.value + "&t="+Math.random(),true);
                xmlhttp.send();
            }

        </script>
    </head>

    <body>
        Input 1 <input type="text" onblur="sendData(this)" name="input1" /><br />
        Input 2 <input type="text" onblur="sendData(this)" name="input2" /><br />
        Input 3 <input type="text" onblur="sendData(this)" name="input3" /><br />
    </body>
</html>

请求末尾的随机数是为了确保浏览器不会返回缓存的响应,而不是实际将数据发送到服务器。

Something like this would probably work for you:

<html>
    <head>
        <script type="text/javascript">
            function sendData(obj) {
                var xmlhttp;

                if (window.XMLHttpRequest) {
                    xmlhttp=new XMLHttpRequest();
                } else {
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }

                xmlhttp.onreadystatechange=function() {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                        // Do stuff when script returns
                        alert(xmlhttp.responseText);
                    }
                }

                xmlhttp.open("GET","script.php?" + obj.name + "=" + obj.value + "&t="+Math.random(),true);
                xmlhttp.send();
            }

        </script>
    </head>

    <body>
        Input 1 <input type="text" onblur="sendData(this)" name="input1" /><br />
        Input 2 <input type="text" onblur="sendData(this)" name="input2" /><br />
        Input 3 <input type="text" onblur="sendData(this)" name="input3" /><br />
    </body>
</html>

The random number at the end of the request is to make sure the browser doesn't return a cached response instead of actually sending your data to the server.

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