在单页中使用ajax和php上传多个表单数据

时间:2012-09-25 10:45:33

标签: php javascript ajax jquery

我的疑问是,如果我有

使用jquery使用过渡效果隐藏这些div标签我想在结果中得到div 1 2 3的结果

1 div
  <div id="1">    
   </div>      
2 div
   <div id="2">
    </div>       
3 div
   <div id="3">
   </div>
   <input type="submit" name="first-sudmit" value="NEXT" onclick="result(this.value)" />
    </div>
    <div id="result">   
        result    
    </div>

在js:

<script type="text/javascript">
function showResult()    
{
    alert("dfadsfdasf");
    xmlhttp.open("GET","result.php?q="+str,true);
    xmlhttp.send();
}
</script>

我甚至尝试过序列化太......不运行php脚本

JS:

<script type="text/javascript">     
function showFormData(oForm) {
   var msg = "The data that you entered for the form with 'name' attribute='" + oForm.name + "': \n";

  submitHandler: function(form) {
                // do other stuff for a valid form
                $.post('result.php', $("#submit_drop,#submit_radio,#submit_check,#submit_text,#submit_drag_text,#submit_drag_img").serialize(), function(data) {
                 $('#results').html(data);
            });           
   alert(msg);
}  
</script>

PHP:

   //page1 process
    if(!ekpty($_POST['first-submit']))
    {
        $first1=$_POST['drop1'];
        $first2=$_POST['drop2'];
        $first3=$_POST['drop3'];
        $first4=$_POST['drop4'];
        $first5=$_POST['drop5'];
      for(i=0;i<=4;i++)
      {         
          $sql="INSERT INTO $score1(correct)VALUES('$first[i]')";        
      }                
    }
    else
    {
          print '<script type="text/javascript">'; 
          print 'alert("page 1 no answer is attempted")'; 
          print '</script>'; 
    }
//output resu;ts
$sql="SELECT count(chkpt) from score1 where chkpt='1'";    
echo $sql;

所以现在如何在最后的结果div标签中获得这些表单的结果.......

请回答一下.......

1 个答案:

答案 0 :(得分:0)

您的问题有点难以阅读,但如果我理解您的要求,我相信您的序列化代码会起作用,但我相信您需要使用包含您希望序列化的元素的表单的ID。所以:

1 div
  <div id="1">
     <form id="firstFormToSubmit">......</form>
   </div>      
2 div
   <div id="2">
     <form id="secondFormToSubmit">......</form>
    </div>       
3 div
   <div id="3">
    <form id="thirdFormToSubmit">......</form>
   </div>
   <input type="submit" name="first-sudmit" value="NEXT" onclick="result(this.value)" />
    </div>
    <div id="result">   
        result    
    </div>

然后处理这些形式:

submitHandler: function(form) {
            // do other stuff for a valid form
            $.post('result.php', $("#firstFormToSubmit").serialize(), function(data) {
             $('#results').html(data);
        });