简体   繁体   English

处理同一个表单上的两个提交按钮

[英]Handling two submit button on the same form

I have two submit buttons on one form.我在一个表单上有两个提交按钮。 I am using serialize method (so it will not refresh the page) to send the form data to php page.我正在使用序列化方法(因此它不会刷新页面)将表单数据发送到 php 页面。 If the two buttons send data to one php page, how can I tell which part of the php script to be processed depending on the button that was pressed?如果两个按钮将数据发送到一个 php 页面,如何根据按下的按钮来判断要处理的 php 脚本部分? Similar questions I saw on this site did not help me.我在这个网站上看到的类似问题对我没有帮助。 My code is shown below.我的代码如下所示。

HTML HTML

<form id='form_id' action='my_page.php' method='post'>

   <input type='text' name='quest1' value='$question'>
   <input type='text' name='test1_id' value='$test_idq'>
   <input type='text' name='test_code1' value='$code'>
   <input type='text' name='class_name1' value='$class_name' >
   <input type='text' name='test_name1' value='$test_nameq'>

   <button  id='response' class='btn btn-danger'>Response</button>
   <button id='subm' class='btn btn-success''><b>Save</b></button>

  </form>

Javascript Javascript

//This script processes the button with the id=subm

$('body').on('click', '#subm', function(){
    $.post($("#form_id").attr("action"), $("#form_id").serialize(), function(info){ $("#msg").html(info); } );
    $("html, body").animate({ scrollTop: 0 }, "slow");
});
$('body').on('submit', '#form_id', function(event){
    return false;
$('#subm').click(function(){
})
});

//This script processes the button with the id=response

$('body').on('click', '#response', function(){
    $.post($("#form_id").attr("action"), $("#form_id").serialize(), function(info){ $("#msg").html(info); } );
    $("html, body").animate({ scrollTop: 0 }, "slow");
});
$('body').on('submit', '#form_id', function(event){
    return false;
$('#response').click(function(){
})
});

PHP PHP

<?php
include("includes/db.php");
$questn1 = $_POST['quest1'];
$class1 = $_POST['class_name1'];
$test_id1 = $_POST['test1_id'];
$t_code1 = $_POST['test_code1'];
$t_name1 = $_POST['test_name1'];

//php code continues
}
?>

You can give each input a different value:您可以为每个输入指定不同的值:

<input type="submit" name="action" value="Update" />
<input type="submit" name="action" value="Delete" />

You can find the whole answer in this question: Two submit buttons in one form你可以在这个问题中找到完整的答案: 两个提交按钮在一个表单中

您可以添加一个<input type='hidden' name='action' value='-'>然后在您的按钮单击函数中设置此隐藏输入的值,然后再序列化表单值。

Set the value of a hidden input based on what submit button is pressed then process the form based on that input.根据按下的提交按钮设置隐藏输入的值,然后根据该输入处理表单。

HTML HTML

<input type='hidden' id="action" name='action' value=''>

   <button type="submit"  id='response' value="response" class='btn btn-danger'>Response</button>
   <button  type="submit" id='subm' value="subm" class='btn btn-success''><b>Save</b></button>

Javascript Javascript

$('body').on('click', '[type="submit"]', function(){
    $('#action').val($(this).val());
});

PHP PHP

<?php
include("includes/db.php");


if($_POST['action'] == 'response' ){
    // do something
}
else if($_POST['action'] == 'subm' ){
    // do something else
}

You can do it in diferents way one is to make two php page scripts.你可以用不同的方式来做,一种是制作两个php页面脚本。
example: save.php示例:save.php

    <?php
include("includes/db.php");
$questn1 = $_POST['quest1'];
$class1 = $_POST['class_name1'];
$test_id1 = $_POST['test1_id'];
$t_code1 = $_POST['test_code1'];
$t_name1 = $_POST['test_name1'];

//php code continues
}
?>

response.php响应.php

       <?php
    include("includes/db.php");
    $questn1 = $_POST['quest1'];
    $class1 = $_POST['class_name1'];
    $test_id1 = $_POST['test1_id'];
    $t_code1 = $_POST['test_code1'];
    $t_name1 = $_POST['test_name1'];

    //php code continues
echo $response;
    }
    ?>

Javascript Javascript

//This script processes the button with the id=subm

$('body').on('click', '#subm', function(){
    $.post('save.php', $("#form_id").serialize(), function(info){ $("#msg").html(info); } );
    $("html, body").animate({ scrollTop: 0 }, "slow");
});
$('body').on('submit', '#form_id', function(event){
    return false;
$('#subm').click(function(){
})
});

//This script processes the button with the id=response

$('body').on('click', '#response', function(){
    $.post('response.php', $("#form_id").serialize(), function(info){ $("#msg").html(info); } );
    $("html, body").animate({ scrollTop: 0 }, "slow");
});
$('body').on('submit', '#form_id', function(event){
    return false;
$('#response').click(function(){
})
});

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

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