<a href="#"><img src="images/jbl.png" id="idhims"/></a>
<a href="#"><img src="images/swastik.png" id="idhims2" name="img2name"/></a>
当点击上面任何一张图片时,我想通过使用点击图片的ID从另一个mysql
中的div
数据库中检索数据。
我想在SELECT
中使用点击图像的ID - 查询从mysql数据库中检索数据。
答案 0 :(得分:2)
使用jQuery
将此包含在
中<script src="http://code.jquery.com/jquery-latest.js"></script>
和
$(document).ready(function(){
$('a').click(function(){
var imgId = $(this).children('img').attr('id');// store the id to varialbe imgId
//alert(imgId);
$.ajax({
type: "POST",
url: "process_form.php",
data: {imageid:imgId},
dataType: "json",
success: function(data) {
//var obj = jQuery.parseJSON(data); if the dataType is not specified as json uncomment this
$("#message_ajax").html(data.frmdb);// message_ajax is the id of the container to show the returned value
}
});
});
})
您可以访问php页面中的值$_POST['imageid']
示例php页面,它将返回点击回我们用户的图像的值
$return_arr = array();
$return_arr['frmdb'] = $_POST['imageid'];
echo json_encode($return_arr);
答案 1 :(得分:1)
添加函数onclick =“yourFunction(this.id)”,例如:
<script>
function yourFunction(id) {
alert(id);
}
</script>
答案 2 :(得分:1)
对于您的查询,我在下面的
中查看了jsfiddleHTML
<img src="http://google.com/img" id="id"/>
<img src="http://google.com/img" id="id1"/>
<img src="http://google.com/img" id="id2"/>
的js
$("img").click(function() {
alert(this.id);
});
答案 3 :(得分:0)
将操作放入控制器以从DB检索数据。像
这样的东西public function actionQuery($id)
{
$result=(your query using $id);
echo CJSON::encode(array('data'=>$result));
}
在使用jQuery的页面上
<a href="#"><img src="1" id="idhims" onclick="query(this)"/></a>
<script type="text/javascript">
function query(obj) {
$.get("your_controller/query", { id: $(obj).attr('id') })
.done(function(data) {
use the data to fill your div;
});
}
</script>