bd96500e110b49cbb3cd949968f18be7.png

The above thread is related, and this seems to be a somewhat common problem, but the answer in that thread doesn't exactly help in my situation.

When an image on my page is clicked, a jQuery function is called, and in that function is an Ajax declaration:

//click an image

$(".img_div").click(function() {

//get integer stored in alt attribute and pass to variable

var altCheck = $(this).find('.modal_img').attr('alt');

//get MySQL data

$.ajax({

//php file to grab data

url: "get.php",

type: "post",

datatype: "json",

//pass above variable to php file

data:{ ajaxid: altCheck },

success: function(response){

//log data to console

console.log(response);

},

error: function(){

console.log("test");

}

});

I'm trying to log the received data into the console purely as a test right now, but I'm getting the entire index.html page logged into the console instead.

A connection has been made to the database and stored in variable $db prior to any image clicks.

Here is my get.php file:

//variable from jQuery

$value = filter_var($_REQUEST["ajaxid"], FILTER_SANITIZE_STRING);

$value = mysqli_real_escape_string($value);

//variable passed to SQL statement

$sql = $conn->prepare("SELECT FROM table WHERE screeningId = ?");

$sql->bind_param("s",$value);

//Get data

$result = mysqli_query($db, $sql);

while ($row = mysqli_fetch_array($result)){

//output data

echo $row['url'];

}

?>

解决方案

First identification, you mentioned datatype as "json", but your ajax response is not json. So Try like this,

// Make DB connection variable($conn) available here

//variable from jQuery

$value = filter_var($_REQUEST["ajaxid"], FILTER_SANITIZE_STRING);

$value = mysqli_real_escape_string($conn, $value);

//variable passed to SQL statement

/*$sql = $conn->prepare("SELECT FROM table WHERE screeningId = ?");

$sql->bind_param("s",$value);*/

$sql = "SELECT * FROM table WHERE screeningId = '$value'";

$result = mysqli_query($db, $sql);

//Get data

$result = mysqli_query($db, $sql);

$temp = array();

while ($row = mysqli_fetch_array($result)){

//output data

array_push($temp,$row['url']);

}

echo json_encode($temp);

?>

For debug purpose, try to direct run get.php in browser with query string. Ex, http://...../../get.php?ajaxid=sample_value.

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐