doushi8187 2013-10-01 09:48
浏览 43
已采纳

jquery / ajax请求服务器并显示所选记录

I am wondering how jquery/ajax can send a query to the server and then let the server (php) calculte the response, and append it to the webpage with jquery/ajax again?

Example : I have 3 products in database with the details for each product (name, description and image). I have a page with links to these 3 products and I want to display only the clicked product details with jquery/ajax :

html :

<nav>
    <ul>
        <li><a href="#prod1">prod1</a></li>
        <li><a href="#prod2">prod2</a></li>
        <li><a href="#prod3">prod3</a></li>
    </ul>
</nav>
<div></div>

JS

$(document).ready(function() {
    $('a').click(function(){
        $('div').load("products.php");
    });
});

this is what my products.php file looks like :

<?php
try
    {$bdd = new PDO('mysql:host=localhost;dbname=ddb', 'root', '');}
catch (Exception $e)
    {die('Error : ' . $e->getMessage());}
$reponse = $bdd->query('SELECT * FROM products ORDER BY id DESC') or die(print_r($bdd->errorInfo()));
?>

<div class="prod_details">
    while ($data = $reponse->fetch()){ ?>
        <h1><?php echo $products['name']; ?></h1>
        <p><?php echo $products['description']; ?></p>
        <img src="<?php echo $products['image_path']; ?>">
    <?php }?>
</div>

This displays all the products, how can I get only the clicked product to be displayed?

  • 写回答

2条回答 默认 最新

  • doudengjin8251 2013-10-01 10:04
    关注

    there is a few things wrong here. First of all your js:

    $('.div').load("products.php");
    

    will try to load products.php into an element with the class of div not into the div element.

    secondly it will try to load products.php from the same directory location as your js script. you probably want to do /products.php and then place products.php in the root.

    Finally your HTML is incorrect as you need a UL or OL tag around your LI tags.

    To get the php to return the individual product you will need to pass the product Id to your PHP and then amend the SQL query to accept a WHERE id clause. something like this:

    HTML:

    <nav>
        <ul>
        <li><a href="#prod1" data-id="1">prod1</a></li>
        <li><a href="#prod2" data-id="2">prod2</a></li>
        <li><a href="#prod3" data-id="3">prod3</a></li>
        </ul>
    </nav>
    <div></div>
    

    JS:

    $(document).ready(function() {
        $('a').click(function(){
            $('div').load("products.php?id=" + $(this).attr('data-id'));
        });
    });
    

    Then your PHP query should be:

    $id = $_GET['id'];
    query("SELECT * FROM products WHERE `id` = '$id' ORDER BY id DESC")
    

    That should do it for you, with a bit of fiddling.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)