Passing and Updating Values in Forms

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:

HTML:
  1. <input name="textfield[0]" type="text" id="textfield[0]" />
  2. <input name="textfield[1]" type="text" id="textfield[1]" />

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:

HTML:
  1. <input name="textfield[]" type="text" id="textfield[0]" />
  2. <input name="textfield[]" type="text" id="textfield[1]" />

Take a look at the php's manual for more details.

The variables are always sent to the same page:

PHP:
  1. <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:

PHP:
  1. $_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:

PHP:
  1. <input name="textfield[0]" type="hidden" id="textfield[0]" value="<?php echo $_POST['textfield'][0]?/>" />
  2. <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:

PHP:
  1. $_POST['textfield'][0] //first element from the textfield[] array, inside $_POST

Here's another example to help you understand bidimensional arrays:

PHP:
  1. <?php
  2. // these are two of our rabbits
  3. $rabbit1 = array ("name" => "snowflake", "age" => 1);
  4. $rabbit2 = array ("name" => "ruda", "age" => 3);
  5.  
  6. $my_rabbits = array ("son" => $rabbit1, "mother" => $rabbit2);
  7.  
  8. // what is the mother's name?
  9. echo $my_rabbits['mother']['name']; // ruda
  10. // what is the son's name?
  11. echo $my_rabbits['son']['name']; // snowflake
  12. // snowflake's age
  13. echo $my_rabbits['son']['age']; // 1
  14. ?>

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!

Comments are closed.