0

Having a hard time parsing XML created from a web-form using POST.

Here's the scenario:

1) User comes to a web-page, enters their name into a text-field, and clicks SUBMIT

2) This calls a PHP file (called "makeXML.php") which generates an XML file containing that user's name in a tag called "currentUserName"

3) an iPhone App then loads this "makeXML.php" file (using 'loadXMLByURL') and parses it, looking specifically to output the contents of the "currentUserName" tag into a UILabel object.

Should be pretty simple - but for some reason, the contents of the "currentUserName" tag are coming up empty in the App - though they show up perfectly well in the generated XML code in the browser.

What's even stranger, is that if I instead hard-code a value to "uName" in the PHP file ("makeXML.php") - as opposed to getting that value from the FORM (using $_POST["userName"];) - it all works perfectly well. I'm able to grab the value from the "currentUserName" tag and output it to the UILabel object.

NSXMLParser seems to just not like POST'ed values for some reason.

Any ideas?

Here's the code:

portal.html:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Web-Form</title>
</head>

<body>

<form name="form1" method="post" action="makeXML.php">
  <p>Enter your name:</p>
  <p>
    <input name="userName" type="text" />
  </p>
  <p>
    <input type="submit" value="SUBMIT" />
  </p>
</form>

</body>
</html>

Here is "makeXML.php":

<?php
header("Content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";

echo "<document>";

$uName = $_POST["userName"]; 

echo "Here is the name you typed:";
echo "<br/>";
echo "<theUsersName>$uName</theUsersName>";

echo "</document>";

?>

Here is the outputted code from makeXML.php: (Note that this code comes out all on one line - is that how its supposed to be? Shouldn't the "br" tag be working and forcing a line break there?)

<?xml version="1.0" encoding="UTF-8" ?><document>Here is the name you typed:<br/><theUsersName>johnson</theUsersName></document>

Again, note that if I replace:

    $uName = $_POST["userName"]; 

with plain-old:

    $uName = "John"; 

Everything works perfectly and the name "John" appears correctly in my UILabel...

sirab333
  • 3,662
  • 8
  • 41
  • 54
  • 1
    So am I to understand that, in the course of your scenario, there are *two* requests to makeXML.php? One from a webpage, one from an iPhone? – QED Apr 15 '12 at 04:11
  • Oh yeah, I guess so - and that's obviously the problem, isn't it? :-) Cause when the iPhone app is calling for "makeXML.php" its re-processing that page - but without providing a value for "userName" - which returns a blank. So, how do I solve this? Do I have to use a database to persist the data? Meaning, the "userName" entered from the web-page is stored in a database and then the iPhone app calls for a different PHP script that reads the database and retrieves "userName" from it? Seems like a lot of work for something fairly simple. Is there no other way? – sirab333 Apr 15 '12 at 10:27
  • 1
    It depends on what you're building. The problem is that you don't really even know what you're asking about - you thought it was an XML parser problem, but the problem isn't with XML parsing, or even PHP or anything else - your problem is understanding how the system as a whole fits together and works. You need to do some reading. – QED Apr 15 '12 at 11:26
  • goodness, I'm almost offended!:-) My friend, I may be _somewhat_ new to working with PHP, Parsing, etc., but lets not go as far as saying "you don't know what you're talking about":-) For starters, its hardly a _constructive_ comment now, is it?:-) You missed that if I was truly clueless I wouldn't have instantly realized what the problem was upon reading your first comment (in which you merely posted a question) and suggested the solution I did - storing and retrieving items in a database - which I've since done by the way, and it works _perfectly..._ No matter, thanks anyway :-) – sirab333 Apr 18 '12 at 11:34
  • Glad you made your code work, and that NJones could help you out. SO is a strange universe. To an extent, you're expected to know everything about your problem, except one little piece, before presenting it here. Also, I've found that it's difficult to ask more abstract or system-level questions in a way that won't be rejected by the community (geeks are like that). In this case, no offense intended, you demonstrated a misunderstanding of system-level considerations, and left your end goal vague. The community wasn't happy about it. I'm running out of letters now... good luck! – QED Apr 18 '12 at 11:58

1 Answers1

0

As psoft alluded to in his comments, it is hard to concretely answer a question such as this since your end goal seems somewhat unclear. I will assume that you are just starting to experiment with all of the involved technologies.

Your PHP script will work, I have tested it on my local Apache server. Although there are some oddities; Such as inserting an html <br/> into XML. XML is a data format(At least in this context), not really intended for presentation. Your html file also works fine.

There are three super-obvious ways to go from where you are.

1) Do what your above comment says. Use a script to write to a database then use another to access it. The first from a webpage the second from objective-c code. I'm fairly sure this is not something you want to try right now.

2) Interrupt the sending of the post from a UIWebView in your application. Send that data to your server by other means and parse the results.

3) Ignore the web-view and just send the arguments to the server directly.

Both 2 & 3 are very related. The main difference is the origin of the NSURLRequest.

For 2, obtaining the post request is pretty easy. First you would set some object, let's say your view controller, as the web-view's delegate. Then implement the following method.

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    // Very simple example of checking for the php script in the url
    if ([request.URL.absoluteString.lastPathComponent isEqualToString:@"makeXML.php"]){
        // Use the "request" parameter to
        // post the request yourself
        return NO; // Because you will handle it.
    } else {
        return YES;
    }
}

For 3, You simply create the NSURLRequest yourself and configure it for your needs. This requires the NSMutableURLRequest subclass.

NSString *nameToPost = @"Mr Ducky";

NSURL *url = [[NSURL alloc] initWithString:@"http://Address&FolderOfScriptHere/makeXML.php"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"POST";
[request setHTTPBody:[[NSString stringWithFormat:@"userName=%@",nameToPost] dataUsingEncoding:NSUTF8StringEncoding]];

And then in either case (2 or 3) you can use request to load the data to be parsed. Here is, for example, a very bad (synchronous) way to load the data.

NSData *xmlData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

This data can then be fed to a parser or for testing simply be logged:

NSString *xmlString = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding];
NSLog(@"xml:%@",xmlString);
NJones
  • 27,139
  • 8
  • 70
  • 88
  • Thanks NJones :-) I went with the very solution I suggested myself (and which you mirrored in your 3 options as option #1) - that of posting the submitted data to a mySQL database. Works very nicely. Thank you for your suggestions - I'm fairly certain they will come in handy for other parts of this project. Cheers! – sirab333 Apr 18 '12 at 11:43