简体   繁体   English

在一个表单中使用2个提交按钮

[英]using 2 submit buttons in a single form

i have a form that allows the user to submit the values, it was working fine, but now i have introduced another button in b/w the form "Save as Draft" . 我有一个允许用户提交值的表格,它工作正常,但是现在我在黑白中引入了另一个按钮“另存为草稿”。

<form class="form-horizontal" role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
    <input name="title">
    <input name="tag1">    
    <textarea name="details"></textarea>

    <button name="draft" type="submit1" class="btn btn-default">Save as Draft</button>
    <input name="location" required>
    /*
    *
    few more i/p values
    *
    */

<button name="save" type="submit" value="submit" >Save</button>
</form>

What i want to do is to save the values of title, tag1 and details in database without saving the values that are under the save as draft button. 我想做的是在数据库中保存title,tag1和details的值,而不保存在另存为草稿按钮下的值。 For this i used the following code 为此,我使用了以下代码

if ($_POST['draft'])
    {
        //exectue the required code
    }
elseif($_POST['save'])
    {
        //exectue the required code
    }

now the issue is that if i click on save as draft it still checks the codition that is below this button (in this eg i have made the location necessary), whereas the requirement is that if the user clicks on "Save as Draft" then the values should get saved and it should not be necessary to fill other values below it, but if submit button is clicked then it is necessary to fill all values. 现在的问题是,如果我单击“另存为草稿”,它仍会检查此按钮下方的编码(在此例中,我已将该位置设为必需的位置),而要求是,如果用户单击“另存为草稿”,则值应该被保存,并且不必填写下面的其他值,但是如果单击了提交按钮,则必须填写所有值。 can anyone tell how to do so 谁能告诉我怎么做

You can only do this using javascript. 您只能使用javascript执行此操作。 You will need to put an onclick handler on the Save Draft button that removes the required attribute from any fields that shouldn't be required for that submission. 您将需要在“保存草稿”按钮上放置一个onclick处理程序,以从该提交不需要的任何字段中删除所需的属性。 Alternatively, you could remove the required attribute from all fields, and handle form validation with custom javascript. 另外,您可以从所有字段中删除必填属性,并使用自定义javascript处理表单验证。

You can do this using javascript. 您可以使用javascript执行此操作。
Please try this code: 请尝试以下代码:

<form class="form-horizontal" role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
    <input name="title" id="title">
    <input name="tag1" id-"tag1">    
    <textarea name="details" id="details"></textarea>

    <button name="draft" id="draft" class="btn btn-default" onclick="update_draft()">Save as Draft</button>
    <input name="location" required>
    /*
    *
    few more i/p values
    *
    */

<button name="save" type="submit" value="submit" >Save</button>
</form>

JS : JS:

<script>
function update_draft(){

            if($("#title").val() === "")
            {
              alert("empty title");
            }
            else if($("#tag1").val()==="")
            {
              alert("empty tag");
            }
            else if($("#details").val()==="")
            {
              alert("empty details");
            }
            else
            {
                   $.post('<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>', {draft:true,title: $("#title").val(), tag1: $("#tag1").val(),details: $("#details").val() }, function(data, status){

                });
            }
}
</script>

SERVER SIDE :: 服务器端 ::

if($_POST["draft"]){

}
elseif($_POST["save"]){

}
<form class="form-horizontal" role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
    <input name="title">
    <input name="tag1">    
    <textarea name="details"></textarea>

    <button name="draft" id="draft" type="submit1" class="btn btn-default">Save as Draft</button>
    <input name="location" required>
    /*
    *
    few more i/p values
    *
    */

<button name="save" id="save" type="submit" value="submit" >Save</button>
</form>
<script>
    $( "#save" ).submit(function( event ) {
      alert( "I'm Save" );

    });

    $( "#draft" ).submit(function( event ) {
      alert( "I'm Draft" );
      event.preventDefault();
    });
</script>

For me, echo var_dump($_POST['draft']); 对我来说, echo var_dump($_POST['draft']); produces the output string(0) "" , which indicates that $_POST['draft'] is an empty string. 产生输出string(0) "" ,表示$_POST['draft']是一个空字符串。 In other words, the value is false and the rest of your code doesn't get executed. 换句话说,该值为false,并且其余代码不会执行。

You need to use isset to check if the key exists. 您需要使用isset来检查密钥是否存在。 I was able to read which button was pressed using the following simplified code: 我能够使用以下简化代码读取按下了哪个按钮:

<html>
<body>
<?php

if (isset($_POST['draft'])) {
    echo 'Draft was saved';
} else if (isset($_POST['submit'])) {
    echo 'Form was submitted';
}

echo '<pre>';
print_r($_POST);
echo '</pre>';

?>
<form method='post'>
<input name='draft' type='submit' value='Save Draft'>
<input name='submit' type='submit' value='Submit'>
</form>
</body>
</html>

The following code, which consists of code that you posted, worked for me as well: 以下代码(包括您发布的代码)也对我有用:

<html>
<body>
<?php
if (isset($_POST['draft'])) {
    echo "Draft was made";
} else if(isset($_POST['save'])) {
    echo "Form was saved";
}
?>
<form class="form-horizontal" role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
    <input name="title">
    <input name="tag1">
    <textarea name="details"></textarea>

    <button name="draft" type="submit1" class="btn btn-default">Save as Draft</button>
    <input name="location" required>
    /*
    *
    few more i/p values
    *
    */

<button name="save" type="submit" value="submit" >Save</button>
</form>
</body>
</html>

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

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