简体   繁体   English

仅在单击特定按钮时提交数据

[英]Submit data only when specific button is clicked

I have two textareas each with their own submit button like so: 我有两个文本区域,每个文本区域都有自己的提交按钮,如下所示:

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

and php responds according to posted data like so: 和php根据发布的数据做出响应,如下所示:

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

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

    echo 'new architect';
}

Problem is no matter in which field I type the text, both buttons will submit data. 问题是无论我在哪个字段中键入文本,两个按钮都将提交数据。 How do I make it so that only button with name "newHousetype" submits data of field "housetypeData" and the same for other two? 如何使只有名称为“ newHousetype”的按钮才能提交字段“ housetypeData”的数据,而其他两个按钮都相同?

You have both submit buttons inside the same form. 您在同一表单中都有两个提交按钮。 Of course they will submit the same content. 当然,他们将提交相同的内容。 Try using two separate forms. 尝试使用两种单独的形式。

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

try this... 尝试这个...

为此,您将需要两个<form>

If you don't wanna use 2 forms, you should use some javascript. 如果您不想使用2种形式,则应使用一些javascript。

Put an onclick event listener on the two submit button. 将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
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM