2015-04-26 120 views
0

只不過想到了我的代碼的一部分確實非常簡短的解釋:檢查是否被點擊的按鈕......沒有工作

  • 我有兩個按鈕,做不同的事情。
  • 其中一人讓通過對任何他/她想要搜索
  • 其他表/數據庫中的用戶搜索允許用戶插入東西到數據庫

什麼,我想DO: 我想檢查用戶點擊哪個按鈕,以便執行相應的代碼。

我一直在環顧四周,幾乎無處不在,人們建議使用isset(),但它不適合我。也許我不完全理解isset()是做什麼的,但是它基本上不檢查是否設置了一個變量?

這裏是我的代碼:

<script> 
 
    function show(x, y){ 
 
     
 
     <!-- Do something --> 
 
     
 
    } 
 
</script> 
 

 

 
<form> 
 
    <button name = "sButton" type = "button" onclick = 'show("searchForm", "insertForm");'>Perform Search</button> 
 
    <button name = "iButton" type = "button" onclick = 'show("insertForm", "searchForm");'>Insert Data</button> 
 
</form> 
 

 

 
<form id = "searchForm" value "search" style = "display: none;" action = "test2.php" method = "post"> 
 

 
    <!-- Do something --> 
 
    
 
</form> 
 

 
<form id = "insertForm" style = "display: none;" action = "test2.php" method = "post"> 
 

 
    <!-- Do something --> 
 
    
 
</form>

<!-- This is the test2.php page --> 
 

 
if(isset($_POST['sButton'])){ 
 
\t \t \t 
 
    <!-- Do something --> 
 

 
} 
 
else{ 
 
\t \t \t 
 
    <!-- Do something --> 
 

 
}

爲了測試它,我有if語句打印 「檢查」 和其他打印「未選中」。當我運行我的代碼時,它會打印「未選中」。我做錯了什麼,我該怎麼做?

在此先感謝!

+0

你現在可以通過在'show'函數中檢查'x'和'y'的值來確定哪個按鈕被點擊了嗎? – sfletche

+0

如果您給這些按鈕一個名稱,那麼被點擊的名稱將與該表單一起提交。或者,偵聽器可以使用'show(「searchForm」,「insertForm」,this.name)''將按鈕的名稱傳遞給函數。爲什麼表單中的按鈕? – RobG

+0

@sfletche嗯,我覺得呢?我的按鈕按我希望的方式工作,所以我認爲它很好。 – wuv1

回答

1

您沒有將按鈕sButtoniButton傳遞到test2.php,因爲您的第二個和第三個表單沒有它們作爲輸入。只有每個特定表單中的輸入纔會被提交。並且具有按鈕的窗體只有按鈕調用JS函數時纔會有動作。

我建議你做的是添加隱藏字段您所提交test2.php如下每種形式:

<form id = "searchForm" value "search" style = "display: none;" action = "test2.php" method = "post"> 
    <input type="hidden" name = "sButton" value="sButton" /> 
    <!-- Do something --> 

</form> 

<form id = "insertForm" style = "display: none;" action = "test2.php" method = "post"> 
    <input type="hidden" name = "iButton" value="iButton" /> 
    <!-- Do something --> 

</form> 

這樣你test2.php應該工作。

+0

謝謝你的幫助!這解決了我的問題。 – wuv1

1

添加

<input type="hidden" name="sButton" /> 

到搜索的形式,和

<input type="hidden" name="iButton" /> 

到插入表格。

之後。您需要在節目中提交(選定)表單(...)javascript函數

+0

謝謝你的幫助! – wuv1

1

使用this

onclick = 'show(this.name, "searchForm", "insertForm");' 

實施例:

<button name = "sButton" type = "button" onclick = 'show(this.name, "searchForm", "insertForm");'>Perform Search</button> 


function show(name, x, y){ 

    alert(name); 

    if(name === "sButton"){ 
    do this.... 
    } 
} 

輸出:

sButton 

DEMO

http://codepen.io/tuga/pen/RPNaXY

+0

謝謝你的幫助! – wuv1

+0

不客氣@ wuv1 :) –

1

我不知道你想做什麼,但我不明白爲什麼按鈕的形式在所有。考慮動態連接聽衆,增加一個名字或ID的按鈕,這樣你可以告訴被點擊了其中一個,然後隱藏或顯示這取決於被點擊的形式:

// The buttons don't seem to need to be in a form, so use some other 
// container so you don't need to worry about a useless form being 
// submitted 
<div id="buttonContainer"> 
    <button id="searchButton">Perform search</button> 
    <button id="insertButton">Insert data</button> 
</div> 

// Forms for testing 
<form id="searchForm"><input value="search"></form> 
<form id="insertForm"><input value="insert"></form> 

,代碼:

<script> 

// Hide and show forms depending on which button was clicked using the 
// button's ID 
function showForm(event) { 

    // If the search button was clicked, show the search form and hide the 
    // input form 
    if (/search/.test(this.id)) { 
    document.getElementById('searchForm').style.display = ''; 
    document.getElementById('insertForm').style.display = 'none'; 

    // If the insert button was clicked, do the opposite 
    } else { 
    document.getElementById('searchForm').style.display = 'none'; 
    document.getElementById('insertForm').style.display = ''; 
    } 
} 

// Attach listeners to the buttons 
window.onload = function() { 
    Array.prototype.forEach.call(document.querySelectorAll('#searchButton, #insertButton'), 
    function(button) { 
     button.addEventListener('click', showForm, false); 
    } 
); 

    // Hide the forms 
    Array.prototype.forEach.call(document.querySelectorAll('#searchForm, #insertForm'), 
    function(form) { 
     form.style.display = 'none'; 
    } 
); 
} 

</script> 
+0

我想我一直在尋找如何在線創建按鈕,以及我看到的一些示例/教程,它們都在窗體中。謝謝您的幫助! – wuv1

相關問題