如何将id从select传递给PHP?

时间:2014-12-26 19:05:40

标签: javascript php select

    <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事件上的select的id,但我不知道如何将更改后的id传递给PHP;我该怎么做(我知道还有另一个类似的问题,但我不知道那个功能)?或者,当选择了一个选项而没有表单时,是否有另一种方法可以用PHP创建一些东西(因为我已经有了一个表单)?

1 个答案:

答案 0 :(得分:0)

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

  <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+'"> 

这是你的' - &gt; id &lt; - ' - 在 PHP $ _ POST ['id']

中访问它
     ');
  });
});

        </script>