javascript - 使用 AJAX 发送的变量在 PHP 中未定义

标签 javascript php jquery ajax

我正在尝试使用 AJAX 将变量从 Javascript 发送到 PHP。

HTML(index.html):

  <div class="table-popup">
    <ul>
      <li id="edit-toggle">Bearbeiten</li>
      <li>Zu Favoriten hinzufügen</li>
      <li>Datei öffnen</li>
      <li>Im Ordner öffnen</li>
      <li>Löschen</li>
    </ul>
  </div>

  <div class="main-content">
    <h2 class="main-content-header">Datenbank</h2>
    <div id="table">
      <table>
        <thead>
          <tr class="table-row" tabindex="1">
            <th class="fixed-header"></th>
            <th>Dateiname</th>
            <th>Benutzer</th>
            <th>Erstelldatum</th>
            <th>Änderungsdatum</th>
            <th>Erste Zeile</th>
            <th>Kategorie</th>
            <th>Projekt</th>
          </tr>
        </thead>
        <?php
        include_once('connect.php');
        $result = $connect->query("SELECT file.name AS 'filename', file.description AS 'filedescription', category.name AS 'categoryname', project.name AS 'projectname', user.name AS 'username', idFile
          FROM file, category, project, file_has_project, file_has_category, user, user_has_project, user_has_category
          WHERE file.idFile = file_has_project.file_idFile AND file_has_project.project_idProject = project.idProject AND file.idFile = file_has_category.file_idFile AND file_has_category.category_idCategory = category.idCategory AND user.idUser = user_has_project.user_idUser AND user_has_project.project_idProject = project.idProject AND user.idUser = user_has_category.user_idUser AND user_has_category.category_idCategory = category.idCategory AND user.idUser = '".$_SESSION['userid']."'");
          //echo "<tbody><td>".$result->num_rows."</td></tbody>";
        if ($result->num_rows > 0) {
          echo "<tbody>";
          while($row = $result->fetch_assoc()) {
            echo "<tr class='table-row' tabindex='1' id='".$row['idFile']."'>";
            echo "<th class='table-edit-button fixed-header'><img src='images/open.gif' /></th>";
            echo "<td>".$row['filename']."</td>";
            echo "<td>".$row['username']."</td>";
            echo "<td>-</td>";
            echo "<td>-</td>";
            echo "<td>".$row['filedescription']."</td>";
            echo "<td>".$row['categoryname']."</td>";
            echo "<td>".$row['projectname']."</td>";
            echo "</tr>";
          }
          echo "</tbody>";
        }
        ?>
      </table>
    </div>
  </div>

使用 jQuery (functions.js) 发送 AJAX 请求的 Javascript:

$(document).ready(function() {
  var fileID;
  $('.table-edit-button').click(function() {
    fileID = $(this).parent().attr('id');
  });
  $('#edit-toggle').click(function() {
    $.ajax({
      url: 'edit.php',
      type: 'post',
      data: { fileID : fileID },
      success: function(data) {
        alert("Success");
      }
    });
  });
});

PHP 文件(edit.php):

<?php
if (isset($_POST['fileID']))
 $fileID = $_POST['fileID'];
?>

<div class="edit-content">
  <h2 class="edit-content-header">Bearbeiten<img src="images/cross.gif" /></h2>
  <div>
    <form action="" method="post">
      <?php echo $fileID; ?>
      <input type="text" placeholder="Dateiname"/>
      <input type="text" placeholder="Benutzer"/>
      <textarea placeholder="Erste Zeile" rows="7em" cols="100"></textarea>
      <select name="Kategorie">
        <option disabled selected>Kategorie</option>
        <?php
          include_once('connect.php');
          $result = $connect->query("SELECT name FROM category");
          if($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
              echo "<option value='".$row['name']."'>".$row['name']."</option>";
            }
          }
        ?>
      </select>
      <select name="Projekt">
        <option disabled selected>Projekt</option>
        <?php
          $result = $connect->query("SELECT name FROM project");
          if($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
              echo "<option value='".$row['name']."'>".$row['name']."</option>";
            }
          }
        ?>
      </select>

      <img id="savebtn" src="images/save.gif" />
    </form>
  </div>
</div>

HTML 在表格中显示数据库中的数据,每行旁边都有一个按钮。使用 jQuery,我获得了单击按钮的行的 id。我想将此 id 发送到我的 php 文件以在那里执行一些操作(目前不相关)。 我遇到的问题是我无法访问 edit.php 文件中的发送变量 (fileID)。

AJAX 调用成功部分的 alert 被执行。 我需要修复什么?我以为我一切都是对的。

我还尝试将 AJAX 调用的 url 更改为 ../edit.php 但这也不起作用。那么成功部分就不会被执行。

不同的变量名也不起作用。

项目结构如下:

  • 项目 (*)
    • 编辑.php
    • index.php
    • 脚本 (*)
      • functions.js

(*)目录

编辑:

The error message: Notice: Undefined variable: fileID in C:\xampp\htdocs\kuhlnotesweb\edit.php on line 10

最佳答案

AJAX 在 data success 变量中返回页面内容。尝试 console.log(data) ,您应该会看到变量已回显到返回的 HTML 中。

如果没有,请在开发工具中检查 fileID 参数是否确实附加到请求中。

更新

<div class="edit-content">
  <h2 class="edit-content-header">Bearbeiten<img src="images/cross.gif" /></h2>
  <div>
    <form action="" method="post">
      <?php
          if (isset($_POST['fileID'])) {
               echo $_POST['fileID'];
          }
      ?>
      <input type="text" placeholder="Dateiname"/>
      <input type="text" placeholder="Benutzer"/>
      <textarea placeholder="Erste Zeile" rows="7em" cols="100"></textarea>
      <select name="Kategorie">
        <option disabled selected>Kategorie</option>
        <?php
          include_once('connect.php');
          $result = $connect->query("SELECT name FROM category");
          if($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
              echo "<option value='".$row['name']."'>".$row['name']."</option>";
            }
          }
        ?>
      </select>
      <select name="Projekt">
        <option disabled selected>Projekt</option>
        <?php
          $result = $connect->query("SELECT name FROM project");
          if($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
              echo "<option value='".$row['name']."'>".$row['name']."</option>";
            }
          }
        ?>
      </select>

      <img id="savebtn" src="images/save.gif" />
    </form>
  </div>
</div>

关于javascript - 使用 AJAX 发送的变量在 PHP 中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32206615/

相关文章:

javascript - 使用 draft-js 自动大写句子的第一个字母

php - PDO::__construct():服务器发送的字符集 (255) 客户端未知。请向开发人员报告

php - 如何使用redbeanphp删除父bean及其子对象?

Javascript:如何获取表格中选定的复选框项目

javascript - 模拟更改不适用于 formik 的选择元素

java - 如何解析 JSDoc

javascript - 在javascript中的光标位置插入图像 - 显示 [object HTMLImageElement] 而不是图像

java - 如何从多个本地网站提取信息?

javascript - 在客户端访问嵌套转发器的文本框

javascript - 根据ajax结果提交表单