Reputation: 1140
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.
Upvotes: 0
Views: 3627
Reputation: 6884
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");
Upvotes: 1
Reputation: 6908
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.
Upvotes: 3