2013-05-03 64 views
2

如何根據點擊哪個發佈按鈕,將POST數據從我的表單發送到兩個不同的頁面?使用2個按鈕從一個表單發送POST數據

我的「導出」按鈕將POST數據發送到「export.php」,但「Graphique」按鈕必須將POST數據發送到「graphique.php」頁面。

這裏是我的代碼:

<form name="TCAgregat" class="TCAgregat" action="export.php" method="post"> 

    <input type="hidden" name="daco" value="<?php echo $DConso; ?>"></input> 
    <input type="hidden" name="CNS" value="<?php echo $CNewCentres; ?>"></input> 

    <input type=submit border="0" class="EXP" name="exp" value="EXPORT" /> 
    <input type=submit border="0" class="DCB" name="dcb" value="GRAPHIQUE" /> 
</form> 

我怎樣才能做到這一點?

+0

把他們分開電子表格? – 2013-05-03 09:41:38

+0

或者使用帶查詢字符串的javascript或acnhor標籤 – asprin 2013-05-03 09:41:51

+0

在此處使用ajax .. – 2013-05-03 09:41:59

回答

2

更改表單操作按鈕點擊:

$("form").on("click", ":submit", function(e) { 
    $(e.delegateTarget).attr('action', $(this).data('action')); 
}); 

HTML:

<input type=submit data-action="export.php" value="EXPORT" /> 
<input type=submit data-action="graphics.php" value="GRAPHIQUE" /> 

http://jsfiddle.net/FAnq9/

+0

這就是它非常感謝 – achillix 2013-05-03 09:59:41

3

action="handler.php"然後寫handler.php沿着線的東西:

<?php 
    if (isset($_POST['exp'])) { 
     include('export.php'); 
    } elseif (isset($_POST['dcb'])) { 
     include('graphique.php'); 
    } else { 
     // Default state/error handling 
    } 
?> 
+0

只是一個點。如果他想將數據發送到這兩個文件而不是一個呢? – itachi 2013-05-03 09:46:24

+1

然後,他可以將它們都包含在「if」語句的同一分支中。問題中沒有任何內容表明他可能想要。 – Quentin 2013-05-03 09:47:23

+0

他可能谷歌翻譯它。但第一行'如何發送相同的POST數據到不同的頁面'給我看,就像他試圖在同一時間在兩個不同的頁面上發佈一樣......只是說。 – itachi 2013-05-03 09:52:03

0

使用JavaScript重寫形式的action,然後submit()

+0

如果你打算這樣做(我不推薦它,因爲它引入了對JS的不必要的依賴),然後調用'submit()'是多餘的。只是不要首先取消表單的默認操作。 – Quentin 2013-05-03 09:44:51

+0

更多評論而非答案。至少應該舉一個例子。 – asprin 2013-05-03 09:50:37

0

既然你已經使用jQuery標籤吧,這裏是一個解決方案:

$(input[type="submit"]).click(function(){ 
var but = $(this).val(); 
var action = ''; 
if(but == 'EXPORT') 
{ 
    action = 'export.php'; 
} 
else if(but == 'GRAPHIQUE') 
{ 
    action = 'graphique.php'; 
} 

$('form').attr('action', action); 

}) 
相關問題