0

This is my first time using ajax which i have being learning from the new Boston website. but i am getting the following error

error on line 8 at column 6: XML declaration allowed only at the start of the document

I tried removing white spaces which usually seems to be the fix but that didn't work. Any suggestions would be great.

<?php
 header('Content-Type: text/xml');
 echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
 echo '<response>';
  $staus;
  $email_in_use = $_GET['email'];
  $query = mysqli_query($link, "SELECT * FROM emails WHERE email='".$email_in_use."'");
  if(mysqli_num_rows($query) > 0){

      $staus = false;
      return $status;

  }else{
      // do something
      if (!mysqli_query($con,$query))
      {
          die('Error: ' . mysqli_error($con));
      }else{
       $staus = true;
       return $status;
      }
  }
 echo '</response>';
?>
fusion3k
  • 11,568
  • 4
  • 25
  • 47
Amy Hyland
  • 61
  • 1
  • 7
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Mar 13 '16 at 13:29
  • 1
    The code you have there will put the XML declaration on the first line. Whatever the problem is, we can't tell from looking at the code you've shared with us so far. – Quentin Mar 13 '16 at 13:30
  • The first thing you should do with debugging this is to look at the output of the script. Open the Developer Tools in your browser. Open the Network tab. Trigger the Ajax request. Examine the response. – Quentin Mar 13 '16 at 13:31
  • @Quentin thanks for your response What is best practice for defense . when I go the network it shows noting . i am including this php script at the top of my index page. So i cant access any page. just goes straight to error. The console says Uncaught TypeError: Cannot read property 'cssFloat' of undefined – Amy Hyland Mar 13 '16 at 13:37

2 Answers2

0

There are many problems in your script.

First of all, $staus is not $status: uniform variable names.

But the core problem is that your script will never output a corrext XML file:

if (!mysqli_query($con,$query))
{
    die('Error: ' . mysqli_error($con));
}else{
    $staus = true;
    return $status;
}

Either die() and return stop script execution, so closing </response> tag is not printed.

BTW, your error has nothing to do with code in example, unless you have blanks lines or something other before <?php declaration.

It's not a good idea output XML in your way, it's better separate execution code and output. Something like this:

$email_in_use = $_GET['email'];
(...)
if( !mysqli_query( $con, $query ) )
{ $status = mysqli_error( $con ); }
else
{ $status = 'OK'; }

header( 'Content-Type: text/xml' );
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
echo '<status>'.$status.'</status>';
echo '</response>';

End note: to guarantee correct XML, I suggest you to use a XML Parser. With SimpleXML, i.e., you can write in this way:

$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" standalone="yes" ?><response><status/></response>');
$xml->response->status = $status;
echo $xml->asXML();
fusion3k
  • 11,568
  • 4
  • 25
  • 47
  • Thanks. Ya i would say i have lots of errors, Im just trying to learn still I only started learning php about a week ago. But If I delete every thing inside and just leave the response tagss it still does not work – Amy Hyland Mar 13 '16 at 13:43
  • First of all, test your script by direct retrieving it in browser, not through ajax. By this way, you can see exact output and isolate the problem. When you are sure that the script is running, call it through ajax. – fusion3k Mar 13 '16 at 13:51
  • Thanks very much for educating me on this, i will implement it now. Can you to me $xml->response->status = $status; echo $xml->asXML(); – Amy Hyland Mar 13 '16 at 17:45
  • And is the best way to catch this like this: xmlResponse = xmlHttp.responseXML; xmlDocumentElement = xmlResponse.documentElement; message = xmlDocumentElement.firstChild.data; – Amy Hyland Mar 13 '16 at 17:46
  • I want to get the status varible in the js file – Amy Hyland Mar 13 '16 at 18:33
  • I'm not a javascript guru, I'm very sorry. I'm work on server side... :( I suggest you to try and — if you have some issue — ask for a new question as javascript tag. I'm very sorry. – fusion3k Mar 13 '16 at 18:38
-1

echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';

This should be your first line for the page. XML expects this to be the first line; no exceptions.

PS. If you remove the header('Content-Type: text/xml'); it will work.

I think what happens here is this line interferes with the opening xml.

Beni
  • 149
  • 2
  • 13