一个ajax php页面中的多个不同形式

时间:2015-08-12 22:31:26

标签: php jquery ajax forms

由于语言限制,我可能不了解如何向Google询问我想要完成的工作,但我希望您能理解。

我有三种形式,我想在同一页面上显示而不刷新。第一种形式向php提交动作,确定要显示哪个功能(还有另一种形式)。但是按钮干扰了,我无法接近我想做的事情。

Index.php应该将用户输入发送到calculator.php,然后根据值打开下一个表单:

<form id="form1" method="post" formaction="calculator.php">
<input id="form1" type="submit" value="Submit" />
<div id="parseSecondForm"></div>

<script type="text/javascript">
$(function(){
$('#form1').on('submit', function(e){
e.preventDefault();
$.ajax({
  type: $(this).attr('method'),
  url: $(this).attr('formaction'),
  data: $(this).serialize(),
  beforeSend: function(){
    $('#parseSecondForm').html('<img src="loading.gif" />');
  },
  success: function(data){
    $('#parseSecondForm').html(data);
  }
});
});
});
</script>

calculator.php接收用户输入并在index.php中的#parseSecondForm div中回显下一个表单:

if (form1 === '1') {
 echo '
   <form id="form2" method="post" formaction="form2.php">
   <input id="form2" type="submit" value="Submit" />
   <div id="thankyou"></div>
   <!--submit saves php result in MySQL and thanks the user in next div-->
 ';
}
 else {
  echo '
   <form id="form3" method="post" formaction="form3.php">
   <input id="form3" type="submit" value="Submit" />
   <div id="thankyou"></div>
   <!--submit saves php result in MySQL and thanks the user in next div-->
 ';
}

我试图从index.php回显修改后的javascript,但它只是重置了所有表单。

也许有一些表格脚本被指定用于原因或任何其他想法如何解决问题?

1 个答案:

答案 0 :(得分:0)

在Javascript / HTML中,您不能拥有两个具有相同ID的项目:

<form id="form1" method="post" formaction="calculator.php">
<input id="form1" type="submit" value="Submit" />

form1只能使用一次。

php动态创建的表单相同。

相关问题