Ajax PHP数据库选择不起作用

时间:2018-08-09 15:24:11

标签: php jquery ajax

我需要帮助。我有一个选择代码无法在Ajax中正常工作。 我实际上不知道我在哪里弄错了,但是代码不起作用。 这是代码:

<?php

require_once 'config/dbconfig.php';

if (isset($_REQUEST['note_id'])) {

    $id = intval($_REQUEST['note_id']);
    $query = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE note_id = :id";
    $stmt = $pdo->prepare( $query );
    $stmt->execute(array(':id'=>$id));
    $row=$stmt->fetch(PDO::FETCH_ASSOC);


    ?>


    <div class="card-body">

                    <div class="col-md-12 mb-4">
                        <h5 class="text-center"><?php echo $row['class']; ?></h5>
                    </div>
                    <div class="row">
                     <div class="col-md-6 mb-4">
                        <h5 class="text-center"><?php echo $row['subject']; ?></h5>
                    </div>
                    <div class="col-md-6 mb-4">
                        <h5 class="text-center"><?php echo $row['topic']; ?></h5>
                    </div>
                </div>
                     <div class="col-md-12 mb-4">
                        <?php echo $row['content']; ?>
                    </div>
                    <div class="row">
                    <div class="col-md-4 mb-4">
                        <h5 class="text-center"></h5>
                    </div>
                    <div class="col-md-4 mb-4">
                        <h5 class="text-center"></h5>
                    </div>
                    <div class="col-md-4 mb-4">
                        Created By: <h5 class="text-center"><?php echo $row['author']; ?></h5>
                    </div>
                     </div>


                    </div>

    <?php               
}
?>

是ajax,它假定将上述选择代码带到父页面。但是不起作用。

以下是ajax代码:

<script>
  $(document).ready(function(){

   $(document).on('click', '#getUser', function(e){

    e.preventDefault();

        var uid = $(this).data('note_id');   // it will get id of clicked row

        $('#dynamic-content').html(''); // leave it blank before ajax call
        $('#modal-loader').show();      // load ajax loader

        $.ajax({
            url: 'getnotes.php',
            type: 'POST',
            data: 'note_id='+uid,
            dataType: 'html'
        })
        .done(function(data){
            console.log(data);  
            $('#dynamic-content').html('');    
            $('#dynamic-content').html(data); // load response 
            $('#modal-loader').hide();        // hide ajax loader   
        })
        .fail(function(){
            $('#dynamic-content').html('<i class="glyphicon glyphicon-info-sign"></i> Something went wrong, Please try again...');
            $('#modal-loader').hide();
        });

    });

 });

</script>

通过锚标记调用它的是锚代码: 实际上是在使用foreach循环。

  <li><a data-id="<?php echo $row['note_id']?>" href="#1" class="waves-effect" id="getUser"><?php echo $row['topic']?></a>
                                    </li>

当我单击链接时,它可以工作,但不会在父div中显示请求。

2 个答案:

答案 0 :(得分:0)

在PHP中,您正在将数据属性添加到锚点。这是data-id

<a data-id="<?php echo $row['note_id']?>" ...

在客户端脚本中,您得到了错误的数据属性。 data-note_id不存在。我认为您混淆了数据库列名称和您提供的数据属性名称。

var uid = $(this).data('note_id');

应该是:

var uid = $(this).data('id');

答案 1 :(得分:0)

我假设您正在尝试获取属性“ data-id”的值。

在这里。

<search:plan xmlns:search="http://marklogic.com/appservices/search">
  <qry:query-plan xmlns:qry="http://marklogic.com/cts/query">
    <qry:expr-trace>impl:apply-search(map:map(&lt;map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" .../&gt;), "xdmp:plan", fn:false())</qry:expr-trace>
    <qry:info-trace>Analyzing path for search: fn:collection()/es:envelope/es:raw/wos_dps:REC/wos_dps:static_data/wos_dps:summary/wos_dps:titles/wos_dps:title</qry:info-trace>
    <qry:info-trace>Step 1 is searchable: fn:collection()</qry:info-trace>
    <qry:info-trace>Step 2 is searchable: es:envelope</qry:info-trace>
    <qry:info-trace>Step 3 is searchable: es:raw</qry:info-trace>
    <qry:info-trace>Step 4 is searchable: wos_dps:REC</qry:info-trace>
    <qry:info-trace>Step 5 is searchable: wos_dps:static_data</qry:info-trace>
    <qry:info-trace>Step 6 is searchable: wos_dps:summary</qry:info-trace>
    <qry:info-trace>Step 7 is searchable: wos_dps:titles</qry:info-trace>
    <qry:info-trace>Step 8 is searchable: wos_dps:title</qry:info-trace>
    <qry:info-trace>Path is fully searchable.</qry:info-trace>
    <qry:info-trace>Gathering constraints.</qry:info-trace>
    <qry:info-trace>Search query contributed 2 constraints: cts:and-query((cts:word-query("pancreatic cancer", ("case-insensitive","punctuation-insensitive","whitespace-insensitive","wildcarded","lang=en"), 1), cts:collection-query("OncoWatch")), ())</qry:info-trace>
    <qry:partial-plan>
      <qry:term-query weight="1">
    <qry:key>17396426271722676659</qry:key>
      </qry:term-query>
    </qry:partial-plan>
    <qry:partial-plan>
      <qry:term-query weight="0">
    <qry:key>16172058720999549713</qry:key>
    <qry:annotation>collection(OncoWatch)</qry:annotation>
      </qry:term-query>
    </qry:partial-plan>
    <qry:info-trace>Executing search.</qry:info-trace>
    <qry:ordering/>
    <qry:final-plan>
      <qry:and-query>
    <qry:term-query weight="0">
      <qry:key>3030157765137304984</qry:key>
      <qry:annotation>descendant(doc-root(element(es:envelope),doc-kind(document)))</qry:annotation>
    </qry:term-query>
    <qry:term-query weight="0">
      <qry:key>14623301529635238289</qry:key>
      <qry:annotation>descendant(element-child(es:envelope/es:raw))</qry:annotation>
    </qry:term-query>
    <qry:term-query weight="0">
      <qry:key>2513953156802156363</qry:key>
      <qry:annotation>descendant(element-child(es:raw/wos_dps:REC))</qry:annotation>
    </qry:term-query>
    <qry:term-query weight="0">
      <qry:key>15620627614067983352</qry:key>
      <qry:annotation>descendant(element-child(wos_dps:REC/wos_dps:static_data))</qry:annotation>
    </qry:term-query>
    <qry:term-query weight="0">
      <qry:key>14267880419025656818</qry:key>
      <qry:annotation>descendant(element-child(wos_dps:static_data/wos_dps:summary))</qry:annotation>
    </qry:term-query>
    <qry:term-query weight="0">
      <qry:key>1256980818223445232</qry:key>
      <qry:annotation>descendant(element-child(wos_dps:summary/wos_dps:titles))</qry:annotation>
    </qry:term-query>
    <qry:or-two-queries>
      <qry:term-query weight="0">
        <qry:key>7002325115516888131</qry:key>
        <qry:annotation>element-child(wos_dps:titles/wos_dps:title)</qry:annotation>
      </qry:term-query>
      <qry:term-query weight="0">
        <qry:key>14914226620346018347</qry:key>
        <qry:annotation>link-child(descendant(element-child(wos_dps:titles/wos_dps:title)))</qry:annotation>
      </qry:term-query>
    </qry:or-two-queries>
    <qry:term-query weight="1">
      <qry:key>17396426271722676659</qry:key>
    </qry:term-query>
    <qry:term-query weight="0">
      <qry:key>16172058720999549713</qry:key>
      <qry:annotation>collection(OncoWatch)</qry:annotation>
    </qry:term-query>
      </qry:and-query>
    </qry:final-plan>
    <qry:info-trace>Selected 1337 fragments to filter</qry:info-trace>
    <qry:result estimate="1337"/>
  </qry:query-plan>
</search:plan>