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

时间:2012-05-20 23:53:37

标签: javascript jquery html

我想'推迟'(停止表单的默认行为)将用户发送到'http://www.othersite.com',直到check.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>

服务器端验证发生在check.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'上完成表单提交

2 个答案:

答案 0 :(得分:0)

$(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;
            }
        });
    });
});

答案 1 :(得分:0)

听起来像是AJAX的绝佳机会(正如@nachito所暗示的那样)。否则,你也可以:

  1. 最初check.php呈现表单并进行客户端验证。
  2. 将表单的操作设置为check.php(有效地回复给自己)。
  3. 成功验证后,将表单的操作设置为www.othersite.com并回显javascript文字值(或隐藏的输入,如果您愿意)标记“自动提交”。
    • 您也可以使用jQuery更改表单的操作,而不是在check.php中翻转。
  4. 检测到该标志后,请在document.ready事件中提交表单。
  5. 如果失败,如果使用check.php已经过了服务器端验证(并且没有,只需谷歌“HTTP POST from PHP, without cURL”),您也可以www.othersite.comcURL发帖。

相关问题