2014-01-10 101 views
0

我想尝试从页面cart.php和ajax调用获得一些价值,但它不工作。 阿贾克斯代码如下:Ajax调用javascript不工作

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 
    }     
?> 
+0

和PHP代码在哪里? – Goikiu

+0

你会收到错误? –

+0

请显示您的php代码。 –

回答

1

p_id是不确定的,你都谈到了p_id值或更改temp_sell(P_ID)。

function temp_sell(p_id) 

代替

function temp_sell(id) 

参考意见:Ajax call with javascript not working

+0

是的,我纠正它,但仍然不工作,我不能从temp_sell.php –

+0

@ForhadSikder得到任何东西:因为,作为其他2个答案解释,你没有获取结果集作为关联数组('while($ data = mysql_fetch_array($ result))'应该是'while($ data = mysql_fetch_assoc($ result))') –

+0

当你调用该函数时,你是否得到了'alert'。确保你的jQuery libaray路径是正确的。 Checck您的浏览器控制台,它可能会通过一些错误,如果有的话 –

0

这样做:

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); 
     }  
     }); 
    } 
+0

'mysql_fetch_array' - 获取一个结果行作为关联数组,或数值数组或 –

+0

谁说的? –

+1

这里检查 - http://in3.php.net/mysql_fetch_array –