检查是否单击了按钮...无法正常工作
Check to see if a button is clicked on... not working
只是对我的部分代码所做的非常简短的解释:
- 我有两个按钮可以做不同的事情。
- 其中之一允许用户在 table/database 中搜索 he/she 想要搜索的任何内容
- 另一个让用户将东西插入数据库
我想做什么:
我试图检查用户单击了哪个按钮,以便执行适当的代码。
我一直在环顾四周,几乎到处都有人建议使用 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"。当我 运行 我的代码时,它打印 "Not checked"。我做错了什么,我应该做什么?
提前致谢!
您没有将按钮 sButton
或 iButton
传递给 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 就可以了。
添加一个
<input type="hidden" name="sButton" />
进入搜索表单,然后
<input type="hidden" name="iButton" />
进入插入表格。
之后。您需要在 show(...) javascript function
中提交(选定的)表单
使用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
演示
我不确定您要做什么,但我根本不明白为什么按钮会出现在表单中。考虑动态附加侦听器,向按钮添加名称或 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>
只是对我的部分代码所做的非常简短的解释:
- 我有两个按钮可以做不同的事情。
- 其中之一允许用户在 table/database 中搜索 he/she 想要搜索的任何内容
- 另一个让用户将东西插入数据库
我想做什么: 我试图检查用户单击了哪个按钮,以便执行适当的代码。
我一直在环顾四周,几乎到处都有人建议使用 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"。当我 运行 我的代码时,它打印 "Not checked"。我做错了什么,我应该做什么?
提前致谢!
您没有将按钮 sButton
或 iButton
传递给 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 就可以了。
添加一个
<input type="hidden" name="sButton" />
进入搜索表单,然后
<input type="hidden" name="iButton" />
进入插入表格。
之后。您需要在 show(...) javascript function
中提交(选定的)表单使用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
演示
我不确定您要做什么,但我根本不明白为什么按钮会出现在表单中。考虑动态附加侦听器,向按钮添加名称或 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>