我需要将图片ID传递给mysql查询。
为此,我完成了这些编码。但它导致未定义索引x:我引用了许多stackoverflow答案。但没有任何效果。
echo "<img src=http://localhost/ss/img/".$p['path']." id=".$p['album_id']." class='img-responsive' alt='' data-toggle='modal' data-target='.bs-example-modal-lg1'>";
if(isset($_GET['x'])) { echo $_GET['x']'; }
<script>
$(document).ready(function() {
$(".g2 img").click(function() {
var albumID = $(this).attr('id');
alert(albumID); //working fine, getting id
$.ajax({
type: 'GET',
url: 'index.php',
//data: x: "albumID",
data: x: albumID,
success: function(data)
{
alert("success!");
}
});
});
});
</script>
我在做什么错?请帮帮我。
答案 0 :(得分:5)
if(isset($_GET['x'])) { echo $_GET['x']'; }
替换为
if(isset($_GET['x'])) { echo $_GET['x']; }
额外'在那里
如果问题仍然存在,请检查您的网址应该是这样的
index.php?x=sometext
答案 1 :(得分:0)
不要像使用它那样只是进行ajax调用并将数据放在你想要的任何div中
使用document.getElementById("yourDiv").innerHTML=data;
尝试
$('body').on('change','.g2 img',function() { //works for ajax loaded contents
var albumID = $(this).attr('id');
var formid = new FormData();
formid.append('albumID ',albumID );
$.ajax({
url : 'index.php',
dataType : 'text',
cache : false,
contentType : false,
processData : false,
data : formid,
type : 'post',
success : function(data){
alert(result);
//document.getElementById("div1").innerHTML=data;
}
});
}
只需尝试使用
url: 'index.php?x='+albumID,
将您提到的href更改为
<a href="/test.php?abid=<?php echo $id;?>">
这应该工作
动态sql查询??
使用类似$str = "insert into table values('".value1."', '".value2."', '".value3."')"
答案 2 :(得分:0)
HTTP://本地主机/ SS / IMG /&#34; $ P [&#39;路径&#39;]&#34; ID =&#34; $ P [&#39; album_id&#39;]&#34。类=&#39; IMG响应&#39; ALT =&#39;&#39;数据肘节=&#39;模态&#39;数据目标=&#39; .BS-例如模态-LG1&#39;&GT;&#34 ;;
if(isset($_GET['x'])) {
echo $_GET['x'];
}
?>
<script>
$(document).ready(function() {
$(".g2").click(function() {
var albumID = $('.img-responsive').attr('id');
alert(albumID); //working fine, getting id
$.ajax({
type: 'GET',
url: 'index.php',
//data: x: "albumID",
data: {x: albumID},
success: function(data)
{
alert("success!");
}
});
});
});
</script>