|
Previous: Making Hyperlinks
Next: Database Access
Forms, Get and PostIt is not necessary in Andromeda to ever put an HTML FORM element into your custom HTML because Andromeda always wraps the main content area in an HTML FORM. You can put a file upload input directly into this form as well with no special features. A Custom Page With A FormBecause Andromeda always wraps the main output in an HTML FORM, you do not ever need to put on into your code. This example is not complete enough to actually work, but it does show that you do not need a form: <?php
# This is file public_items
class public_items extends x_table2 {
function main() {
// This example shows only that you do not
// need an HTML form, there is not enough
// here yet for it to actually work.
?>
Your favorite color: <input name='color'>
<br/>
<input type="submit">Save</input>
<?php
}
}
?>
In order to make this form actually work, we need to tell it what page to post to. If we want to handle the processing inside of public_items.php, we must set the value of gp_page as a hidden variable. We could do this with explicit HTML, but it is better to use the Andromeda PHP function hidden to set the value. Our example is now a little better, it looks like this: <?php
# This is file public_items
class public_items extends x_table2 {
function main() {
// This puts a hidden variable onto the form. When it
// is POSTed, it will return to public_items for processing
hidden('gp_page','public_items');
?>
Your favorite color: <input name='color'>
<br/>
<input type="submit">Save</input>
<?php
}
}
?>
We still have one problem. When the user submits the form, it will come right back to the original page without doing anything. We need to put another hidden variable onto the form, some type of flag that will tell us we are coming in on a Form Post. This skeleton code now has enough for us to get going: <?php
# This is file public_items
class public_items extends x_table2 {
function main() {
// If we are coming in from a FORM POST, this
// property will be set
if(gp('postflag')==1) {
$this->processPost();
return;
}
// Tell it what page to go to, and tell it
// that we are posting a form. You can use
// any name you want for the post flag
hidden('gp_page','public_items');
hidden('postflag','1');
?>
Your favorite color: <input name='color'>
<br/>
<input type="submit">Save</input>
<?php
}
function processPost() {
?>
Your favorite color is <?=gp('color')?>, thanks
for participating!
<?php
}
}
?>
Get and Post ParametersAndromeda considers GET and POST parameters to be the same, and enforces no rules on when you must use on or the other. In fact, Andromeda actually merges them together into one array which you can access with the gp function. Specifically, Andromeda copies the $_POST array into its own array, then it merges this array with the $_GET array, so that $_GET variables override $_POST variables of the same name. Andromeda also un-escapes them if they have been escaped, since they are escaped by different methods later on depending on whether they are going to the browser or the database. The Get/Post overlay can be very useful when you are debugging, because you can simulate an entry form by typing in a url like "&x2t_column_name=value&x2t_another_column=value" and it will be processed the same as if an HTML FORM had been POSTed. Our page on GET-POST Variables contains a complete rundown of all of the ways you can manipulate the query string.
Previous: Making Hyperlinks
Next: Database Access
|
