Ajax 请求未完成,值未反映在数据库中

问题描述 投票:0回答:1

我正在尝试创建可编辑的表格,我按照 youtube 上的教程使用 ajax 请求进行相同的操作,但是编辑表格后保存更改的 ajax 请求似乎失败了,请建议我可能导致的错误错误“未保存”。 主页代码

 <script type='text/javascript'>
$(document).ready(function(){
  
 // Show Input element
 $('.edit').click(function(){
  $('.txtedit').hide();
  $(this).next('.txtedit').show().focus();
  $(this).hide();
 });
 
 // Save data
 $(".txtedit").focusout(function(){
   
  // Get edit id, field name and value
  var id = this.id;
  var split_id = id.split("_");
  var field_name = split_id[0];
  var edit_id = split_id[1];
  var value = $(this).val();
   
  // Hide Input element
  $(this).hide();
 
  // Hide and Change Text of the container with input elmeent
  $(this).prev('.edit').show();
  $(this).prev('.edit').text(value);
 
  $.ajax({
  url: 'update.php',
  method: 'POST',
  data: { field:field_name, value:value, id:edit_id },
  success:function(response){
    if(response == "success"){ 
      console.log('Saving successfully'); 
    }else{ 
      console.log("Not saving.");  
    }
  },
  error: function(xhr, status, error) {
    console.log("Error: " + xhr.responseText);
  }
});
  
 });
 
});
</script>

</head>
<body>
<div id="bgn"></div>
<div id="titlebar">
  <h1 id="pageTitle">PROJECT MANAGEMENT SYSTEM</h1>
  <div id="bg1"></div>

<div id="menu">
  <ul id="menuitems">
    <li>
      <div class="menu-item">
        <span class="icon-signini" onclick="signOut()"></span>
        <span class="item" onclick="signOut()">Sign Out</span>
      </div>
    </li>

    <li>
      <div class="menu-item" id="masterdata">
          <span class="icon-dashbi"></span>
          <span class="item">Master Data</span>
        <!-- Submenu -->
        <ul id="projectSubMenu" class="submenu" onclick="department_table()">
          <li><span>Department List</span></li>
          <div class="departable">


</div>
          <li><span onclick="project_table()">Projects List</span></li>
        </ul>
      </div>
    </li>

    <li>
      <div class="menu-item">
        <span class="icon-transi"></span>
        <span class="item">Transactions</span>
        <!-- Submenu -->
      </div>
    </li>

    
<li>
      <div class="menu-item">
        <div class="project">
          <span class="icon-requestpi"></span>
          <span class="item" onmouseover="toggleSubmenu('projectSubMenu')">Reports</span>
        <!-- Submenu -->
        <ul id="projectSubMenu" class="submenu">
        </ul>
      </div>
    </li>

  </ul>
</div>
</div>


<div id= "tablePane"  style="display: none;">
<div id= departtableTitle>
  <h1>DEPARTMENT TABLE</h1>
  </div>
  <div id= buttons>
  <button class="btn" type="button" name="add">Add</button> 
  <button class="btn" type="button" name="save" onclick="save">Delete</button>
  <button class="btn" type="button" name="save">Edit</button>
  <button class="btn" type="button" name="save">Save</button>
  </div>
<div class="tableContainer">
    <table class="departable" id="department_table">
    <thead>
        <th>Sr No.</th>
        <th>Name</th>
        <th>Username</th>
        <th>Email</th>
        <th>Department</th>
</thead>

<tbody>
        <?php
        include('db.php');

        // Create connection
        $conn = new mysqli($hostname, $user_name, $pass_word, $database);
        
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        
        $query_departid= " SELECT `department` FROM `department id`";
        $query_departable= " SELECT `id`,`name`,`username` ,`email`, `department id` FROM `users`order by `id`";
        $result_departable= mysqli_query($conn,$query_departable);

         $counter = 1; // Initialize counter
         while($row_departatble = mysqli_fetch_assoc($result_departable))
            {
          ?>
            <tr>  
            <td><?php echo $counter; ?></td>
            <td>
            <div class='edit' > <?php echo $row_departatble['name'];?></div> 
            <input type='text' class='txtedit' value='<?php echo $row_departatble['name'];?>' id='name_<?php echo $row_departatble['id'];?>' >
            </td>
            <td>
            <div class='edit' > <?php echo $row_departatble['username'];?></div> 
            <input type='text' class='txtedit' value='<?php echo $row_departatble['username'];?>' id='username_<?php echo $row_departatble['id'];?>' >
            </td>
            <td>
            <div class='edit' > <?php echo $row_departatble['email'];?></div> 
            <input type='text' class='txtedit' value='<?php echo $row_departatble['email'];?>' id='email_<?php echo $row_departatble['id'];?>' >
            </td>
            <td>
            <div class='edit' > <?php echo $row_departatble['department id'];?></div> 
            <input type='text' class='txtedit' value='<?php echo $row_departatble['department id'];?>' id='department_<?php echo $row_departatble['id'];?>' >
            <?php echo $row_departatble['department id'];?></td>
            </tr>
            <?php
            }
            $count ++;
            ?>

</tbody>          
    </table>
          </div> 
</div>

更新.php

<?php
include('db.php');
if(isset($_POST['field']) && isset($_POST['value']) && isset($_POST['id'])){
   $field = $_POST['field'];
   $value = $_POST['value'];
   $editid = $_POST['id'];
 
    $sql = "UPDATE user SET ".$field."='".$value."' WHERE id = $editid"; 
    $update = $conn->query($sql); 
 
   echo 1;
}else{
   echo 0;
}
exit;
?>

db.php


<?php
// Database configuration
$hostname = "localhost"; // Replace with your host name
$user_name = "root"; // Replace with your database username
$pass_word = ""; // Replace with your database password
$database = "project_ms"; // Replace with your database name

// Establishing the database connection
$conn = mysqli_connect($hostname, $user_name, $pass_word, $database);

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  exit();
}
?>

browser console

php html css ajax database
1个回答
0
投票

在客户端 JS 端,您正在检查

response == "success"
,但在后端 PHP 端,您正在输出
echo 1

有两种解决方案:

  1. 将 JS 条件更改为
    if(response == "1")
    而不是
    if(response == "success")

  1. 将 PHP 响应更改为
    echo "success";
    而不是
    echo 1;
© www.soinside.com 2019 - 2024. All rights reserved.