【问题标题】:How to update the database, when changing a select option - PHP, AJAX更改选择选项时如何更新数据库-PHP,AJAX
【发布时间】:2016-09-19 02:06:31
【问题描述】:

我有一个用 php 制作的表格,现在我想让用户使用下拉菜单(选择/选项)更改每个用户的访问级别。例如:

一旦用户按下常规,它就会将请求发送到 ajax,立即更改/更新数据库。

admin.php

    <?php
//database connection
include("db_conn.php");
include("session.php");

session_start();

$username = $_SESSION['logged_in']; //set the current user
$query = "SELECT `ID`, `Username`,`Name`, `DOB`, `Email`, `Access` FROM `users`";

$result =   $mysqli->query($query);
$row=$result->fetch_array(MYSQLI_ASSOC);


    function show_table($myData)
    {
        echo "<table border=1 id='staff_table'>
        <tr>
        <th>ID</th>
        <th>Username</th>
        <th>Name</th>
        <th>DOB</th>
        <th>Email</th>
        <th>Access</th>
        </tr>";
        while ($row = $myData->fetch_array(MYSQLI_ASSOC))
        {   
            $access= $row['Access'];
            $access2;


              $access_data = 1; //admin = level 1
              $access_data2 = 2; //general = 2
            if($access == 1)
            {
                $access = "Admin";
                $access2 = "General";

                $access_data = 1; //change when the page loads (depending what access level they are)
                $access_data2 = 2; //change when the page loads
            }
            else
            {

                $access = "General";
                $access2 = "Admin";

                $access_data = 2; //the reason i have these values is they 
                $access_data2 = 1; //change depending on what the user's access level is once the page loads
            }

            echo "<tr>";
            echo "<td>" . $row['ID'] . "</td>";
            echo "<td>" . $row['Username'] . "</td>";
            echo "<td>" . $row['Name'] . "</td>";
            echo "<td>" . $row['DOB'] . "</td>";
            echo "<td>" . $row['Email'] . "</td>";
            echo "<td id='" . $row['ID'] .  "'>";
            echo "<select name='admin_menu' class='admin_menu'>"; 
            echo '<option class="admin_option" value="' . $access_data2. '">' . $access . '</option>';
            echo '<option class="admin_option" value="' . $access_data. '">' . $access2 . '</option>';
            echo "</select>";
            echo "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }

?>
<html>
    <head>
        <title>Admin Table</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $('.admin_menu').change(function() {
                    id = $(this).parent().attr('id');
                    level = $('.admin_menu  option:selected').val();
                    var request = $.ajax({
                      url: "changeLevel.php",
                      method: "POST",
                      data: { id : id, level: level },
                      dataType: "html"
                      });

                    request.done(function() {
                       //location.reload(); //reload page (if it actually updates)
                    });

                    });
                });
        </script>   
    </head>


    <body>
        <h1>Admin Table</h1>
        <?php show_table($result); ?>
    </body>
</html>

changeLevel.php

<?php
include("db_conn.php");

$id = $_POST['id'];
$level = $_POST['level'];

    $queryUpdate="UPDATE users SET Access='$level' WHERE ID='$id'";
    $update = $mysqli->query($queryUpdate);
?>

用户表没有更新,还有其他解决方案吗?或者任何人都可以看到问题?在此先感谢,我对 AJAX 还很陌生,因此感谢所有帮助:)

【问题讨论】:

  • @daremachine 它没有更新大声笑:(
  • ajax 成功了吗?以及您在服务器端看到的内容,例如 var_dump($_POST);死;
  • 一定要用ajax吗?
  • @IvanBarayev 不,我没有
  • 您需要检查错误。如果你的 ajax 是用状态 200 发送的,那么你需要检查 php 端,比如 $mysqli 是好的,$id,$level 是好的。我建议您包装您的查询以尝试捕获块并处理异常,问题可能出在数据库

标签: php jquery html ajax select


【解决方案1】:

首先改变这一行

echo "<select name='admin_menu' class='admin_menu'>"; 
echo '<option class="admin_option" value="' . $access_data2. '">' . $access . '</option>';
echo '<option class="admin_option" value="' . $access_data. '">' . $access2 . '</option>';
echo "</select>";

echo "<select name='admin_menu' class='admin_menu' id='Selectid' onchange='postlist();'>"; 
echo '<option class="admin_option" value="'.$access_data2.'-'.$access.'">'.$access.'</option>';
echo '<option class="admin_option" value="'.$access_data.'-'.access2 .'">'.$access2.'</option>';
echo "</select>";

在更改的行中,我在更改&lt;option value=""&gt; 后将onchange="" 函数和id="" 添加到"select" 标记; "$access_data""$access" 加上“-”用于爆炸值。

JavaScript

<script>
    function postlist() {
    $.ajax({
        type: 'POST', 
        url: 'changeLevel.php', // here posting value to your php file
        data: $('#Selectid').val(), // here get the option value from select
        success: function (answer) {
            $("#ReportResult").html(answer) // here you can define an alert function for after success or you can use it with an id for showing the response
        }
    })
}
</script>  

在 changeLevel.php 中

<?php
include("db_conn.php");

$id = $_POST['id']; //here we get value like (13-Admin);
$ids = explode("-", $id); //and here we explode the $id value (explode mean split the value with "-" or you can define "space",",","." or another sign)

    $queryUpdate="UPDATE users SET Access='$ids[1]' WHERE ID='$ids[0]'";
    $update = $mysqli->query($queryUpdate);
?>

PS = $ids[0] eq 到 id 和 $ids[1] eq 到 Level

您可以轻松更改此顺序

【讨论】:

  • 为什么这种方法比原始方法更好,如果使用 jquery,为什么要避免使用 jQuery 侦听器?
  • 我的回答是建议她不必申请,但接受这是另一种方式!
  • “做这个”类型的答案通常不是很有用。解释有什么不同以及为什么应该这样做。您正在解决哪些问题以及您是如何解决的?
【解决方案2】:

您可以使用

在字段发生更改时触发表单发布

onchange='this.form.submit()'

所以你的选择框可能看起来像这样

  <form action='' method='post'>
      <select name="dropdown" onchange='this.form.submit()'>
          <option name="option1" value="option1">This is Option 1</option>            
          <option name="option2" value="option2">This is Option 2</option>            
      </select>
  </form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多