我的网页上有两个表单,一个带有字段和一个按钮,另一个带有两个字段和一个按钮。
我很难检查按下哪个表格的按钮,欢迎任何有关如何处理这个问题的建议。
<form class="form-inline" action="somewhere.php" method="post">
<div class="form-group">
<label for="date">Date:</label>
<input type="text" class="form-control" name="date" id="date" placeholder="abc">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<form class="form-inline" action="somewhere.php" method="post">
<div class="form-group">
<label for="date1">Date 1:</label>
<input type="text" class="form-control" name="date1" id="date1" placeholder="abc>
</div>
<div class="form-group">
<label for="date2">Date 2:</label>
<input type="text" class="form-control" name="date2" id="date2" placeholder="abc">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
应该检查按下什么按钮。
$date = $_POST['date'];
$date1 = $_POST['date1'];
$date2 = $_POST['date2'];
if (!empty(date)) {
//do something
}
if (!empty($date1) && !empty($date2)) {
//do something else
}
答案 0 :(得分:1)
首先在按钮中输入name属性。
<button type="submit" name="button1" class="btn btn-default">Submit</button>
<button type="submit" name="button2" class="btn btn-default">Submit</button>
现在php边检查按下了哪个按钮:
if(isset($_POST['button1']){
//Do something.
}
else if(isset($_POST['button2'])
{
//Do something.
}
答案 1 :(得分:1)
HTML
<button type="submit" name="btn1" class="btn btn-default">Submit</button>
<button type="submit" name="btn2" class="btn btn-default">Submit</button>
PHP
if(isset($_POST['btn1']){
//put code here.
}else{
//put code here.
}