2012-05-20 13 views
0

我想'延期'(停止表單的默認行爲)發送用戶到'http://www.othersite.com',直到服務器端驗證完成check.php使用JQuery做服務器端驗證,然後將用戶發送到另一個站點

一旦完成,發送用戶到他們的方式'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'

回答

0

聽起來像是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通過(並且沒有,只是谷歌「HTTP POST from PHP, without cURL」)。

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; 
      } 
     }); 
    }); 
}); 
+0

返回的數據打開一個新頁面,並在那裏寫入錯誤消息,而不是在錯誤div中。我有jquery.form.js,干擾腳本嗎? – Patriotec

+0

實際上,它仍然在我的測試頁上發佈到表單操作中設置的頁面 – Patriotec

相關問題