Javascript 处理同一表单上的两个提交按钮
我在一个表单上有两个提交按钮。我正在使用serialize方法(所以它不会刷新页面)将表单数据发送到php页面。如果这两个按钮将数据发送到一个php页面,如何根据按下的按钮来判断要处理的php脚本的哪一部分?我在这个网站上看到的类似问题对我没有帮助。我的代码如下所示 HTMLJavascript 处理同一表单上的两个提交按钮,javascript,php,jquery,forms,Javascript,Php,Jquery,Forms,我在一个表单上有两个提交按钮。我正在使用serialize方法(所以它不会刷新页面)将表单数据发送到php页面。如果这两个按钮将数据发送到一个php页面,如何根据按下的按钮来判断要处理的php脚本的哪一部分?我在这个网站上看到的类似问题对我没有帮助。我的代码如下所示 HTML <form id='form_id' action='my_page.php' method='post'> <input type='text' name='quest1' value='$qu
<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>
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
}
?>
您可以添加
然后在按钮单击功能中,在序列化表单值之前设置此隐藏输入的值。根据按下的提交按钮设置隐藏输入的值,然后根据该输入处理表单 HTML PHP
您可以为每个输入指定不同的值:
<input type="submit" name="action" value="Update" />
<input type="submit" name="action" value="Delete" />
你可以在这个问题中找到完整的答案:
你可以用不同的方法来做,一种是制作两个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
}
?>
<?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;
}
?>
“如何根据按下的按钮来判断要处理的php脚本的哪一部分?”-您可以使用带有条件语句的isset()
。谢谢@Fred,但这将刷新我不想要的页面。@Fred ii-LUL这就是您作为新程序员的smarta**所得到的。恕我直言,我觉得这很有趣。@bassxzero对不起,我不太懂“smarta**”这个词。我必须注意表单只有一个action属性,它告诉表单处理将发生在哪个页面。那么,当单击按钮时,我将如何告诉哪个php页面去?$.post('response.php',$(“#form_id”).serialize(),function(info){$(“#msg”).html(info)};此函数的第一个参数表示url操作只是将操作放在那里,如我的示例中所示非常感谢@Fermin。你的解决方案奏效了。我不得不从表单标签中删除action属性,它成功了。谢谢@Mohamed,但我不想刷新页面。这就是我使用jQuery和php提交表单的原因。请告诉我如何将您的javascript与我的javascript合并,因为我将您的javascript放在我的javascript之前,但它不起作用。
<?php
include("includes/db.php");
if($_POST['action'] == 'response' ){
// do something
}
else if($_POST['action'] == 'subm' ){
// do something else
}
<input type="submit" name="action" value="Update" />
<input type="submit" name="action" value="Delete" />
<?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
}
?>
<?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;
}
?>
//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(){
})
});