检查是否单击了按钮...无法正常工作

时间:2015-04-26 02:26:07

标签: javascript php html button isset

只是对我的代码的一部分做了一个非常简短的解释:

  • 我有两个按钮可以做不同的事情。
  • 其中一个允许用户在表/数据库中搜索他/她想要搜索的内容
  • 另一个允许用户将内容插入数据库

我在做什么: 我正在尝试检查用户点击了哪个按钮,以便执行相应的代码。

我一直在四处看看,我到处都是,人们建议使用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'])){
			
    <!-- Do something -->

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

}

为了测试它,我让if语句打印“Checked”,而else打印“Not checked”。当我运行我的代码时,它会打印“未选中”。我做错了什么,我该怎么办?

提前致谢!

4 个答案:

答案 0 :(得分: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就可以了。

答案 1 :(得分:1)

添加

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

进入搜索表单,然后是

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

进入插入表单。

之后。您需要在show(...)javascript函数

中提交(选定)表单

答案 2 :(得分: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

样本

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

答案 3 :(得分: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>