2017-05-02 85 views
0

我的網頁有兩種形式,一種是字段和按鈕,另一種是兩個字段和一個按鈕。檢查用兩種形式按下哪個按鈕

我很難檢查按下了哪個窗體的按鈕,歡迎任何關於如何處理這個窗體的建議。

<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 
} 

回答

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

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. 
}