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

时间:2015-08-25 14:22:07

标签: javascript php jquery ajax

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

HTML(index.html):

30

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

  <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>

PHP文件(edit.php):

$(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");
      }
    });
  });
});

HTML显示表中数据库的数据,每行旁边都有一个按钮。使用jQuery,我得到了单击按钮的行的id。我想把这个id发送到我的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> 文件中的发送变量(fileID)。

AJAX调用成功部分中的edit.php被执行。 我需要修理什么?我以为我把一切都搞定了。

我还试图将AJAX调用的url更改为alert,但这也不起作用。然后成功部分将不会被执行。

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

项目结构如下:

  • 项目(*)
    • edit.php
    • 的index.php
    • 脚本(*)
      • functions.js

(*)目录

编辑:

  

错误消息:注意:未定义的变量:第10行的C:\ xampp \ htdocs \ kuhlnotesweb \ edit.php中的fileID

3 个答案:

答案 0 :(得分:2)

AJAX返回数据成功变量中页面的内容。尝试使用console.log(数据),您应该看到您的变量已经回显到返回的HTML中。

如果没有,请检查dev工具中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>

答案 1 :(得分:0)

当您尝试传递数据时,问题是js中的语法错误 你的代码。见js小提琴示例http://jsfiddle.net/mdamia/njd8m0g5/4/。如何从tr获得价值。

  data: ({ fileID : fileID }), 

应该是数据:

 { fileID : fileID } // no ()

经过测试,确实有效。

来自jQuery AJAX

$.ajax({
  method: "POST",
  url: "some.php",
    data: { name: "John", location: "Boston" }
  })
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
});

答案 2 :(得分:0)

这是我现在的解决方案:

functions.js:

$(document).ready(function() {
  var fileID, fileName, fileDescription, fileCategory, fileProject;
  $('.table-edit-button').click(function() {
    fileID = $(this).parent().attr('id');
  });

  $('#edit-toggle').click(function() {
    $.ajax({
      url: 'ajax-edit.php',
      type: 'post',
      data: { fileID : fileID },
      dataType: 'json',
      success: function(data) {
        fileName = data.filename;
        fileDescription = data.filedescription;
        fileCategory = data.categoryname;
        fileProject = data.projectname;
        $('#edit-fileid').val(fileID);
        $('#edit-filename').val(fileName);
        $('#edit-description').val(fileDescription);
        $('#edit-projectname').val(fileProject);
        $('#edit-categoryname').val(fileCategory);
      }
    });
  });
});

Ajax的edit.php:

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

include_once('connect.php');
$result = $connect->query("SELECT file.name AS 'filename', file.description AS 'filedescription', project.name AS 'projectname', category.name AS 'categoryname'
  FROM file, project, category, file_has_project, file_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 file.idFile = '".$fileID."'");
$result = $result->fetch_assoc();

echo json_encode($result);
 ?>

edit.php:

<div class="edit-content">
  <h2 class="edit-content-header">Bearbeiten<img src="images/cross.gif" /></h2>
  <div>
    <form action="edit.php" method="post">
      <input type="hidden" id="edit-fileid" name="edit-fileid"/>
      <input type="text" id="edit-filename" name="edit-filename" placeholder="Dateiname"/>
      <input type="text" id="edit-username" name="edit-username" placeholder="Benutzer"/>
      <textarea id="edit-description" name="edit-description" placeholder="Erste Zeile" rows="7em" cols="100"></textarea>
      <select id="edit-categoryname" name="edit-categoryname">
        <option disabled selected value="category-first">Kategorie</option>
        <?php
          include_once('connect.php');
          $result = $connect->query("SELECT category.name FROM category,user,user_has_category WHERE user.idUser = user_has_category.user_idUser AND user_has_category.category_idCategory = category.idCategory AND user.idUser = '".$_SESSION['userid']."'");
          if($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
              echo "<option value='".$row['name']."'>".$row['name']."</option>";
            }
          }
        ?>
      </select>
      <select id="edit-projectname" name="edit-projectname">
        <option disabled selected value="project-first">Projekt</option>
        <?php
          $result = $connect->query("SELECT project.name FROM project,user,user_has_project WHERE user.idUser = user_has_project.user_idUser AND user_has_project.project_idProject = project.idProject AND user.idUser = '".$_SESSION['userid']."'");
          if($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
              echo "<option value='".$row['name']."'>".$row['name']."</option>";
            }
          }
        ?>
      </select>

      <input type="image" name="submit-button" id="savebtn" src="images/save.gif" />
    </form>
  </div>
</div>

我认为我试图实现的目标存在许多误解,以及我无法理解AJAX的工作原理。 edit.php只是HTML中的一小部分,包含在index.php中,而我正在制作的AJAX调用是在同一个文件中生成的,所以我无法使用我通过AJAX发送的变量我的edit.php文件,因为在我想要使用它的地方甚至没有发送,因此$_POST['fileID']未定义。 我的解决方案是我创建了一个单独的php文件(ajax-edit.php),在那里我从数据库中检索我需要的信息并将其作为JSON对象返回。有了这个,我可以使用JSON对象中的信息来更改输入字段的值。

感谢大家的帮助,对不起,我很顽固^^。