2012-12-05 21 views
0

我有一個表格,例如:保存數據通過郵寄方式發送,當它

<form method="post" action="some external URL"> 
    <input type="hidden" id="id" name="session_id" /> 
    <input type="hidden" id="val1" name="val1" /> 
    <input type="hidden" id="val2" name="val2" /> 
    <input type="submit" value="Send" /> 
</form> 

我需要它發送到「一些外部鏈接」前保存這些數據到MySQL - 讓我們說,我的「保存到數據庫「腳本在」腳本/保存「網址上。在通過此表單發送之前,如何將數據傳輸到那裏?

編輯

這將是很好,當我的形式將只發送到「一些外部URL」的時候,「腳本/保存」給出了肯定的答覆。 你能幫我解決嗎?

+2

您可能希望編寫一個接受發佈數據的內部腳本,爲您的數據庫處理它,然後將帖子張貼到外部URL。 – Stegrex

+0

是的 - 但不是一些Ajax解決方案更好? – pawel

+0

Ajax可能會更好,但您可能遇到相同原產地政策的問題。 – Stegrex

回答

0

嘿只是使用Ajax調用的onsubmit然後提交表單當Ajax調用完成

<html> 
<head> 
<script type="text/javascript"> 
    function doSomething() 
    { 
     xmlHttp = new XMLHttpRequest(); 
     xmlHttp.open('GET', 'yourscript.php, true); 
     xmlHttp.onreadystatechange = function(){ 
      if (xmlHttp.readyState == 4 && xmlHttp.status == 200){ 
       var response = xmlHttp.responseText; 
       // response is what you return from the script 
       if(response == 1){ 
        alert('works'); 
        form[0].submit; 
       }else{ 
        alert('fails'); 
       } 
      }else{ 
       alert('fails'); 
      } 
     } 
     xmlHttp.send(null); 
    } 
</script> 
</head> 
<body> 
    <form action="external-url" method="post" onsubmit="doSomething();"> 
     <input type="text" name="blah" id="blah"> 
     <input type="submit" value="save"> 
    </form> 
</body> 
</html> 
+0

你能檢查我的問題的編輯嗎? – pawel

+0

你去了。檢查我的編輯。 – SeanWM

+0

你也可以使用jQuery進行ajax調用以及http://api.jquery.com/jQuery.ajax/ – SeanWM

0

數據庫訪問在服務器上處理,這是您的表單正在傳送到的內容。因此,爲了做到這一點,請將表單重新路由到服務器上的其他腳本;這個腳本將寫入數據庫,然後調用「一些外部URL」。

0

您需要在表單的onsubmit事件期間使用ajax將數據發送到您的url。以下代碼可能適用於您。

$("form").submit(function(){   
    $(this).ajaxSubmit({url: 'script/save', type: 'post'}); 
}); 
+0

你可以檢查我的問題的編輯? – pawel

0

而是可以發佈您的形式向外部URL的,它發佈到內部腳本:

<?php 
// Process your $_POST here and get all the stuff you need to process your SQL statements. 

// Then, POST to the external URL: 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "your external URL"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, true); 

curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST); 
$output = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch); 
?>