doumeng3345 2016-06-17 02:00
浏览 314

如何在按下提交时将表单控件中的值传递给php html中的变量

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.

  • 写回答

3条回答 默认 最新

  • dongyuan9149 2016-06-17 02:22
    关注

    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'].

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?