簡體   English   中英

使用JavaScript的Ajax調用不起作用

[英]Ajax call with javascript not working

我想嘗試從ajax調用的頁面cart.php中獲取一些價值,但無法正常工作。 Ajax代碼如下:

function temp_sell(id) {
    //var p_id=document.getElementById('temp').value;
    alert(p_id);
    $.ajax({
        type: "POST",
        url: "temp_sell.php",
        data: "value=" + p_id,
        success: function (message) {
            alert(message);
            $("#your_cart").html(message);
        }
    });
}

temp_sell.php是我要在下面顯示一些產品詳細信息的地方

<?php 
include("connection.php");
echo $p_id = $_POST['value'];
$qty=1;
$query="SELECT * FROM product WHERE id='$p_id'";
    $result=mysql_query($query);
    while($data=mysql_fetch_array($result))
    {
    ?>
            <form>
            <table>
                <tr>
                    <img src="<?php echo "img/".$data['image'];?>"/>
                    <strong><?php echo $data['pro_name'];?></strong>
                    <strong><?php echo $data['price'];?></strong>
                    <input type="text" value="<?php echo $qty;?>"/>
                </tr>
            </table>
            </form>
    <?php
    }                   
?>  

p_id未定義,您已注釋了p_id值或更改了temp_sell(p_id)。

function temp_sell(p_id)

代替

function temp_sell(id)

參考評論: 使用javascript的Ajax調用不起作用

做這個 :

function temp_sell(id){
var p_id=document.getElementById('temp').value; // <--- uncomment this line
alert(p_id);
$.ajax({
         type: "POST", 
         url: "temp_sell.php",
         data: {value:p_id}, // <--- change this
         success: function(message){ 
         alert(message);
         $("#your_cart").html(message);
        }       
       });
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM