This is a short and simple tutorial, or just an example if you prefer.
I decided to write it after helping someone at phpBuilder. As I spent some minutes writing that code, here's the place to (re)post it.
I love PHP's logic: using the same document for different things! In this case, the same document takes four different actions. You can:
- fill in a form
- check the values
- edit the values
- send an email... or something else
This is achieved with conditionals that test where you came from, or, more precisely, which button you clicked.
You can see a working and useless example here, and download the code here (delete the .zip extension).
Instead of "usual" names that would originate "usual" php variables, an array is used:
Not for any particular reason... it was requested buy the person I helped.
I suggested using predetermined positions in the array textfield, - textfield[0] and textfield[1] - which may offer you more control. Those positions are assigned automatically when you use textfield[] instead:
Take a look at the php's manual for more details.
The variables are always sent to the same page:
-
<form name="form1" id="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Thanks to echo $_SERVER['PHP_SELF']; that echoes the current url (manual)
n this case the variables are sent via POST (the other option is GET) - so you have to get them from the $_POST array:
-
$_POST['Submit'], $_POST['textfield'][0], ...
This is needed (for security reasons) in more recent versions, if you have register_globals=false. If you were using GET, you'd use the $_GET array instead.
I used hidden fields to store and send the variable's values:
-
<input name="textfield[0]" type="hidden" id="textfield[0]" value="<?php echo $_POST['textfield'][0]?/>" />
-
<input name="textfield[1]" type="hidden" value="<?php echo $_POST['textfield'][1]?/>" />
You can achieve the same using sessions or cookies.
As mentioned before, you have to fetch the variables from the $_POST array and, textfield, is itself an array, i.e., you have a multidimensional array (bidimensional).
That's why you need to use this strange notation:
-
$_POST['textfield'][0] //first element from the textfield[] array, inside $_POST
Here's another example to help you understand bidimensional arrays:
-
<?php
-
// these are two of our rabbits
-
-
-
// what is the mother's name?
-
// what is the son's name?
-
// snowflake's age
-
?>
I use Dreamweaver MX 2004 in my projects as its design view is a great help for HTML, and the code view does the rest with PHP.
With these explanations and the comments in the php file, I hope this is a good starting point!