1

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);
arcaderob
  • 481
  • 1
  • 7
  • 21
  • 4
    Are you initializing the php with – mcphersonjr Oct 07 '14 at 16:36
  • 2
    Are you testing on a web-server that has php installed or are you testing on a file on the local file system like `file:///path/to/file.php`? The last one will have this effect and if you have a web-server and php installed on your local system, you should use something like `http://localhost/path/to/file.php` instead. – jeroen Oct 07 '14 at 16:38
  • I am currently using a WAMP server that has the ability to execute the PHP. I have made similar requests that work in the past but for some reason I cannot get this to work. – arcaderob Oct 07 '14 at 16:40
  • 3
    *`I am using tags`* - Are short tags enabled? If not, do or change `` to ` – Funk Forty Niner Oct 07 '14 at 16:41
  • @Fred-ii-That solved it! Thanks! I feel like an idiot :/ – arcaderob Oct 07 '14 at 16:43
  • 1
    @arcadeRob Cool, glad it worked out. `Another happy ending`. – Funk Forty Niner Oct 07 '14 at 16:44
  • @arcadeRob If you want the question to be closed, I can make it an answer, or you can just delete the question. Either way, the choice is yours Rob. What matters is that the problem was solved, *cheers*. – Funk Forty Niner Oct 07 '14 at 16:48
  • 2
    @Fred-ii-Answer the question and I'll mark it as answered. It may help somebody in the future. :) – arcaderob Oct 07 '14 at 16:56
  • @arcadeRob It has been done, plus I've added some extra information to my answer to give it a bit of **meat** to it, *cheers* – Funk Forty Niner Oct 07 '14 at 17:03

1 Answers1

1

"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:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141