2013-07-31 89 views
2

我有一個包含多個提交按鈕的表單。多個提交按鈕,如何確定哪個被點擊?

每個提交按鈕是IMG SRC垃圾桶它表示的消息在一個基於Web的消息郵件收件箱中的刪除圖標

什麼是找出哪些提交按鈕圖標被點擊,這樣我就可以寫的最好辦法PHP/MySQL代碼刪除消息?

if(!empty($_POST)){ 
     // How do I figure out which submit button has been clicked to get the ID of the message to delete? 
} 

<form method="POST"> 
<input src="http://www.foo.com/img.png" id="button_1"> 
<input src="http://www.foo.com/img.png" id="button_2"> 
<input src="http://www.foo.com/img.png" id="button_3"> 
<input src="http://www.foo.com/img.png" id="button_4"> 
... 
<input src="http://www.foo.com/img.png" id="button_100"> 
</form> 
+0

糟糕抱歉..應該讀<輸入SRC =」 ...> – bobafart

+3

可能的[單個窗體上的多個提交按鈕]的重複項(http://stackoverflow.com/questions/6129173/multiple-submit-buttons-on-a-single-form) –

+1

可能重複的[Multiple在HTML表單中提交按鈕](http://stackoverflow.com/questions/48/multiple-submit-buttons-in-an-html-form) –

回答

4

設置value每個提交按鈕,然後檢查PHP並找到哪一個被點擊

<form method="POST"> 
<img src="http://www.foo.com/img.png" id="button_1" name="submit_btn" value="1"> 
<img src="http://www.foo.com/img.png" id="button_2" name="submit_btn" value="2"> 
<img src="http://www.foo.com/img.png" id="button_3" name="submit_btn" value="3"> 
<img src="http://www.foo.com/img.png" id="button_4" name="submit_btn" value="4"> 
... 
<img src="http://www.foo.com/img.png" id="button_100" name="submit_btn" value="100"> 
</form> 

echo $_POST['submit_btn'];會給你點擊提交按鈕的價值

+0

好的,謝謝..那我該如何查看哪一個被點擊? – bobafart

+0

查看編輯@bobafart –

+2

@bobafart你......只是沒有。停止。你的名字。我只是。什麼? –

0

你可以給一個namevalue到每個按鈕。然後,它會顯示下$_POST['submit']

<img src="http://www.foo.com/img.png" id="button_4" name='submit' value='4' /> 
2

給每個按鈕的名稱=「」

然後,你可以這樣做

isset($_POST['button_name']) { 
     // execute code here if true 
} 
+0

我不能這樣做,因爲可能有數百個按鈕..你如何解釋他們所有的人來檢查看看哪一個被點擊? – bobafart

-1

你必須通過刪除名稱和每個值的值來將你的值傳遞給當前文件..然後你可以在你的php腳本中回顯以便知道哪一個是點擊。

+0

如果您有問題,爲每個按鈕編寫長代碼,然後更好地使用循環.. –

1

此問題的解決方案是使用標記輸入/按鈕的NAME屬性。

<input type="submit" name="submitSave" value="Save"/> 
<input type="submit" name="submitAddComment" value="Add comment"/> 

<button type="submit" name="submitSave">Save</button> 
<button type="submit" name="submitAddComment">Add comment</button> 

我想你也可以使用按鈕標記的價值屬性,這是明確不可能與輸入的標籤。

如果需要使用的ID或其他變量,可以使用名稱= 「submitDelete [888]」 然後,用PHP檢查:

if(isset($_POST['submitDelete'])) { 
    echo key($_POST['submitDelete']);// Displays the ID to delete, e.g. 888. 
} 
相關問題