[英]Submit a form with multi submit buttons - PHP
I'm trying to submit an HTML form that has 3 submit buttons with different names but I can't identify which one was pressed to submit the form. 我正在尝试提交一个HTML表单,该表单包含3个具有不同名称的提交按钮,但是我无法确定按下哪个按钮提交了表单。
HTML: 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 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';
}
Result: 结果:
Array ( [pl_name] => aaa )
I have tried isset
function to check if any submit button was clicked but I got nothing. 我尝试过
isset
函数来检查是否单击了任何提交按钮,但是我什么也没得到。
I also tried to use input
insted of buttons
我也尝试使用
buttons
input
<input type="submit" name="save2" id="save" class="btn btn-lg btn-success" onclick="needToConfirm = false;" value="Save">
but same problem. 但同样的问题。
Please help. 请帮忙。
edit
when I dropped this JS
code from the HTML
page it worked fine but I need this JS
code to validate the form. 当我从
HTML
页面上删除此JS
代码时,它工作正常,但我需要此JS
代码来验证表单。 can anyone fix this please. 谁能解决这个问题。
<script src="lib/parsleyjs/dist/parsley.min.js"></script>
This method used in Core PHP Try this in your way
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
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.