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

时间:2022-09-25 15:31:05

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. For this i used the following code

我想要做的是将title,tag1和details的值保存在数据库中,而不保存save as draft按钮下的值。为此我使用了以下代码

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

现在的问题是,如果我点击另存为草稿,它仍会检查该按钮下面的编码(例如我已经确定了所需的位置),而要求是如果用户点击“另存为草稿”那么值应该保存,不需要填充它下面的其他值,但如果单击提交按钮,则必须填写所有值。任何人都可以告诉如何这样做

4 个解决方案

#1


0  

You can only do this using 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. Alternatively, you could remove the required attribute from all fields, and handle form validation with custom javascript.

你只能使用javascript来做到这一点。您需要在“保存草稿”按钮上放置一个onclick处理程序,该按钮将从该提交不需要的任何字段中删除所需的属性。或者,您可以从所有字段中删除所需的属性,并使用自定义javascript处理表单验证。

#2


0  

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

#3


0  

You can do this using javascript.
Please try this code:

你可以用javascript做到这一点。请尝试以下代码:

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

<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"]){

}

#4


0  

For me, echo var_dump($_POST['draft']); produces the output string(0) "", which indicates that $_POST['draft'] is an empty string. In other words, the value is false and the rest of your code doesn't get executed.

对我来说,echo var_dump($ _ POST ['draft']);产生输出字符串(0)“”,表示$ _POST ['draft']是一个空字符串。换句话说,该值为false,其余代码不会被执行。

You need to use isset to check if the key exists. I was able to read which button was pressed using the following simplified code:

您需要使用isset来检查密钥是否存在。我能够使用以下简化代码读取按下了哪个按钮:

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

#1


0  

You can only do this using 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. Alternatively, you could remove the required attribute from all fields, and handle form validation with custom javascript.

你只能使用javascript来做到这一点。您需要在“保存草稿”按钮上放置一个onclick处理程序,该按钮将从该提交不需要的任何字段中删除所需的属性。或者,您可以从所有字段中删除所需的属性,并使用自定义javascript处理表单验证。

#2


0  

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

#3


0  

You can do this using javascript.
Please try this code:

你可以用javascript做到这一点。请尝试以下代码:

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

<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"]){

}

#4


0  

For me, echo var_dump($_POST['draft']); produces the output string(0) "", which indicates that $_POST['draft'] is an empty string. In other words, the value is false and the rest of your code doesn't get executed.

对我来说,echo var_dump($ _ POST ['draft']);产生输出字符串(0)“”,表示$ _POST ['draft']是一个空字符串。换句话说,该值为false,其余代码不会被执行。

You need to use isset to check if the key exists. I was able to read which button was pressed using the following simplified code:

您需要使用isset来检查密钥是否存在。我能够使用以下简化代码读取按下了哪个按钮:

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