Php 无法通过其id获取表单

Php 无法通过其id获取表单,php,jquery,ajax,Php,Jquery,Ajax,我有一个在外部文件中创建的表单,然后我通过php将其包括在内,然后通过其id获取该表单进行序列化。 index.php <div id="addressesList"> <?php include 'addresstable.php' ?> </div> 查看index.php文件的源代码: index.php中的jquery代码: <script> function editAddress(){ $.

我有一个在外部文件中创建的表单,然后我通过php将其包括在内,然后通过其id获取该表单进行序列化。 index.php

<div id="addressesList">        
 <?php include 'addresstable.php' ?>
</div>

查看index.php文件的源代码:

index.php中的jquery代码:

<script>
   function editAddress(){
       $.ajax({
        url: 'insert.php',
        type: 'POST',
        data: $('#addressListForm').serialize() , // An object with the key 'submit' and value 'true; + '&' + 'editAddress' + '=' + id
        success: function (result) {
          //$(thisObj).parents("tr:first").remove();

          $("#addressesList").html(result);
        }
    }); 
   }
</script>

函数editAddress(){
$.ajax({
url:'insert.php',
键入:“POST”,
数据:$('#addressListForm').serialize(),//键为'submit'且值为'true;+'&'+'editAddress'+'='+id'的对象
成功:功能(结果){
//$(thisObj).parents(“tr:first”).remove();
$(“#地址列表”).html(结果);
}
}); 
}
我想通过单击调用EditAddress函数的元素来获取其id为“addressListForm”的表单。 addresstable.php文件

                <?php
                    $i=1;
                    $results=mysqli_query($dbCnn,"select * from addresses");
                    while( $row=mysqli_fetch_array($results,MYSQLI_ASSOC))

                    { 
                        echo '<form action="insert.php" method="post" id="addressListForm">';
                        echo "<tr>
                                <input type='hidden' value='$row[id]' name='id'/> 
                                <td>$i</td>
                                <td><input name='title' type='text' value='$row[title]' style='width:60px'  /></td>
                                <td><input name='address' type='text' value='$row[address]' style='width:100%' /></td>
                                <td><input name='tel' type='text' value='$row[phone]' required='required' style='width:100px' /></td> 
                                <td><input name='fax' type='text' value='$row[fax]' required='required' style='width:100px' /></td>
                                <td><i onclick=\"editAddress($row[id],this)\" class='fa fa-pencil-square-o' title='ثبت تغییرات رایانامه'></i></td> 
                                <td><i onclick=\"deleteAddress($row[id],this)\" class='fa fa-trash-o deletUser' title='حذف کاربر' ></i></td>
                            </tr>";

                    $i++;
                    echo"</form>";
                }
            ?>

</tbody>
    <tbody id="addressTb">

        <?php
                $i=1;
                $results=mysqli_query($dbCnn,"select * from addresses");
                while( $row=mysqli_fetch_array($results,MYSQLI_ASSOC))

                { 
                    echo "
                              <tr>
                                   <td>$i<form action='insert.php' method='post' id='addressListForm$i'><input form='addressListForm$i' type='hidden' value='$row[id]' name='id'/> </td>
                                   <td><input form='addressListForm$i' name='title' type='text' value='$row[title]' style='width:60px'  /></td>
                                   <td><input form='addressListForm$i' name='address' type='text' value='$row[address]' style='width:100%' /></td>
                                   <td><input form='addressListForm$i' name='phone' type='text' value='$row[phone]' required='required' style='width:100px' /></td> 
                                   <td><input form='addressListForm$i' name='fax' type='text' value='$row[fax]' required='required' style='width:100px' /></td>
                                   <td><i onclick=\"editAddress($row[id],this,$i)\" class='fa fa-pencil-square-o' title='ثبت تغییرات رایانامه'></i></td>
                                   <td><i onclick=\"deleteAddress($row[id],this,$i)\" class='fa fa-trash-o deletUser' title='حذف کاربر' ></i></form></td> 
                                 </tr>
                         ";
                $i++;
            }
        ?>       
</tbody>


addresstable.php
文件中,您应该将table标记放在表单中,现在无法访问
addressListForm
id(在源代码的语法突出显示中也很明显)


如果您有多个表单,请将每个表单放入新的

将其更改为:

                <?php
                    $i=1;
                    $results=mysqli_query($dbCnn,"select * from addresses");
                    while( $row=mysqli_fetch_array($results,MYSQLI_ASSOC))

                    { 
                        echo "<tr id="addressListForm">

                                       <td>
                                        <input type='hidden' value='$row[id]' name='id'/> 
                                       $i</td>
                                       <td><input name='title' type='text' value='$row[title]' style='width:60px'  /></td>
                                       <td><input name='address' type='text' value='$row[address]' style='width:100%' /></td>
                                       <td><input name='tel' type='text' value='$row[phone]' required='required' style='width:100px' /></td> 
                                       <td><input name='fax' type='text' value='$row[fax]' required='required' style='width:100px' /></td>
                                       <td><i onclick=\"editAddress($row[id],this)\" class='fa fa-pencil-square-o' title='ثبت تغییرات رایانامه'></i></td> 
                                       <td><i onclick=\"deleteAddress($row[id],this)\" class='fa fa-trash-o deletUser' title='حذف کاربر' ></i></td>

                             </tr>";

                    $i++;
                }
            ?>

</tbody>

我通过更改代码中的某些内容来解决问题:

index.php

<div id="addressesList">        
 <?php include 'addresstable.php' ?>
</div>
    function editAddress(id,thisObj,x){
        var formId="#addressListForm" + x;
       $.ajax({
        url: 'insert.php',
        type: 'POST',
        data: $(formId).serialize()+ '&' + 'editAddress' + '=' + id , 
        success: function (result) {
         $("#addressesList").html(result);

        }
    }); 
   }
和addresstable.php文件

                <?php
                    $i=1;
                    $results=mysqli_query($dbCnn,"select * from addresses");
                    while( $row=mysqli_fetch_array($results,MYSQLI_ASSOC))

                    { 
                        echo '<form action="insert.php" method="post" id="addressListForm">';
                        echo "<tr>
                                <input type='hidden' value='$row[id]' name='id'/> 
                                <td>$i</td>
                                <td><input name='title' type='text' value='$row[title]' style='width:60px'  /></td>
                                <td><input name='address' type='text' value='$row[address]' style='width:100%' /></td>
                                <td><input name='tel' type='text' value='$row[phone]' required='required' style='width:100px' /></td> 
                                <td><input name='fax' type='text' value='$row[fax]' required='required' style='width:100px' /></td>
                                <td><i onclick=\"editAddress($row[id],this)\" class='fa fa-pencil-square-o' title='ثبت تغییرات رایانامه'></i></td> 
                                <td><i onclick=\"deleteAddress($row[id],this)\" class='fa fa-trash-o deletUser' title='حذف کاربر' ></i></td>
                            </tr>";

                    $i++;
                    echo"</form>";
                }
            ?>

</tbody>
    <tbody id="addressTb">

        <?php
                $i=1;
                $results=mysqli_query($dbCnn,"select * from addresses");
                while( $row=mysqli_fetch_array($results,MYSQLI_ASSOC))

                { 
                    echo "
                              <tr>
                                   <td>$i<form action='insert.php' method='post' id='addressListForm$i'><input form='addressListForm$i' type='hidden' value='$row[id]' name='id'/> </td>
                                   <td><input form='addressListForm$i' name='title' type='text' value='$row[title]' style='width:60px'  /></td>
                                   <td><input form='addressListForm$i' name='address' type='text' value='$row[address]' style='width:100%' /></td>
                                   <td><input form='addressListForm$i' name='phone' type='text' value='$row[phone]' required='required' style='width:100px' /></td> 
                                   <td><input form='addressListForm$i' name='fax' type='text' value='$row[fax]' required='required' style='width:100px' /></td>
                                   <td><i onclick=\"editAddress($row[id],this,$i)\" class='fa fa-pencil-square-o' title='ثبت تغییرات رایانامه'></i></td>
                                   <td><i onclick=\"deleteAddress($row[id],this,$i)\" class='fa fa-trash-o deletUser' title='حذف کاربر' ></i></form></td> 
                                 </tr>
                         ";
                $i++;
            }
        ?>       
</tbody>


在当前页面中单击时,是否要从其他位置加载表单?在何处序列化表单数据?不能将表单元素嵌入tbody中。这是无效且不受支持的
id
在哪里定义的?您使用两个参数调用函数,但函数定义未显示任何参数。我已再次编辑该问题。注意addresstable.phpthanks,它可以工作,但我正在使用slimtable对表进行分页和排序,您的代码会给它带来一些问题it@Hamid再次编辑,因为您使用serilize和ajax来获取和发送数据,所以不需要
标记,所以我删除了它,并看到:这是一个无效的标记,标记不应该有冲突
它工作得很好,在源代码视图中没有错误和警告