I need to create a dynamically generated window which shows an image and various elements which are taken from a database. The pop-up window works, but it’s not getting the “id” which I am trying to get using an onclick function.
Using the console I get this message: XMLHttpRequest cannot load getProduct.php?id=undefined. Origin null is not allowed by Access-Control-Allow-Origin. I’m not sure what to do define the “id” further.
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);
Database Table Structure:
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;
Since you are using jQuery – you can get rid of the inline onClicks and bind the event handler at dom ready.
Also use var’s when declaring your variables or they’ll be global. You also need to move the var f and var b before the click binding since you are using them in your other function