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'];
}
?>
3
  • 1
    First identification, you mentioned datatype as "json", but your ajax response is not json. Commented May 30, 2017 at 1:48
  • I tried changing this to "text", but the result was the same. Commented May 30, 2017 at 1:48
  • 1
    try to direct run get.php in browser with query string. Ex, http://...../../get.php?ajaxid=sample_value Commented May 30, 2017 at 1:51

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. Running the page isolated gave a syntax error! Good tip. Fixing and then checking your code..
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given
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);
Yes, I established connection within the file and that seemed to fix it.
Cheers!! :) (Y)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.