2011-02-17 66 views
0

我查找了解決方案,但只找到了在我自己的網站上提交表單的方法。我想在這裏完成的是2件事。 1:將表單數據從我的網站表單發送到第二個網站上的表單。 2.這樣做,而不刷新我的網站。將表單數據發送到外部表單而不刷新頁面

這是我的表格。它提交的很好,但在表單發送後重定向到外部網頁。

<form id="widget_contact" action="http://www.website.com/" method="post"> 
    <input type="text" name="pcode" id="fc_name" /> 
    <input type="hidden" name="hdnCmd" value="sb-gimme" /> 
    <input name="send_button" id="fc_submit" class="btn_b" type="submit" value="Gimme" /> 
</form> 
+0

您選擇了ajax和jquery作爲標籤。您是否嘗試通過ajax發送數據?代碼在哪裏? – DKSan 2011-02-17 12:16:39

回答

0

你可以做的是設置一個PHP文件,它將接受表單數據,然後使用HTTP POST將數據發送到其他網站。您將使用Ajax將數據發送到您自己的PHP文件以避免刷新。下面是如何POST數據發送到使用PHP其他網站爲例(這是唯一的東西,我只是用Google搜索資料來源:http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl

<?php 
function do_post_request($url, $data, $optional_headers = null) 
{ 
    $params = array('http' => array(
       'method' => 'POST', 
       'content' => $data 
      )); 
    if ($optional_headers !== null) { 
    $params['http']['header'] = $optional_headers; 
    } 
    $ctx = stream_context_create($params); 
    $fp = @fopen($url, 'rb', false, $ctx); 
    if (!$fp) { 
    throw new Exception("Problem with $url, $php_errormsg"); 
    } 
    $response = @stream_get_contents($fp); 
    if ($response === false) { 
    throw new Exception("Problem reading data from $url, $php_errormsg"); 
    } 
    return $response; 
} 

你也可以使用捲曲這是一個更容易一點,但要求捲曲被安裝。

0

我正在處理提交請求給返回JSON文件的Web服務的頁面。我的請求導致重定向到JSON文件。不是我所需要的。使用下面的代碼解決了問題:

var xmlhttp = new XMLHttpRequest(); 
<!-- Get Excel Spreadsheet Templates. --> 
var url = "https://store.office.com/assets/templates/get?culture=en-CA&sourceIds=TM10000104,TM10000108,TM10000093,TM10000101,TM10000089,TM10000094,TM10000098,TM10000110,TM10000092,TM10000109,TM10000099,TM10000105,TM10000103,TM10000102,TM10000091,TM10000090,TM10000106,TM10000107,TM10000095,TM10000111&format=json"; 

xmlhttp.onreadystatechange=function() { 
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
     myFunction(xmlhttp.responseText); 
    } 
} 
xmlhttp.open("GET", url, false); 
xmlhttp.send(); 

function myFunction(response) { 
    // Do what you need to do here, if anything! 
} 

您可以用提交url替換url變量。 GET也可以用POST替換。

結賬:http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp。這詳細介紹了從服務器獲取數據和向服務器發佈數據。

希望這會有所幫助。

相關問題