Basic Post Question

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
caz3000
Posts: 4
Joined: Sun Jun 26, 2011 9:23 am

Basic Post Question

Post by caz3000 » Mon Jun 27, 2011 3:11 am

Hi, I'm pretty new to Livecode and am just sussing out the trial product.

I am trying to make an app that will go to a webpage (eg. Google), enter data into a field on that webpage, press the button next to that field so that the webpage processes the data. I am trying to work out how to get the data into the field on the webpage.

I figure I need to use the Post command. What I'm confused about is how to specify both the webpage and the field in the command. The examples in the Dictionary only give you one or the other:

Code: Select all

post myData to URL "URLhttp"
post field "Return Values" to URL field "Current Page
"

I don't want to actually launch the webpage in the app, I just want it to access the data from the page in the background.

Sorry if this is a basic, silly question, but I can't seem to find the answer anywhere.

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: Basic Post Question

Post by SparkOut » Mon Jun 27, 2011 9:30 am

You need to get the data to post, and then post that data to the url. Sounds glib I know, I'm not trying to patronise. Forget treating it like the form on the web page, all that does is put a GUI in the browser and submits the data to the processing page as defined by the form. So you can look at the source code for the form on the webpage and see what "action" and "method" are defined, and check the fields it is submitting to the form.

You might, for instance have a form with action "http://www.mydomain.com/formprocessor.php" or, more likely a relative path to the current page, such as "formprocessor.php". So you know you would have to post your data to "http://www.mydomain.com/formprocessor.php"

The data to be posted will be in the form of name=value pairs. You might find the form fields "username", "rating", "comment" (or anything at all that the form is supposed to be processing). In your livecode app you would need to be collecting whatever values your web target requires. You would then create a string of data to post and use that to submit to the url. The libURLFormData function will do this for you. You will also need to consider encoding any special characters (even spaces) in the string. So something like:

Code: Select all

on mouseUp
  put urlEncode(field "username") into tUsername
  put urlEncode(field "rating") into tRating
  put urlEncode(field "comment") into tComment
  put libURLFormData("username", tUsername, "rating", tRating, "comment", tComment) into tDataToBePosted
  post tDataToBePosted to url (field "urlOfWebForm")
  put it into field "displayResult"
end mouseUp
You need not put every individual field into a variable of its own before assembling, but it makes the actual libURLFormData function easier to read above.

If you need to preserve a session (for instance, if the form is asking for username and id to be able to log in) then you will also have to check headers received with the libURLLastRHHeaders() and parse out any cookies set, and set the httpHeaders with the cookie value so that the server continues to accept the future connections as having authenticated.

Post Reply

Return to “Internet”