使用 JQuery 执行服务器端验证,然后将用户发送到另一个站点

Use JQuery to do server side validation then send user to another site

本文关键字:用户 站点 另一个 然后 执行 JQuery 服务器端 验证 使用      更新时间:2023-09-26

我想"推迟"(停止表单的默认行为(将用户发送到"http://www.othersite.com",直到服务器端验证在检查时完成.php

完成后,将用户发送到"http://www.othersite.com"。我可以发送到验证页面没有问题,但我需要验证然后发送到另一个网站。假设我有这样的表格

$(document).ready(function(){
$('#sendbutton').click(function(){
$('#error').empty();
$('#send').ajaxForm(function (data, textStatus) {
$('#error').append(data);
});
});
});
<form id="send" name="send" method="post" action="http://www.othersite.com/index.htm">
<input type="text" id="fname" name="fname" />
<input type="text" id="lname" name="lname" />
<input type="text" id="address" name="address" />
<input type="text" id="city" name="city" />
<input type="text" id="state" name="state" />
<button id="sendbutton">Submit</button>
</form>
<div id="error"></div>

服务器端验证在检查时进行.php

check field inputs here on check.php
if not correct
echo error message
if successful echo "validation_ok"

如果返回的数据为"validation_ok",则让用户在"http://www.othersite.com"上完成表单提交

听起来像是

AJAX 的绝佳机会(正如@nachito所暗示的那样(。否则,您还可以:

  1. check.php最初呈现表单并执行客户端验证。
  2. 让表单的操作check.php(有效地回发到自身(。
  3. 成功验证后,将表单的操作设置为www.othersite.com并回显 javascript 文本值(或隐藏输入,如果您倾向于这样做(,标记为"自动提交"。
    • 您还可以使用 jQuery 来更改表单的操作,而不是在 check.php 中翻转它。
  4. 检测到标志后,提交有关document.ready事件的表单。

如果做不到这一点,你也可以check.php发布到www.othersite.com如果服务器端验证已经通过cURL(没有,只是谷歌"来自PHP的HTTP POST,没有cURL"(。

$(document).ready(function() {
    $('#send').submit(function() {
        $('#error').empty();
        // Post the form to your validation page
        $.post('/check.php', $(this).serialize(), function (data) {
            // If validation failed, stop submission
            if (data != 'validation_ok') {
                $('#error').append(data);
                // Prevent submission.
                return false;
            }
        });
    });
});