在执行AJAX调用时,MySQL没有检索任何内容,jquery也不再工作了

时间:2017-04-28 11:32:17

标签: php jquery mysql ajax

我有一个用户表,我想当管理员点击一个用户时,一个框会在底部显示我用过的用户信息,框显示和AJAX调用从另一个页面检索代码,html /文本,但如果我使用mysql查询来填充来自数据库的用户信息的字段,则字段中不会显示任何内容。例如:

UserID:3 - 将正确显示 但
UserID:<?php echo $row[Account]; ?> - 仅显示为UserID :(为空)

另外,当管理员点击该框内的复选框时,我有一个jQuery显示一个字段,它工作正常,直到将代码放在另一个页面并使用AJAX调用它,现在它不再工作了

包含代码的其他文件:

<?php
include('../connect.php');
session_start();

  if(!empty($_POST['id'])){

    $dataID = $_POST['id'];
    $accountsAwaitingSQL = "SELECT `ID`, `Account`, `AccountID`, `Email`, `Answer1`, `Answer2`, `Answer3`, `Date` FROM AccountsAwaiting WHERE ID = $dataID";
    $accountsAwaitingResult = mysqli_query($db, $accountsAwaitingSQL);
    $accountsAwaitingRow_cnt = mysqli_num_rows($accountsAwaitingResult);

    $accountArray = array();
        while($row = mysqli_fetch_assoc($accountsAwaitingResult)){
            $accountArray[] = $row;
        }

      foreach($accountArray as $datum){
      echo json_encode("
      <form id='userAnswersForm'>
      <center>
        <b>Account Name</b>: <i><?php echo $datum[Account]; ?></i>.&nbsp; &nbsp; &nbsp; &nbsp;
        <b>Account ID</b>: <i>25</i>.&nbsp; &nbsp; &nbsp; &nbsp;
        <b>Account E-Mail</b>: <i>somedude@hotmail.com</i>.
      </center>
        <hr />

      <center>
        <b><u>Character Story</u></b><br />
        <label class='form-check-label'>
        <input type='checkbox' id='answerOneReasonCheck' class='form-check-input'>
        <small>Check this box if answer is wrong</small>
        </label>
        <div class='form-group' id='answerOneReason1' style='display:none; max-width:350px;'>
          <label for='answerOneReason'>Reason</label>
          <input type='text' class='form-control' id='answerOneReason' maxlength='250' name='answerOneReason'>
        </div>
        <blockquote><small>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus laoreet scelerisque consectetur. Nunc aliquam condimentum nunc, eget rhoncus ipsum ornare sed. Aenean arcu nunc, blandit ac arcu sit amet, euismod accumsan nisl. Cras vehicula odio erat, et scelerisque quam mollis eget. Suspendisse quis orci ullamcorper, suscipit mauris sit amet, posuere nunc. Integer pellentesque, arcu scelerisque cursus malesuada, lectus mauris fermentum elit, ac eleifend purus dolor et lectus. Curabitur dui justo, bibendum non gravida in, dignissim rutrum mauris.</small></blockquote>
        <br /><br />
        <b><u>Please explain our rules for Sexual Roleplay and Robbery</u></b><br />
        <label class='form-check-label'>
        <input type='checkbox' id='answerTwoReasonCheck' class='form-check-input'>
        <small>Check this box if answer is wrong</small>
        </label>
        <div class='form-group' id='answerTwoReason2' style='display:none; max-width:350px;'>
          <label for='answerTwoReason'>Reason</label>
          <input type='text' class='form-control' id='answerTwoReason' maxlength='250' name='answerTwoReason'>
        </div>
        <blockquote><small>Duis dignissim sit amet metus et sagittis. Vivamus hendrerit finibus maximus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Donec cursus felis id libero iaculis efficitur. Maecenas tempus eleifend neque. Vestibulum pellentesque lectus eget erat interdum lacinia. Etiam metus libero, tempor eu sem in, convallis faucibus dui. Ut iaculis vehicula est vitae facilisis. Cras vitae nisl ante. In interdum fermentum mauris, et blandit dolor vulputate sed. Donec luctus tincidunt velit, et molestie nibh egestas ac. Proin fringilla ornare fermentum. Vivamus id massa dignissim, ullamcorper ipsum ut, interdum erat. Proin congue dictum enim, lacinia egestas risus auctor nec. Donec et elementum justo. Donec ut diam a lorem sollicitudin maximus.</small></blockquote>
        <br /><br />
        <b><u>Write the definition of Meta Gaming and Power Gaming along with two examples for each</u></b><br />
        <label class='form-check-label'>
        <input type='checkbox' id='answerThreeReasonCheck' class='form-check-input'>
        <small>Check this box if answer is wrong</small>
        </label>
        <div class='form-group' id='answerThreeReason3' style='display:none; max-width:350px;'>
          <label for='answerThreeReason'>Reason</label>
          <input type='text' class='form-control' id='answerThreeReason' maxlength='250' name='answerThreeReason'>
        </div>
        <blockquote><small>Duis pharetra, erat sed tincidunt rhoncus, tellus neque laoreet augue, aliquet convallis nisi nunc eget ante. Fusce vitae justo sed elit congue auctor at et magna. In euismod iaculis eros, eu scelerisque nibh malesuada ut. Aliquam aliquet dolor sapien, a rhoncus neque accumsan ac. Pellentesque facilisis pulvinar turpis, non finibus enim. Suspendisse et massa ut dolor congue dapibus. Duis porttitor tempor ultricies. Sed luctus arcu arcu, vel elementum risus egestas vel. Vivamus vel hendrerit ligula. Fusce vel augue vestibulum, mattis nulla in, vehicula mi. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nam at tellus scelerisque, convallis neque non, eleifend eros. Aliquam sed velit et velit congue maximus.</small></blockquote>
        <br />
        <div class='form-group' style='max-width:350px;'>
          <label for='answerTwoReason'>Reason visible to other Admins</label>
          <input type='text' class='form-control' id='answerTwoReason' maxlength='180'  name='answerTwoReason'>
        </div>
        <center>
          <p>
            <span style='color:orange'>If all the answers are good then don't check any checkbox, if you spot an answer to be wrong
            then please check the box that is under its title and type in the reason for that answer.
            Even if there are no answers wrong you still need to enter a Reason for Admins, if all answers are good then just say something that you think about that player, for example say if you think that the answers are copied or not, if he did very well or not etc.</span>
          </p>
        </center>
        <button type='submit' class='btn btn-block btn-primary'>Submit</button>
      </center>
      </form>
      ");
    }
  }

?>

正如您在上面的代码中看到的那样,除Account Name:之外的所有内容都将显示在第一页上,这些内容将显示为Account Name:.,而不是在其中显示帐户名称,因此mysql没有选择什么...

这是ajax代码:

$(document).ready(function(){
    $("#rowID").click(function(){
        var id = $(this).attr("data-id");

        $.ajax({
            type: "POST",
            dataType: "json",
            contentType:"application\json;charset=utf-8",
            url: "get-data.php",
            data: {id:id},
            success: function(result){
                $("#bodyDisplay").html(result);
            }
        });
    });
});

这是显示复选框的代码,如果我在get-data.php文件中有复选框,则该代码不起作用。

$('#answerOneReasonCheck').click(function(){
    $("#answerOneReason1").toggle(this.checked);
});

$('#answerTwoReasonCheck').click(function(){
    $("#answerTwoReason2").toggle(this.checked);
});

$('#answerThreeReasonCheck').click(function(){
    $("#answerThreeReason3").toggle(this.checked);
});

上面的代码在accounts.php文件中,复选框在get-data.php中,但我回显了accounts.php文件中的复选框,所以它应该有效:?

编辑1

我检查了控制台,我看到了:

error404.000webhost.com/:1 Uncaught SyntaxError: Unexpected token <
error404.000webhost.com/:1 Uncaught SyntaxError: Unexpected token <
error404.000webhost.com/:1 Uncaught SyntaxError: Unexpected token <
DevTools failed to parse SourceMap: http://zgaming.comxa.com/pages/dist/purify.min.js.map

编辑2

通过删除<?php echo ?>并仅使用$datum[ ]

来解决此问题

但是复选框的jQuery仍无效。

编辑3

我现在在控制台中收到此错误 Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://error404.000webhost.com/?".

1 个答案:

答案 0 :(得分:0)

更改此声明

<b>Account Name</b>: <i><?php echo $datum[Account]; ?></i>.&nbsp; &nbsp; &nbsp; &nbsp;

<b>Account Name</b>: <i>$datum[Account]</i>.&nbsp; &nbsp; &nbsp; &nbsp;

希望它有所帮助!一切顺利!