ajax / php表单和变量赋值

时间:2014-07-29 15:58:23

标签: php ajax

我的目标是在php中使用while循环向我的AJAX函数提交一组变量。这是我第一次使用AJAX,所以请原谅它是否凌乱,并且接近正确。我感谢任何协助。

PHP FORM:

                x=0;
    while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
        {
            $id = $row['col1'];
            $ad = $row['col2'];
            cho '<form id="msu_form">';
            echo "<tr><td>{$ad}</td>";
            echo "<td>";

            $query2 = "SELECT col1,col2 FROM table WHERE notes = 'x'";
            $result2 = mysql_query($query2);
            $count2 = mysql_num_rows($result2);
             if($count2 > 0)
            {
                echo '<select class="Primary" name="primary" onchange=doAjaxPost()>';
                while($row2 = mysql_fetch_array($result2))
                {
                    echo "<option value=".$row2['col1'].">".$row2['col2']."</option>";
                }
                echo "</select>";
            }else
            {
                echo "Blah";
            }
            echo "</td>";
            echo "<td>";                    
            $query3 = "SELECT col1,col2 FROM table2 WHERE notes = 'y'";
            $result3 = mysql_query($query3);
            $count3 = mysql_num_rows($result3);
            if($count3 > 0)
            {
                 echo '<select class="Secondary" name="secondary">';
                 while($row3 = mysql_fetch_array($result3))
                {
                    echo "<option value=".$row3['col1'].">".$row3['col2']."</option>";
                }
                echo "</select>";
            }else
            {
                echo "Bah";
            }
            echo "</td>";
            echo '<input type="hidden" class="ID" name="ID" value="'.$id.'"/>';
            echo '<input type="hidden" class="desc" name="desc" value="'.$ad.'"/>';
            //echo '<td>'."<input type='submit' name='btnupdate' value='UPDATE' /></td>";
            echo '</form>';
            $x = $x+1;
         }

所以每当我改变任何&#34;主要&#34;选择屏幕上的框,我只在第一行获得变量的值。我想从选择框中接收表单的值。我通过一个按钮测试了它,它提交了注释掉的表单,但是该按钮向页面提交了所有正确的信息,但我不想每次都提交数据。有没有办法实现我的目标?

谢谢 - 如果它有助于答案,请在ajax下面。

                    <script>  
                function doAjaxPost() {  
                 // get the form values  
                 var primary = $(this).val();
                 var secondary = $(this).parent().next().child('.Secondary').val();
                 var hidden = $(this).parent().nextAll('.ID').val();
                 //var desc = $(this).parent().nextAll('#desc').val();
                 $.ajax({  
                   type: "POST",  
                   url: "functions/database_write.php",
                   data: $('#msu_form').serialize(),
                   //data: "Primary="+primary+"&Hidden="+hidden+"&Secondary="+secondary,
                   success: function(resp){  
                      //we have the response  
                    alert("'" + resp + "'");  
                   },  
                   error: function(e){  
                     alert('Error: ' + e);  
                   }  
                 });  
                }  
                </script>

2 个答案:

答案 0 :(得分:1)

首先,在您的PHP代码中更改以下4行(使用id

echo "<select id=Primary name=primary onchange=doAjaxPost()>"; 
echo "<select id=Secondary name=secondary>";
echo '<input type="hidden" id="ID" name="ID" value="'.$id.'"/>';
echo '<input type="hidden" id="desc" name="desc" value="'.$ad.'"/>';

到(使用class已修改

echo '<select class="Primary" name="primary" onchange="doAjaxPost(this)">'; //added (this)
echo '<select class="Secondary" name="secondary">';
echo '<input type="hidden" class="ID" name="ID" value="'.$id.'"/>';
echo '<input type="hidden" class="desc" name="desc" value="'.$ad.'"/>';

然后,在您的javascript代码中,更改

function doAjaxPost() {
 var primary = $('#Primary').val();  
 var secondary = $('#Secondary').val();
 var hidden = $('#ID').val();
 var desc = $('#desc').val();

已修改

function doAjaxPost(sel) { // added (sel)
 var primary = $(sel).val();  //changed to $(sel)
 var secondary = $(sel).parent().next().children('.Secondary').val(); //changed to $(sel) and changed to children()
 var hidden = $(sel).parent().nextAll('.ID').val(); //changed to $(sel) and changed to nextAll()
 var desc = $(sel).parent().nextAll('#desc').val(); //changed to $(sel) and changed to nextAll()

答案 1 :(得分:0)

如果在正常提交时一切正常,那么可能您生成的ajax数据错误,而是给您的表单一个id:

echo '<form id="myform">';

然后序列化表单以获取正确的数据:

function doAjaxPost() {  

 $.ajax({  
   type: "POST",  
   url: "functions/data.php",  
   data: $('#myform').serialize(),
   success: function(resp){  
      //we have the response  
    alert("'" + resp + "'");  
   },  
   error: function(e){  
     alert('Error: ' + e);  
   }  
 });  
}  

编辑好了你编辑已经清理了一些东西,我没有注意到你有多种形式。

使用与上面相同的doAjaxPost函数,将data属性更改为:

  data: $(this).parents('form').serialize();