Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving XML - very stuck

Status
Not open for further replies.

Trebor100

Programmer
Joined
Mar 14, 2006
Messages
90
Location
GB
Hi all,

Am trying to write my first web application that utilises XML rather than writing directly to a database. I can load the xml fine by using the xmlhttprequest which does the job however I haven’t been able to figure out how to save the XML back to the server.

I don’t mind if this is performed server side or client side however it needs to be achieved without any screen refresh. Have tried the approach shown in W3School but this requires another page to be opened which I don’t want to do.

If anyone can help me out here it would be much appreciated as I’m banging my head against the wall and it’s starting to hurt!

Cheers all.
Rob
 
i may be missing the point here...

isn't it exactly the same as loading? except i guess you want to construct the HTTP to POST the xml data rather than just append it to the uri (and to set the content type to "application/x-
you will need some serverside technology to handle the incoming data, of course.

for what it's worth, i use sajax (modernmethod.com) for all the little ajax projects i'm working on. there are a few limitations (notably in the richness of the return value) but these are surmountable and it is extremely easy to use.
 
Yer it's the server side that i currently dont get. the xmlhttprequest code im using is:

var objHTTP, strResult;
objHTTP = new ActiveXObject('Microsoft.XMLHTTP');
objHTTP.Open('POST',"OtherPage.asp",false);
objHTTP.setRequestHeader('Content-Type',
'application/x-
objHTTP.send("id=4;title='teach ya self xml';url='
strResult=objHTTP.responseText;

Again any tips or examples would be much appriciated.
Cheers
Rob
 
can't see anything terribly wrong.

should you not be escaping the post data first?

Code:
var pd = "id=4;title='teach ya self xml';url='[URL unfurl="true"]www.wible.com'";[/URL]
pd = escape(pd);
objHTTP.send(pd);

i'm not an ASP programmer i'm afraid. in php the variables would be accessible by the receiving page in the $_POST superglobal (as $_POST['id'] etc). I'm sure ASP has something similar.

in php you'd do something like
Code:
<?
if (isset (!$_POST['id']) || !isset($_POST['title']) || !isset($_POST['url']) {
 return "You damn fool, you've got it wrong";
}
$fh = @fopen("somexmlfile.xml", "abt");
if ($fh === false) return "can't open the file for writing";
@flock($fh, LOCK_EX); //suppress the error because some file systems don't like flock
fwrite($fh, "<id>");
fwrite($fh, $_POST['id']);
fwrite($fh, "</id>");
...
fclose($fh); //also releases lock
return "all is groovy";

?>
 
Cheers for the pointers jpadie. This is where I am currently up to:

My htm page is as follows:

<html>
<head>
<script type="text/javascript">
function storesomething()
{
var objHTTP, strResult, pd;
objHTTP = new ActiveXObject('Microsoft.XMLHTTP');
objHTTP.Open('POST',"Store.php",false);
objHTTP.setRequestHeader('Content-Type','application/x- pd = "id=4;title='teach ya self xml';url=' pd = escape(pd);
objHTTP.send(pd);
strResult = objHTTP.responseText;
alert(strResult);
}
</script>
</head>
<body>
<form>
<input type="button" onclick="storesomething()" value="write xml">
</form>
</body>
</html>

My php page is as follows:

<?
if (isset (!$_POST['id']) || !isset($_POST['title']) || !isset($_POST['url']) {
return "You damn fool, you've got it wrong";
}
$fh = @fopen("somexmlfile.xml", "abt");
if ($fh === false) return "can't open the file for writing";
@flock($fh, LOCK_EX); //suppress the error because some file systems don't like flock
fwrite($fh, "<id>");
fwrite($fh, $_POST['id']);
fwrite($fh, "</id>");
...
fclose($fh); //also releases lock
return "all is groovy";

?>

When i run this all i get is an alert box with everything from the php file printed.
 
then ... most likely ... you have not got php running on your server!

but if instead you are getting an error message it's because of my sloppy coding. try these:

the form and js (note the changes in the formulation of the postdata: the values are escaped and they are separated with ampersands (sorry i didn't spot that in your previous post)

Code:
<html>
<head>
<script type="text/javascript">
function storesomething()
{
    var objHTTP, strResult, pd;
    objHTTP = new ActiveXObject('Microsoft.XMLHTTP');
    objHTTP.Open('POST',"somepage.php",false);
	objHTTP.setRequestHeader("Method", "POST " + "somepage.php" + " HTTP/1.1");
    objHTTP.setRequestHeader('Content-Type','application/x-[URL unfurl="true"]www-form-urlencoded');[/URL]
    pd = "id=4&title="+escape("'teach ya self xml'")+"&url=[URL unfurl="true"]www.wibble.com";[/URL]
    objHTTP.send(pd);
    strResult = objHTTP.responseText;
    alert(strResult);
}
</script>
</head>
<body>
<form>
<input type="button" onclick="storesomething()" value="write xml">
</form>    
</body>
</html>

and the php file (i'd "returned" the messages rather than echo'd them as i usually have my ajax calls going back to the central despatch page and then work out which function to call from the contents of the post data).

Code:
<?
echo processAJAXInput();
function processAJAXInput() {

if (!isset($_POST['id']) || !isset($_POST['title']) || !isset($_POST['url'])) {
 return "You damn fool, you've got it wrong.  The POST data includes " .print_r($_POST, true);
}
$fh = @fopen("somexmlfile.xml", "abt");
if ($fh === false) 
	{ return "can't open the file for writing"; }
@flock($fh, LOCK_EX); //suppress the error because some file systems don't like flock
fwrite($fh, "<id>");
fwrite($fh, $_POST['id']);
fwrite($fh, "</id>");
fclose($fh); //also releases lock
return "all is groovy";
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top