0

I see various posts here on similar topics. Unfortunately these have not helped.

I make an ajax call to a php file which creates an XML file from which I populate appropriate form fields assuming data is found.

If I specify the return data to be XML as in the following I do not get a success and the alert "Could not retrieve XML file" is displayed.

function check_Address(){

alert("username=" + $("#username").attr("value"));

$.ajax({
    type: "POST",
    url: "admin_Check_Address.php",
    data: "username=" + $("#username").attr("value"),
    dataType: "xml",                
    success: function(return_Data){
        set_Address(return_Data);
    },
    error: function(return_Data){
        alert("Could not retrieve XML file.");
    }
});  // end ajax call
}

However if I remove the datatype I get success function set_Address is actioned and the tagNames in return_Data are found and the field are populated correctly. i.e.

function check_Address(){

alert("username=" + $("#username").attr("value"));

$.ajax({
    type: "POST",
    url: "admin_Check_Address.php",
    data: "username=" + $("#username").attr("value"),
    success: function(return_Data){
        set_Address(return_Data);
    },
    error: function(return_Data){
        alert("Could not retrieve XML file.");
    }
});  // end ajax call
}

Here is the php:

<?php 

/*
 * Accept username from Ajax
 * Get appropriate key
 * Check for pre existing address(es)
 */

if(isset($_POST["username"])){

    // retrieve user key

    $sql  = "SELECT * FROM User ";
    $sql .= "WHERE username = ";
    $sql .= "'";
    $sql .= $_POST['username']; 
    $sql .= "'";
    $sql .= " LIMIT 1;";
    $user = User::find_By_Sql($sql);
    $user_Key = $user[0]->get_User_Key();

    // check for an existing address or addresses

    $sql  = "SELECT * FROM Address ";
    $sql .= "WHERE user_Key = ";
    $sql .= "'";
    $sql .= $user_Key;  
    $sql .= "';";
    $addresses = Address::find_By_Sql($sql);

    // how many addresses found
    // if none nothing to do except set focus on address 1
    // if 1 address returned check active and return xml to complete fields
    // if >1 return xml to create list for user to select from

    echo "addr ct:" . count($addresses);

    if (count($addresses) == 1){
        construct_XML($addresses[0]);
    }
} else {
    echo "error nothing passed";
}


function construct_XML($address){

    echo ("start xml");

    $xml_String  = "<?xml vesrion=\"1.0\"?>";
    $xml_String .= "<addresses>";
    $xml_String .= "<address>";
    $xml_String .= "<username>";
    $xml_String .= $_POST['username'];
    $xml_String .= "</username>";
    $xml_String .= "<address1>";
    $xml_String .= $address->address_1;
    $xml_String .= "</address1>";
    $xml_String .= "<address2>";
    $xml_String .= $address->address_2;
    $xml_String .= "</address2>";
    $xml_String .= "<city>";
    $xml_String .= $address->city;
    $xml_String .= "</city>";
    $xml_String .= "<county>";
    $xml_String .= $address->county;
    $xml_String .= "</county>";
    $xml_String .= "<country>";
    $xml_String .= $address->country;
    $xml_String .= "</country>";
    $xml_String .= "<post_Code>";
    $xml_String .= $address->post_Code;
    $xml_String .= "</post_Code>";
    $xml_String .= "</address>";
    $xml_String .= "</addresses>";

    echo $xml_String;

}

?>

The set address function which reads the XML is:

function set_Address(return_Data){

$("#address_1").attr("value", $("address1", return_Data).text());
$("#address_2").attr("value", $("address2", return_Data).text());
$("#city").attr("value", $("city", return_Data).text());
$("#county").attr("value", $("county", return_Data).text());
$("#country").attr("value", $("country", return_Data).text());          
$("#code").attr("value", $("post_Code", return_Data).text());
}

I am thinking that I should be returning a file.xml rather than echoing the string but I am not sure how to do this.

Any ideas where I am going wrong? I could simply omit the dataType and it all works fine but I would like to know why it won't recognise this as XML so I understand for next time.

Many thanks.

Ok many thanks for the responses. Yes I had left two mistakes in the original post which echoed out 2 unwanted lines.

I now understand that I needed to include the header prior to the string in order to get ajax to receieve this back as XML.

header ("Content-Type:text/xml");

However I still could not get Jquery to correctly parse the data returned from the PHP have encountered :

xml parsing error no element found location moz-nullprincipal

I have been unable to resolve this. Therefore I have updated the ajax so that I nest one call within another to get the php to return XML. I am not sure is this is appropriate but it works:.....

Here is the php

<?php require_once("../../_includes/initialize.php"); ?>


<?php 

/*
 * Accept username from Ajax
 * Get appropriate key
 * Check for pre existing address(es)
 */

$tmpuser = "[email protected]";

if(isset($_POST["username"])){

    // retrieve user key

    $sql  = "SELECT * FROM User ";
    $sql .= "WHERE username = ";
    $sql .= "'";
    $sql .= $_POST['username']; 
    $sql .= "'";
    $sql .= " LIMIT 1;";
    $user = User::find_By_Sql($sql);
    $user_Key = $user[0]->get_User_Key();

    // check for an existing address or addresses

    $sql  = "SELECT * FROM Address ";
    $sql .= "WHERE user_Key = ";
    $sql .= "'";
    $sql .= $user_Key;  
    $sql .= "';";
    $addresses = Address::find_By_Sql($sql);

    // how many addresses found
    // if none nothing to do except set focus on address 1
    // if 1 address returned check active and return xml to complete fields
    // if >1 return xml to create list for user to select from

    if (count($addresses) == 1){
        construct_XML($addresses[0]);
    }
} else {
    //"error nothing passed";
}


function construct_XML($address){

// initialize xml object

$xml_Data = simplexml_load_string("<?xml version='1.0'?>\n<addresses></addresses>");

$address_Detail = $xml_Data->addChild("address");
$address_Detail->addChild("username", $_POST['username']);
$address_Detail->addChild("address1", $address->address_1);
$address_Detail->addChild("address2", $address->address_2);
$address_Detail->addChild("city", $address->city);
$address_Detail->addChild("county", $address->county);
$address_Detail->addChild("country", $address->country);
$address_Detail->addChild("post_Code", $address->post_Code);

$fp = fopen("addresses.xml","wb");
fwrite($fp,$xml_Data->asXML());
fclose($fp);

}

?>

Here is the amended Ajax call:

function check_Address(){

$.ajax({
    type: "POST",
    url: "admin_Check_Address.php",
    data: "username=" + $("#username").attr("value"),
    success: function(return_Data){
            $.ajax({
            type: "POST",
            url: "addresses.xml",
            dataType: "xml",
            success: function(xml_Data){
                set_Address(xml_Data);
            },
            error: function(){
                alert("Could not retrieve XML file.");
            }
        });  // end innerajax call  
    },
    error: function(){
        alert("Could not retrieve XML file.");
    }
});  // end ajax call   
}

The adjust set_Address function

function set_Address(xml_Data){

$("#address_1").attr("value", $("address1", xml_Data).text());
$("#address_2").attr("value", $("address2", xml_Data).text());
$("#city").attr("value", $("city", xml_Data).text());
$("#county").attr("value", $("county", xml_Data).text());
$("#country").attr("value", $("country", xml_Data).text());         
$("#code").attr("value", $("post_Code", xml_Data).text());

}

So not quite 100% there I would really like to be able to make the ajax call in one hit. So any further advises very welcome.

2
  • 1
    First: You really need to escape the values you're outputting in the XML-structure, as well as escaping the values you use in your MySQL-query. Second: You probably want to set the content-type to XML, and remove the "start XML" bit that you preface the XML with. Commented Jul 16, 2012 at 9:27
  • oops yes the "start XML" shouldn't be there. Commented Jul 16, 2012 at 9:30

2 Answers 2

3

You need to set the appropriate header in php.

header ("Content-Type:text/xml");

Also I'm quit sure that the echo ("start xml"); before you start outputting the xml could be troublesome.

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

5 Comments

Ok yes got that I have added the header just above the echo the ajax is now accepting that as an xml file. Now the set_Address is unhappy
Wihtout seeing what set_Adress is doing it is impossible to help.
Sorry @yan.kun I have have put the set_Address function in the post, I was making a statement rather than asking a question. I have removed the "start XML" and "addr ct" which were both accidently left in. OK looking at fire bug I see the response is exactly as I except now. But XML reports XML Parsing Error: no element found Location: moz-nullprincipal:{8a178f17-a757-461f-b5a6-90fbb85bcca7} Line Number 1, Column 1: ... I am now looking at this error.
I suspect that this is related to the escaping referred to by @fiskfisk
and Nealv yes thanks for setting me in the right direction. You will see from my edit to the original post that I can not get the xml file back from the php but only by writing it out to a file and nesting the ajax calls so that that file comes back in. Obviously this shouldn't be necessary. No matter where I look on the net I have yer to find a definitive working example of this. So I am going to settle for my 2 stage approach for now as it works.
1

Probabely, the return response from the server has a header different from XML (you can check in firebug).

So what you actually send is xml that has an html markup (because the header says so).

If you say explicitally that the the returned response is XML in the ajax call, it won't be able to parse it.

If you set nothing, it will be able to read the HTML but like DOM elements.

If you set the header right, it wil propabely be fixed

header ("Content-Type:text/xml"); 

5 Comments

Ok yes got that I have added the header just above the echo the ajax is now accepting that as an xml file. Now the set_Address is unhappy.
Yan and @Nealv yes thanks for setting me in the right direction. You will see from my edit to the original post that I can not get the xml file back from the php but only by writing it out to a file and nesting the ajax calls so that that file comes back in. Obviously this shouldn't be necessary. No matter where I look on the net I have yer to find a definitive working example of this. So I am going to settle for my 2 stage approach for now as it works.
it appears I cannot accept both answers. So thanks you for you input I have accepted Yan's answer as he replied first.
Well no you can't update both. The answer has toggled away from Yan and won't go back. Can't waste time on that. It is as it is. But annoying.
Lol, it's ok don't worry, as long as you are helped I am fine

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.