Search code examples
phpjqueryhtmljsoncontent-type

Having trouble between PHP and jQuery GET


I am trying to send two variables from an HTML page to a PHP script but the response keeps coming back as text/html. aka, the entire code in the PHP file is being returned to the console.

My jQuery code:

    $.get(                             //call the server
            "biography_query.php",                //At this url
            {
                field: "value",
                id: decodeURIComponent(id),
                name: decodeURIComponent(name)
            }                          //And send this data to it
        ).done(                         //And when it's done
            function(data)
            {    
                console.log(data);
            },"jsonp"
        );

PHP code:

header('Content-Type: application/json');

//start session on server to store the users information
session_start();

// establish connection to SQL database
$con = mysqli_connect("localhost","root","","capstone") or die("Error: " . mysqli_error($con));

$id = $_REQUEST['id'];
$name = $_REQUEST['name'];

// build statement to query database and return all characters
$SQL = "SELECT real_name, alternate_identities, aliases, nicknames, place_of_birth, first_appearance FROM `character` WHERE id='$id' AND superhero_name='$name'";

// execute the statement
$sqlReturn = mysqli_query($con, $SQL);

$row = array();
while($r = mysqli_fetch_assoc($sqlReturn)) {
    $row['real_name'] = $r['real_name'];
    $row['alternate_identities'] = $r['alternate_identities'];
    $row['aliases'] = $r['aliases'];
    $row['nicknames'] = $r['nicknames'];
    $row['place_of_birth'] = $r['place_of_birth'];
    $row['first_appearance'] = $r['first_appearance'];
}
    echo json_encode($row);

Solution

  • "I am using <? tags"

    As per OP's wishes: (to close the question, and for future readers)

    If short open tags are not enabled, you will need to either enable them, or change <? to <?php.

    Here are a few articles on the subject, on Stack:

    On PHP.net: