1

Related Thread: jQuery Ajax returns the whole page

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:

<?php

//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'];
}
?>
rpivovar
  • 3,150
  • 13
  • 41
  • 79

1 Answers1

2

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

<?php

//  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.

Naga
  • 2,190
  • 3
  • 16
  • 21
  • 1
    Thank you. Running the page isolated gave a syntax error! Good tip. Fixing and then checking your code.. – rpivovar May 30 '17 at 02:15
  • Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given – rpivovar May 30 '17 at 02:17
  • 1
    Also you are missing mysql connection in get.php file. First make `$conn`(DB connection) variable available in get.php and pass it into `mysqli_real_escape_string()` funtion. Ex, $conn->real_escape_string($value); OR mysqli_real_escape_string($conn, $value); – Naga May 30 '17 at 02:25
  • Yes, I established connection within the file and that seemed to fix it. – rpivovar May 30 '17 at 02:25
  • 1
    Cheers!! :) (Y) – Naga May 30 '17 at 02:26