如何将 id 从 select 传递到 PHP

How to pass the id from select to PHP?

本文关键字:PHP select id      更新时间:2023-09-26
    <script>

    function firstStep(element) {
var select_name = element.name;
var option_value = element.value;
var option_user_selection = element.options[ element.selectedIndex ].value;
console.log(option_user_selection);
element.id=option_user_selection;

    }</script>
</head>
<body>
    <div class="bck">
        <form name="login" action="" method="post" accept-charset="utf-8">
            <label for="usermail">Username</label>
            <input type="text" name="nume" placeholder="username" required>
            <label for="password">Password</label>
            <input type="password" name="password" placeholder="password" required>
            <input type="submit" value="Login">
        </form>
        <?php
            $l=0;
            $nl=1;
            $connection = mysqli_connect("127.0.0.1", "root", "", "agentie");
            if(isset($_POST['nume']) && isset($_POST['password'])) {
                echo $_POST["nume"];
                echo $_POST["password"];
                $nl=0;
                $query1 = mysqli_query($connection, "SELECT user, password FROM user u WHERE u.user='$_POST[nume]'") or die("Error in the consult.." . mysqli_error($connection));
                while ($row = mysqli_fetch_assoc($query1)) {
                    if (($row["user"] == $_POST["nume"]) && ($row["password"] == $_POST["password"]))
                    {
                    echo "Bine ati venit" . $row["user"];
                    $l=1;
        ?>
        <span>Alegeti actiunea:</span>
        <select class="nume" name="optiune" onchange="firstStep(this)" id="select">
            <option value="select">Afiseaza</option>
            <option value="insert">Introdu</option>
            <option value="delete">Sterge</option>
            <option value="update">Modifica</option>
        </select>
            <script>
            $(document).ready(function() {
              $('.nume').on('change',function(){
                var id = $(this).attr('id');
                alert(id);
              });
            });
            </script>
        <?php
        }

        }
        if($l==0) {
        ?>
        <div style="position:relative; top:300px;" class="col-md-offset-3 col-md-8">
            <span class="titlu">Jermaine Modelling</span>
        </div>
        <?php
        }
        }
        if(($l==0) && ($nl==1)) {
        ?>
        <div style="position:relative; top:300px;" class="col-md-offset-3 col-md-8">
            <span class="titlu">Jermaine Modelling</span>
        </div>
        <?php
        }
        ?>
    </div>
</body>

根据所选的选项,我希望对PHP和HTML做一些事情;所以我用Javascript更改了onChange事件选择的id,但我不知道如何将更改后的id传递给PHP;我该怎么做(我知道还有另一个类似的问题被问到, 但我不知道这与该功能有关)?或者,当选择一个选项时,有没有表单(因为我已经有一个表单)时,有没有另一种方法可以使用 PHP 制作一些东西?

如果要将任何值动态传递给表单,则需要使用脚本中已有的函数。(在函数()中查找注释);

  <script>
        $(document).ready(function() {
          $('.nume').on('change',function(){
            var id = $(this).attr('id');
            alert(id); // You can delete that once you tested and it works.
            //we need send this data, so we will need to 
            //create or update hidden input with value that we got
            $('form[name="login"]').append('
             <input type="hidden" name="id" value="'+id+'"> 

这是您的"-> id <-" - 以 PHP $_POST['id'] 访问它

     ');
  });
});
        </script>