弹出/ Jquery和;数据库

时间:2012-10-23 19:47:13

标签: php jquery database popup

我需要创建一个动态生成的窗口,该窗口显示从数据库中获取的图像和各种元素。弹出窗口有效,但它没有得到我试图使用onclick函数获得的“id”。

使用控制台我收到此消息:XMLHttpRequest无法加载getProduct.php?id = undefined。 Access-Control-Allow-Origin不允许使用null。我不知道该怎么做才能进一步定义“id”。

HTML& JS:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript">
$(document).ready(function() {

onClickRegister = function(){   
var id = $(this).attr('id');

    b = $("#blanket");
    b.css("display","block");
    b.css("position","fixed");
    b.css("width", window.innerWidth);
    b.css("height", window.innerHeight);
    b.css("background-color", "rgba(0, 0, 0, 0.7)");
    f=$("#openImg");
    f.css("position", "fixed");
    f.css("top", "10%");
    f.css("left", "10%");

    $.getJSON("getProduct.php?id="+id,function(data){
        var img = data.data.prodImg;
        var name = data.data.prodName;
        var price = data.data.prodPrice;
        var description = data.data.prodDesc;
        var qty = data.data.prodQty;
        $("#openImg").html("<p style='float:right; margin: 0; padding: 0;'><a href='#' id='close' onClick='onClickClose();'>[X]</a></p><br /><img src='"+img+"' width='544' height='512' /><br />Name: "+name+"<br />Price: "+price+"<br />Qty: "+qty+"<br />Description: "+description)
    });
};
window.onresize = function () {
    if ($("#blanket").length > 0) {
        b.css("width", window.innerWidth);
        b.css("height", window.innerHeight);
   }
};
});
</script>
</head><body>

<div id="body"><div id="wrapper">

<a href="#" onclick="onClickRegister()" id="1"><img src="images/plug1_s.jpg" width="200" height="200" /></a>
<a href="#" onclick="onClickRegister()" id="2"><img src="images/plug2_s.jpg" width="200" height="200" /></a>
<a href="#" onclick="onClickRegister()" id="3"><img src="images/plug3_s.jpg" width="200" height="200" /></a>
<a href="#" onclick="onClickRegister()" id="4"><img src="images/plug4_s.jpg" width="200" height="200" /></a>
<a href="#" onclick="onClickRegister()" id="5"><img src="images/plug5_s.jpg" width="200" height="200" /></a>
<a href="#" onclick="onClickRegister()" id="6"><img src="images/plug6_s.jpg" width="200" height="200" /></a>

<div id="blanket"><div id="openImg">

</div></div>

<script type="text/javascript">
function onClickClose(){
    $("#blanket").hide();
}
</script></div></div>
</body></html>

PHP:

// retval: 0 - login ok, 1 - login failed, 2 - internal error
$json = array("retval" => 2, "data" => NULL, "debug" => "");

$id=json_decode($_REQUEST['id']);


$sql="SELECT * FROM prodTB WHERE prodId=" . $id;

$json['debug'] .= "SQL query was: ".$sql."\n";
$result=mysql_query($sql);
if (!$result) {
    $json['debug'] .= "SQL query failed\n";
    $json['debug'] .= "Other output: ". ob_get_contents();
    ob_end_clean();
    die(json_encode($json));
}
$count=mysql_num_rows($result);

if($count==1){
    $json['retval'] = 0;
    $json['data'] = mysql_fetch_assoc($result);
} else {
    $json['retval'] = 1;
}
$json['debug'] .= "Other output: ". ob_get_contents();
ob_end_clean();
echo json_encode($json);

数据库表结构:

CREATE TABLE `prodTB` (
`prodId` int(11) NOT NULL,
`prodImg` varchar(80) COLLATE utf8_unicode_ci NOT NULL,
`prodName` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`prodPrice` decimal(10,2) NOT NULL,
`prodDesc` longtext COLLATE utf8_unicode_ci NOT NULL,
`prodQty` int(11) NOT NULL,
PRIMARY KEY (`prodId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

1 个答案:

答案 0 :(得分:1)

由于您正在使用jQuery - 您可以摆脱内联onClicks并在dom ready上绑定事件处理程序。

$(document).ready(function() {
    var b = $("#blanket");
    var f = $("#openImg");
    $('#wrapper a').click(function() { 
        var id = $(this).attr('id');// this will get your ID now

        b.css("display", "block");
        b.css("position", "fixed");
        b.css("width", window.innerWidth);
        b.css("height", window.innerHeight);
        b.css("background-color", "rgba(0, 0, 0, 0.7)");

        f.css("position", "fixed");
        f.css("top", "10%");
        f.css("left", "10%");

在声明变量时也使用var,或者它们是全局变量。您还需要在单击绑定之前移动var f和var b,因为您在其他函数中使用它们