POST values are unlimited in length, and thus are very well suited for forms, especially forms with a lot of fields. If you have PHP 4.2.0 or later, don’t worry about it. htaccess file (if you are using Apache server) for the exact same reasons as were mentioned in the previous tutorial on GET. If you are using a version of PHP earlier than 4.2.0, you should strongly consider setting register_globals to “off” in your. You can use server variables like $_SERVER and $_SERVER to build an action value.įor more information, see Using PHP_SELF in the action field of a form Register globals off? This is important because it will also preserve the querystring when the form is submitted (the ?lang=english part). However, by not putting in an action, browsers will assume that the form is submitting to itself. One more thing to notice: the “action” on the form is now missing. It’s recommended not to do this unless you really have to, because it can be confusing, and it’s best to be clear about where an input is coming from. If GET and POST variables have the same name, POST will take priority. The order doesn't matter.Instead of using GET and POST arrays, you can also use the $_REQUEST array, which will contain the combined contents of the data. With this, you can define already in the form, in which form the data should be available later. In addition, as you can see, we are completely free in the arrangement and can mix normal fields between array fields. Of course you can also transfer any number of arrays next to each other and combine them with non-array fields: Īnd then read out in PHP: $field = $_POST įoreach ($otherarray as $value) echo $value This makes it easier to keep the individual fields apart. In our PHP script we can address the individual fields directly using their assigned names: // receive all data in an array Alternatively, we can give the individual fields in the array names and thus get an associative array: In our first example, by writing we got a numerically indexed array. So we save ourselves the work of addressing and reading out each field individually. In this example we assign $_POST to a variable, and then iterate through the array element by element. We can work with the array like a normal PHP array: // receive all data in an array In our example we use $_POST to access the value that the user entered in the first field. The first array field from our form has the index 0, the second 1 and so on. In our PHP script we can receive the array with $_POST and access the transferred values: // output a single value The name "field" is arbitrary, but each input field must have the same name field so that all user entries are later in the same array. The key to being able to receive the data entered by the user directly as an array are the square brackets. In our first example, we have three input fields and we want to receive their content in our PHP script "script.php" as just one array. A first exampleįirst, let's look at a simple example. Typical applications are, for example, forms with numerous fields of the same type or forms in which a previously unknown number of fields is created dynamically. I think this approach has more sense that the other solutions, as this way you would have an array of items, being each item a value and a description, instead of having two separate arrays of values and descriptions and having to get the values for your item from those two arrays instead of one. In this tutorial, I will show you, how to do this. It is often quite handy to receive inputs from a form directly as an array in a PHP script to be able to directly start processing the data in the correct form.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |