我有一个包含2个表单的页面。这是使用PHP / PDO插入db。我面临的问题是插入页面上的IF语句产生错误,即使我没有按下提交按钮。尝试了几种方法与$ _POST isset /!== 0等等。到目前为止没有运气。
jQuery / FORM-combo会出现问题吗?也许这会触发$ _POST?
用于选择表单的jQuery:
<script type="text/javascript">
$(document).ready( function() {
$('#formSel').change( function() {
var id = $(this).val();
if( id != '-' )
{
$('form').hide();
$('#form'+id).show();
//$('#myModal').modal('show');
}
});
});
</script>
表单设置如下:
<div>
<select class="form-control input-small" id="formSel">
<option value="-">Choose category</option>
<option value="One">One</option>
<option value="Two">Two</option>
</select>
</div>
<!-- Form One -->
<form id="One" style="display:none" method="POST" action="insert.php">
<input type="text" id="aaa" placeholder="aaa">
<input type="text" id="bbb" placeholder="bbb">
<button type="submit" value="btn-One">Submit</button>
</div>
<!-- Form Two -->
<form id="One" style="display:none" method="POST" action="insert.php">
<input type="text" id="ccc" placeholder="ccc">
<input type="text" id="ddd" placeholder="ddd">
<button type="submit" value="btn-Two">Submit</button>
</div>
要插入数据,我有一个包含PHP / PDO的第二页(insert.php)。这被设置为在按下提交按钮时执行:
if ($_SERVER['REQUEST_METHOD'] == 'POST' && (!empty($_POST['btn-One']))) {
//execute code
}
else {
echo 'Error submit One';
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && (!empty($_POST['btn-Two']))) {
//execute code
}
else {
echo 'Error submit Two';
}
我完全没有想法和技能来看待解决方案。希望有人能够发现触发表格的内容。
答案 0 :(得分:2)
您可能在表单中遗漏了name
,并将</div>
替换为</form>
。并形成ID!
<div>
<select class="form-control input-small" id="formSel" name="formSel">
<option value="-">Choose category</option>
<option value="One">One</option>
<option value="Two">Two</option>
</select>
</div>
<!-- Form One -->
<form id="formOne" style="display:none" method="POST" action="insert.php">
^
<input type="text" id="aaa" name="aaa" placeholder="aaa">
^
<input type="text" id="bbb" name="bbb" placeholder="bbb">
^
<button type="submit" value="btn-One" name="btn-One">Submit</button>
^
</form>
^^^^^^^
<!-- Form Two -->
<form id="formTwo" style="display:none" method="POST" action="insert.php">
^
<input type="text" id="ccc" name="ccc" placeholder="ccc">
^
<input type="text" id="ddd" name="ddd" placeholder="ddd">
^
<button type="submit" value="btn-Two" name="btn-Two">Submit</button>
^
</form>
^^^^^^^
答案 1 :(得分:0)
如果您关闭表单以及向元素添加name属性,这可能会有所帮助。
<!-- Form One -->
<form id="One" style="display:none" method="POST" action="insert.php">
<input type="text" id="aaa" placeholder="aaa">
<input type="text" id="bbb" placeholder="bbb">
<button type="submit" value="btn-One">Submit</button>
</div>
变为:
<!-- Form One -->
<form id="One" style="display:none" method="POST" action="insert.php">
<input type="text" id="aaa" placeholder="aaa" name="aaa">
<input type="text" id="bbb" placeholder="bob" name="bbb">
<button type="submit" value="btn-One" name="btn-One">Submit</button>
</form>
和
<!-- Form Two -->
<form id="One" style="display:none" method="POST" action="insert.php">
<input type="text" id="ccc" placeholder="ccc">
<input type="text" id="ddd" placeholder="ddd">
<button type="submit" value="btn-Two">Submit</button>
</div>
变为:
<!-- Form Two -->
<form id="One" style="display:none" method="POST" action="insert.php">
<input type="text" id="ccc" placeholder="ccc" name="ccc">
<input type="text" id="ddd" placeholder="ddd" name="ddd">
<button type="submit" value="btn-Two" name="btn-Two">Submit</button>
</form>
答案 2 :(得分:0)
首先,您错过了输入元素的名称属性。
此外,您还错过了结束表单标记。
在这里,我添加了两个隐藏元素(selectedForm1&amp; selectedForm2)来识别提交的表单。
在脚本中,将$('#form'+id).show();
替换为$('#'+id).show();
<强> SCRIPT 强>
<script type="text/javascript">
$(document).ready(function() {
$('form').hide();
$('#formSel').change( function() {
var id = $(this).val();
if( id != '-' )
{
$('form').hide();
$('#'+id).show();
//$('#myModal').modal('show');
}
});
});
</script>
<强> HTML 强>
<div>
<select class="form-control input-small" id="formSel">
<option value="-">Choose category</option>
<option value="One">One</option>
<option value="Two">Two</option>
</select>
</div>
<div>
<!-- Form One -->
<form id="One" method="POST" action="insert.php">
<input type="hidden" id="selectedForm1" name="selectedForm1">
<input type="text" id="aaa" name="aaa" placeholder="aaa">
<input type="text" id="bbb" name="bbb" placeholder="bbb">
<button type="submit" value="btn-One" >Submit</button>
</form>
</div>
<div>
<!-- Form Two -->
<form id="Two" method="POST" action="insert.php">
<input type="hidden" id="selectedForm2" name="selectedForm2">
<input type="text" id="ccc" name="ccc" placeholder="ccc">
<input type="text" id="ddd" name="ddd" placeholder="ddd">
<button type="submit" value="btn-Two">Submit</button>
</form>
</div>
<强> PHP 强>
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST)) {
if ((isset($_POST['selectedForm1']))) {
//execute code
} else {
echo 'Error submit One';
}
if ((isset($_POST['selectedForm2']))) {
//execute code
} else {
echo 'Error submit Two';
}
}