|
Dealing with FormsOne of the most powerful features of PHP is the way it handles HTML forms. The basic concept that is important to understand is that any form element will automatically be available to your PHP scripts. Please read the manual section on Variables from outside of PHP for more information and examples on using forms with PHP. Here is an example HTML form: Example 2.6. A simple HTML form<form action="action.php" method="post">
There is nothing special about this form. It is a straight HTML form
with no special tags of any kind. When the user fills in this form
and hits the submit button, the Example 2.7. Printing data from our formHi <?php echo htmlspecialchars($_POST['name']); ?>. A sample output of this script may be: Hi Joe. You are 22 years old.
Apart from the htmlspecialchars() and
You can also deal with XForms input in PHP, although you will find yourself comfortable with the well supported HTML forms for quite some time. While working with XForms is not for beginners, you might be interested in them. We also have a short introduction to handling data received from XForms in our features section. Code Examples / Notes » tutorial.formsyasman
[Editor's Note: Since "." is not legal variable name PHP will translate the dot to underscore, i.e. "name.x" will become "name_x"] Be careful, when using and processing forms which contains <input type="image"> tag. Do not use in your scripts this elements attributes `name` and `value`, because MSIE and Opera do not send them to server. Both are sending `name.x` and `name.y` coordiante variables to a server, so better use them. danrulz98
In the sample above, the code dose not work. Even if you copy and paste the sample code you will get: Hi . You are 0 years old The code should look more like this on the main page: <?php echo ' Hello World'; ?> <form action="action.php" methhod="get"> Your name: <input type="text" name="name" /> your age: <input type="text" name="age" /> <input type="submit" /> Then the action page should look like this: Hi <?php echo htmlspecialchars($_GET['name']); ?>. You are <?php echo (int)$_GET['age']; ?> years old maheshs60
Here is a PHP script to add more form fields in a page by clicking on a button on the same page.You can add unlimited number of fields dynamically(in run time) in a page by using this script. (For the alligning of form fields,forms are given in layers) <html> <style type="text/css"> #Layer1 { position:absolute; left:413px; top:123px; width:203px; height:35px; z-index:1; } #Layer2 { position:absolute; left:21px; top:122px; width:389px; height:50px; z-index:2; } </style> <body> <? $n=0; $n1=$_POST['hf']; if($n1>=$n){ $t=$n1;} else{ $t=$n;} /*For example this script allows user to add more and more file upload fields in a page by clicking on a button*/ ?> <!--creating a form--><!--save this page as "upload.php" --> <div id="Layer1"> <form name="form1" method="post" action="upload.php"> <!--creating a hidden field and sets it's value as $t--> <input name="hf" type="hidden" id="hf" value="<? echo ++$t; ?>"> <!--creating a button.When we click this, a new field will be generated--> <input type="submit" name="Submit" value="one more"> </form> <!--another form to submit the uploaded file to be processed--> <!--the upload script should be written in the file "uploader.php"--> </div> <div id="Layer2"> <form action="uploader.php" method="post" enctype="multipart/form-data"> <? $i=0; while($i<$t){ echo "Filename:" ?> <label for="file"> <input type="file" name="file" id="file" > <input type="submit" name="submit" value="Submit" > <?++$i;}?> </form> </div> </body> </html> Like this, you can add any type of form field into the same page by the click of a button. svendk
As Seth mentions, when a user clicks reload or goes back with the browser button, data sent to the server, may be sent again (after a click on the ok button). It might be wise, to let the server handle whatever there is to handle, and then redirect (a redirect is not visible in the history and thus not reachable via reload or "back". It cannot be used in this exact example, but as Seth also mentions, this example should be using GET instead of POST sethg
According to the HTTP specification, you should use the POST method when you're using the form to change the state of something on the server end. For example, if a page has a form to allow users to add their own comments, like this page here, the form should use POST. If you click "Reload" or "Refresh" on a page that you reached through a POST, it's almost always an error -- you shouldn't be posting the same comment twice -- which is why these pages aren't bookmarked or cached. You should use the GET method when your form is, well, getting something off the server and not actually changing anything. For example, the form for a search engine should use GET, since searching a Web site should not be changing anything that the client might care about, and bookmarking or caching the results of a search-engine query is just as useful as bookmarking or caching a static HTML page. |