GradDev
GradDev

Reputation: 411

<form> and </form> tags printed in php do not align properly

I'm creating a list of clients and their details, which are imported from the database. At the end of every row, there's an 'edit' button which will display all the information about that client in a form, which is to be edited.

As the client list is imported from database, I've written the form in PHP as following-

while($row = mysqli_fetch_array($result))
        {   
            echo"   <tr id=".$count.">";
            echo"       <form method='post' id='edit_form' action='search_client_details.php?edit=true'>";
            echo"       <td><input type='checkbox' id='chkStatus' name='customer' class='chkbox'  value='".$row['client_id']."'></td>";
            echo"       <td>".$row['client_name']."</td>";
            echo"       <td>".$row['client_address']."</td>";
            echo"       <td>".$row['client_type']."</td>";
            echo"       <td><input type='submit' name='submit' id='edit_form_submit' value='Edit'  class='btn btn-success' style='background-color:#27ae60; color:#fff'></td>";
            echo"       </form>";
            echo"   </tr>";

            $count++;
        }

It prints the list properly. But when the edit button is pressed, it does not work. Inspect element on a row shows this-

<tr id="1" style="display: table-row;">     
<form method="post" id="edit_form" action="search_client_details.php?edit=true"></form>     
<td><input type="checkbox" id="chkStatus" name="customer" class="chkbox" value="1"></td>        
<td>Name</td>       
<td>address</td>        
<td>type</td>       
<td><input type="submit" name="submit" id="edit_form_submit" value="Edit" class="btn btn-success" style="background-color:#27ae60; color:#fff"></td>

As you can see, the form tags are printed first and then the rest. Why is this happening? I'm a beginner at this, all help is appreciated.

Upvotes: 0

Views: 132

Answers (2)

B. Desai
B. Desai

Reputation: 16446

You cant add form in tr attribute. Instead of it you can carete new table within existing and assign it to form

try this:

while($row = mysqli_fetch_array($result))
        {   
            echo "   <tr id=".$count.">";
            echo "       <td><form method='post' id='edit_form' action='search_client_details.php?edit=true'><table><tr>";
            echo "       <td><input type='checkbox' id='chkStatus' name='customer' class='chkbox'  value='".$row['client_id']."'></td>";
            echo "       <td>".$row['client_name']."</td>";
            echo "       <td>".$row['client_address']."</td>";
            echo "       <td>".$row['client_type']."</td>";
            echo "       <td><input type='submit' name='submit' id='edit_form_submit' value='Edit'  class='btn btn-success' style='background-color:#27ae60; color:#fff'></td>";
            echo "</tr></table></form></td>";
            echo "</tr>";

            $count++;
        }

Upvotes: 1

Norlihazmey Ghazali
Norlihazmey Ghazali

Reputation: 9060

It happened because you create the <td> element immediately after the opening of form's tag. Your html markup is invalid. Consider this two lines :

echo "<form method='post' id='edit_form' action='search_client_details.php?edit=true'>";
echo"<td><input type='checkbox' id='chkStatus' name='customer' class='chkbox'  value='".$row['client_id']."'></td>";

You see the second line having another opening td element which is invalid in this case

Added

If you want the whole submitted, you must use array for element's name. Put the form's tag opening outside of table, and closing form's tag after table's closing tag.

<form method='post' id='edit_form' action='search_client_details.php?edit=true'>
<table>
 .... any markup
<?php
while($row = mysqli_fetch_array($result))
    {   
        echo"   <tr id=".$count.">";
        echo"       <td><input type='checkbox' id='chkStatus' name='customer[]' class='chkbox'  value='".$row['client_id']."'></td>";
        echo"       <td>".$row['client_name']."</td>";
        echo"       <td>".$row['client_address']."</td>";
        echo"       <td>".$row['client_type']."</td>";
        echo"   </tr>";

        $count++;
    }?>
</table>
    <input type='submit' name='submit' id='edit_form_submit' value='Edit'  class='btn btn-success' style='background-color:#27ae60; color:#fff'>
</form>

Then later in server side search_client_details.php, you can use for loop to iterate all the code :

for( $i = 0,$length = count($_POST['customer']); $i < $length; $i++ ) {
   echo $_POST['customer'][$i]; // this will show all the data sending from form
   // do the operation here
}

Upvotes: 2

Related Questions