Scripts For Sites

Your script source

phpstore.info
 

PHP Form Handling
by David Sklar,

If your PHP program is a dynamic web page (and it probably is) and your PHP program is dealing with user input (and it probably is), then you need to work with HTML forms. Here are some tips for simplifying, securing, and organizing your form-handling PHP code.

1. Use $_SERVER['PHP_SELF'] as a form action.
The $_SERVER auto-global array holds various useful server- and request-specific info. The PHP_SELF element of $_SERVER holds the filename of the currently executing script (relative to your web site's document root directory). So, supplying $_SERVER['PHP_SELF'] as the action attribute of the form tag makes the form submit to the same page that displayed it. This lets you put the logic to handle the form in the same page as the logic that displays it. For many simple forms, this keeps things easy to manage.

2. Put [] at the end of the name of a multivalued form parameter.
When you've got a form element like < select multiple > that can submit multiple values to the server, put [] at the end of the form element name so that the PHP interpreter knows to accept multiple values.

For example, consider this form:

< form method="POST" action="< ?php echo $_SERVER['PHP_SELF']; ? >" >
Pick some desserts: < select name="sweet[]" multiple>

< option value="square" > Coconut Milk Gelatin Square< /option >
Add to cart
View cart
 
 

Copyright 2008 scripts-for-sites.info

The PHP Resource Index