Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass a value from a form control to a variable in php html when submit is pressed

Tags:

html

php

what I am trying to do is pass a value to a variable depending on which button the user presses. I created a table and delete button with a while loop, however, because of this, all the buttons have the same name and id. how can i pass the specific UserID to a variable when the appropriate button is pressed??

this is my PHP with embedded HTML

<?php
    $servername = "localhost";
    $username = "root";
    $password = "A1128857795!";

    // Create connection
    $conn = new mysqli($servername, $username, $password);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } else{
        echo "Connected successfully";}


    mysqli_select_db($conn,'test_schema');

    $sql = "SELECT * FROM userinfo;";

    $retvalues = mysqli_query($conn, $sql);

    if(!$retvalues)
        {
            die("could not get data: " . mysqli_error());
        }


    print("<br>");
    echo "<form action='delete.php' method='POST'>";
    echo"<table border='1'>";
    echo"<tr align='center'>";
    echo "<th>ID</th>";
    echo "<th>NAME</th>";
    echo "<th>GENDER</th>";
    echo "</tr>";
    while($row = mysqli_fetch_array($retvalues, MYSQL_ASSOC))
    {   
            print("<tr>");
            echo "<td><input type='text' name='id' value='{$row['UserID']}'/></td>". " " . "<td>{$row['FirstName']}" . " " . "{$row['LastName']}</td>" .
                " " . "<td align='center'>{$row['gender']}</td>"; 
            echo "<td><input  name='Delete' value='Delete' type='submit'></td>";
            print("</tr>");
    }

    echo "</table>";
    echo "</form>";

When I press any of the buttons, it returns the ID for the last row of the table every time, causing the DELETE statement to always delete the last record from my database.

if(isset($_POST['id']))
    {   

        $delUID = $_POST['id'];
        $sql="DELETE FROM userinfo WHERE UserID = $delUID";
        $query = mysqli_query($conn, $sql);
        echo "DELETE FROM userinfo WHERE UserID = $delUID;";
    }
    $conn->close();
?>

Any advice will be welcomed. thank you.

like image 865
vhdz04 Avatar asked Jan 18 '26 13:01

vhdz04


1 Answers

You can either use an array in the name of the old school input-button or just use the <button> tag to have different value and caption.

<form method="post">
  <?php for($id = 1; $id <= 3 ; $id++): ?>
    <input type="submit" name="delete_a[<?php echo $id; ?>]" value="Caption of delete button a">
    <button type="submit" name="delete_b" value="<?php echo $id; ?>">Caption of delete button b</button>
    <br>
  <?php endfor; ?>
</form>
<?php
var_dump($_POST);
?>

With the array version you could even use checkboxes and any submit button to delete multiple rows at once. You are then interested in the keys of the array $_POST['delete_a'].

like image 109
Quasimodo's clone Avatar answered Jan 20 '26 01:01

Quasimodo's clone