游.程 2013-08-23 20:30 采纳率: 0%
浏览 24

PHP MySQL jQuery / AJAX问题

I have problem with jQuery.

This is my jQuery code:

function UpdateRecord(id)
  {
      jQuery.ajax({
       type: "GET",
       url: "index.php",
       data: 'id='+id,
       cache: true,
       success: function(data)
       {
         $('#output').html(data);

       }
     });
 }

UpdateRecord(id) get data from here :

echo"<select>";
    foreach($products->fetch_products($id) as $product)
    {
        $price = $product->price;
     echo"<option value = $product->id onclick=\"UpdateRecord($product->id)\">$product->p_name</option>";
    }
    echo"</select>";

This is piece of the HTML and php code where new result should be <div id="output"></div> :

$i = 0;
    foreach($products->fetch_products($id) as $pricer){
    if ($i==1) break;    
    echo"<td>";
    echo"<center>";
    echo("<div id=\"output\"><p>$pricer->price</p></div>");
    echo"</center>";
    echo"</td>";
    $i++;
    }

And finally this is the PHP function which return the price :

public function update_price($id){

        $stmt = $this->connect()->prepare("SELECT `price` FROM `products` WHERE id=?");

        $stmt->bindValue(1,$id);

        $stmt->execute();

        $row = $stmt->fetchAll(PDO::FETCH_OBJ);

         //echo json_encode($row);

         foreach($row as $data){

            echo $data->price;

         }
}

I tested my PHP function and it's working properly.

The problem is when I click on my dropdwon list and choose product the whole table is repeating aggain in "Price" field like this :

Normal :

http://i44.tinypic.com/34473mp.jpg

Problem :

http://i43.tinypic.com/3449xmw.jpg

EDIT

This the table :

<tbody>

<?php




foreach($category->fetch_category() as $data){

    $id = $data->id;

    echo"<tr>";
    echo"<td>";
    echo"<center>";
    echo"<input type=\"hidden\" value=\"$data->id\" />";
    echo"<p><strong>".$data->cat_name."</strong></p>";
    echo"</center>";
    echo"</td>";
    echo"<td>";
    echo"<center>";

    echo"<select>";
    foreach($products->fetch_products($id) as $product)
    {
        $price = $product->price;
    echo"<option value = $product->id onclick=\"UpdateRecord($product->id)\">$product->p_name</option>";
    }
    echo"</select>";

    echo"</center>";
    echo"</td>";

    echo"<td>";
    echo"<center>";
    echo"<input class=\"input-mini\" type=\"text\" value=\"1\"/>";
    echo"</center>";
    echo"</td>";
    $i = 0;
    foreach($products->fetch_products($id) as $pricer){
    if ($i==1) break;    

    echo"<td id=\"$pricer->category_id\">";

    echo"<center>";
    echo("<div id=\"output\"><p>$pricer->price</p></div>");
    echo"</center>";
    echo"</td>";
    $i++;
    }
    echo"</tr>";
}

?>
<tr>
<td></td>
<td></td>
<td></td>
<td>Total: </td>
</tr>

</tbody>

Thanks in advance ! I am trying to fix this for whole day...

  • 写回答

3条回答 默认 最新

  • weixin_33733810 2013-08-23 20:35
    关注

    You are possibly echoing the entire HTML Page from the URL that receives the AJAX request. That is the reason it is bringing in a whole new page and fitting it in the place of $('#output'). Can you post the Code of the page that is receiving the AJAX request?

    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码