2014-09-11 72 views
-1

我都有兩個文字區域用自己的提交按鈕,像這樣:提交的數據只有在特定的按鈕被點擊

<textarea style="width: 200px; height: 22px; resize: none" type="text" name="housetypeData"></textarea> 
<input type="submit" name="newHousetype" value="New house"></input> 

<textarea style="width: 200px; height: 22px; resize: none" type="text" name="architectData"></textarea> 
<input type="submit" name="newArchitect" value="New architect"></input> 

和PHP根據像這樣發佈的數據響應:

if (!empty($_POST['housetypeData'])) { 

    echo 'new housetype'; 
} 
elseif (!empty($_POST['architectData'])) { 

    echo 'new architect'; 
} 

問題是不管在哪個字段中鍵入文本,兩個按鈕都會提交數據。我該如何做到這一點,只有名稱爲「newHousetype」的按鈕提交字段「housetypeData」的數據,而其他兩個字段的數據相同?

+0

只是使用2種不同的形式... – ProGM 2014-09-11 10:08:29

回答

1

你們都在同一個表單中提交按鈕。當然他們會提交相同的內容。嘗試使用兩個單獨的表單。

<form action="test4.php" method="POST"> 
    <textarea style="width: 200px; height: 22px; resize: none" type="text" name="housetypeData"></textarea> 
    <input type="submit" name="newHousetype" value="New house"></input> 
</form> 
<form action="test4.php" method="POST"> 
    <textarea style="width: 200px; height: 22px; resize: none" type="text" name="architectData"></textarea> 
    <input type="submit" name="newArchitect" value="New architect"></input> 
</form> 

試試這個...

1

你需要兩個<form>這一點。

0

如果你不想使用2種形式,你應該使用一些JavaScript。

在兩個提交按鈕上放置一個onclick事件監聽器。

// U'll need to add an id to you're button 
$('#newHousetype').click(function(ev) { 
    ev.preventDefault(); // to stop the form from submitting 
    $("#newArchitect").remove(); // Remove the other textarea 
    $('#myForm').submit(); // Submit the form 
});