我有一个包含复选框,单选按钮和文本区域的表单。然后我有三个按钮,无论点击哪个按钮,它都应该调用相同的php文件来处理表单数据。我需要跟踪哪个按钮被按下,因为根据按下的按钮,表单数据的处理方式会有所不同。表单没有返回预期数据的ajax请求
我想这样更新页面而不用重新加载或重定向,并且我无法正常使用ajax。这个php文件现在只包含:“<pre><?php print_r($_POST); ?></pre>
”,我将在基本功能工作后实现一个switch-case。
单击按钮时,页面上会生成表单副本,而不是返回POST值。
<form id="form" action="calc.php" method="post">
<input type="radio" name="rb[]" value="one" checked> one<br>
<input type="radio" name="rb[]" value="two">two<br>
Option a<input type="checkbox" name="cb[]" value="a">
Text a<input type="text" name="tb[]">
Option b<input type="checkbox" name="cb[]" value="b">
Text b<input type="text" name="tb[]">
<button id="first" class="button" name="button[]" value="first">first</button>
<button id="second" class="button" name="button[]" value="second">second</button>
<button id="third" class="button" name="button[]" value="third">third</button>
</form>
<script>
$('.button').click(function(e) {
var f = $('form').serialize();
var b = this.id;
console.log(b);
console.log(f);
$.ajax({
data: {'button':b, 'formval': f},
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(resp){
$('#result').html(resp);
}
});
return false;
});
</script>
我怎么能发送表单值作为数组与被点击到PHP文件中的按钮一起,然后使其返回页面上的PHP文件的结果呢?
谢谢。
谢谢,这工作很好,并从PHP返回的数据以预期的格式发布到页面! –