Home > OS >  Adding an input button in table cell with php/mysql
Adding an input button in table cell with php/mysql

Time:11-05

I have a table with some cells populating data from mysql, then I want to add a push button to change stuff for a particular row (providing parameter as hidden input for change.html so they get passed in and retrieve them in change.html. I added an input form action type submit for the last column but no matter what I do, its not working. In one revision I have the entire line input type=submit name=id_html id=id_html value=0

showed up for the cell content. I am puzzled cause this worked an another platform. And I dont want to do it with JAVAScript. Note that I am using single quote ' for the form input and double quotes within it for it to construct html code without interacting with variables/double quotes for values etc.

$conn = new mysqli($server, $user, $password, $database); echo "<h2>Avaliable pets</h2><ol>";
if ($conn->connect_error)    
{ 
   die("Connection failed: " . $conn->connect_error);    
} 
echo "conneced successfully";
    
$sql = "SELECT * from cats";    $result=$conn->query($sql);
if ($result->num_rows > 0)    
{ 
   echo "<style> table,th, td {border: 1px solid black;}</style>"; echo "<table><tr>
         <th>ID</th><th>Name</th><th>Owner</th><th>Birth</th><th>Change</th></tr>"; 
         while ($row = $result->fetch_assoc())
         { 
            echo "<style> td [text-align:center;]</style>";
            $id=$row["id"];
            $name=$row["name"];
            $owner=$row["owner"];
            $birth=$row["birth"];

          //echo "<tr><td>".$id."</td><td>".$name."</td><td>".$owner."</td><td>".$birth."</td><td></td></tr";
          echo "<tr><td>".$id."</td><td>".$name."</td><td>".$owner."</td><td>".$birth."</td><td>";
echo '<form action="change.html" method="get"> input type="submit" name="id_html" id="id_html" value="'.$id.'"/></form>'
echo "</td></tr>";

         }    
    }

CodePudding user response:

Change input type="submit" to <input type="submit">

Correct the way you define the form, input, and button within the table cell.

Try this updated code:

$conn = new mysqli($server, $user, $password, $database);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "<h2>Available pets</h2><ol>";

$sql = "SELECT * from cats";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<style> table, th, td {border: 1px solid black;}</style>";
    echo "<table><tr><th>ID</th><th>Name</th><th>Owner</th><th>Birth</th><th>Change</th></tr>";
    
    while ($row = $result->fetch_assoc()) {
        $id = $row["id"];
        $name = $row["name"];
        $owner = $row["owner"];
        $birth = $row["birth"];

        echo "<tr><td>" . $id . "</td><td>" . $name . "</td><td>" . $owner . "</td><td>" . $birth . "</td><td>";
        echo '<form action="change.html" method="get">';
        echo '<input type="hidden" name="id_html" value="' . $id . '">';
        echo '<input type="submit" value="Change">';
        echo '</form>';
        echo "</td></tr>";
    }
}

Changes

  • Fixed the HTML markup for the form, input, and button.

    Added a hidden input field to pass the 'id' to the 'change.html' page.

    Removed unnecessary style tags and alignment properties.

  • Related