我正在尝试提交一个HTML表单,其中包含3个不同名称的提交按钮,但我无法识别哪个按钮提交了表单。
HTML:
<div class="col-md-12">
<form id="validation-form" class="form-horizontal form-label-left"
data-parsley-priority-enabled="false"
novalidate="novalidate"
action="./controller/productlineNewController.php" method="post">
<div class="col-md-6">
<section class="widget">
<div class="body">
<fieldset>
<div class="form-group">
<label class="control-label col-md-4" for="pl_name">Product Line Name *</label>
<div class="col-md-6">
<input type="text" id="pl_name" name="pl_name" onBlur="checkAvailability()"
class="form-control input-transparent"
placeholder="Enter Product Line Name" required="required">
<span class="help-block" id="availability-status"></span>
</div>
<p><img src="img/small_loading.gif" id="loaderIcon" style="display:none" /></p>
</div>
</fieldset>
</div>
</section>
</div>
<div class="col-md-12">
<section class="widget">
<div class="form-actions">
<div class="row">
<div class="col-md-10 col-md-offset-2">
<button type="submit" name="save" class="btn btn-lg btn-success"
onclick="needToConfirm = false;">Save</button>
<button type="submit" name="save_new" class="btn btn-lg btn-success"
onclick="needToConfirm = false;">Save & New Product Line</button>
<button type="submit" name="save_project" class="btn btn-lg btn-primary"
onclick="needToConfirm = false;">Save & Add Project</button>
<button type="button" onclick="resetForm()"
class="btn btn-lg btn-danger">Reset Form</button>
</div>
</div>
</div>
</section>
</div>
</form>
</div>
PHP - productlineNewController.php
print_r($_POST);
if (array_key_exists("save", $_POST)) {
$action = TRUE;
$new = FALSE;
$task = FALSE;
echo 'save';
} elseif (array_key_exists('save_new', $_POST)) {
$action = TRUE;
$new = TRUE;
$task = FALSE;
echo 'save_new';
} elseif (array_key_exists('save_project', $_POST)) {
$action = TRUE;
$new = FALSE;
$task = TRUE;
echo 'save_project';
}
结果:
Array ( [pl_name] => aaa )
我已经尝试isset
函数来检查是否点击了任何提交按钮,但我什么都没有。
我还尝试使用input
buttons
内容
<input type="submit" name="save2" id="save" class="btn btn-lg btn-success" onclick="needToConfirm = false;" value="Save">
但同样的问题。
请帮忙。
edit
当我从JS
页面中删除此HTML
代码时,它工作正常但我需要此JS
代码来验证表单。任何人都可以解决这个问题。
<script src="lib/parsleyjs/dist/parsley.min.js"></script>
答案 0 :(得分:0)
Core PHP中使用的此方法以您的方式尝试
<?php
if (isset($_POST['save'])) {
# save was clicked
}
else if (isset($_POST['save_new'])) {
# save_new was clicked
}
else if (isset($_POST['save_project'])) {
# save_project was clicked
}
?>