【问题标题】:ajax method ($.post) with mysql not working simple code带有mysql的ajax方法($ .post)不工作的简单代码
【发布时间】:2018-02-12 11:16:16
【问题描述】:

我有这个代码不工作的问题,我想显示 mysql 的结果而不发送数据 我的简单代码 ajax

$('.myClass').on('click',function(){


    $.post('resultAll.php');
});

代码html

<li class="myClass" > click this </li>

在页面上显示结果的代码php / mysql(名称resultAll.php)

 $stmt = $connect->prepare('SELECT * FROM table WHERE price = :aff');
$stmt->execute(array(':aff'=>'5'));
$res = $stmt->fetchAll(); 
foreach ($res as $row) {

    ?>
    <div class="noon" style="width: 1000px;height: 500px">
        <?php print_r($row['id'].' ');  ?>
    </div>

结果这些什么都没有

【问题讨论】:

  • 如果您使用的是PDO或mysqli,请删除$stmt-&gt;execute(array(':aff'=&gt;'5'));中的双冒号或分享$stmt-&gt;execute背后的代码
  • 那么是PHP代码不起作用还是通过jQuery POST?因为你的 POST 什么都不发送,至少对响应什么也不做。
  • jquery 代码不工作

标签: javascript php jquery mysql ajax


【解决方案1】:

根据您的代码 sn-p,您希望从 resultAll.php 文件中获取数据到您的 HTML 页面中。如果我是正确的那么你的代码是错误的,你必须使用下面的方法而不是 $.post() 方法

$('.myClass').on('click',function(){
    $("#result").load('resultAll.php');
});

你的 HTML 代码应该是

<li class="myClass" > click this </li>

....
...

<div id="result"></div>

我的意思是在你的页面中必须有一个带有id="result"的div标签。

我希望这对你有用。

【讨论】:

    【解决方案2】:

    正如您所说,您的 Jquery 无法正常工作。你的元素.myClass是用JS动态创建的吗?确保这一点:

    $(document.body).on('click', '.myClass', function(){ $.post('resultAll.php'); });

    编辑

    将来自$.post 的响应添加到正文:

    $('.myClass').click(function(){
        $.post('resultAll.php', function(response){
            $(body).append(response);
        });
    });
    

    【讨论】:

    • jquery 正确但不显示任何内容,我只想显示页面 resultAll.php 的结果 mysql 代码。 $.post('resultAll.php') 没有运行我的问题
    猜你喜欢
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2015-07-21
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    相关资源
    最近更新 更多