w3pop.com :: ÍøÂçѧԺ :: PHP :: PHP ±íµ¥
The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.
PHP $_GET ºÍ $_POST±äÁ¿ÊÇÓÃÀ´»ñÈ¡±íµ¥ÖеÄÐÅÏ¢µÄ£¬±ÈÈçÓû§ÊäÈëµÄÐÅÏ¢¡£
The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.
ÔÚÎÒÃÇ´¦ÀíHTML±íµ¥ºÍPHP±íµ¥Ê±£¬ÎÒÃÇÒª¼ÇסµÄÖØÒªÒ»µãÊÇ£ºHTMLÒ³ÃæÖеÄÈκÎÒ»¸ö±íµ¥ÔªËض¼¿ÉÒÔ×Ô¶¯µÄÓÃÓÚPHP½Å±¾£º
Form example:
±íµ¥¾ÙÀý£º
<html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html> |
The example HTML page above contains two input fields and a submit button. When the user fills in this form and click on the submit button, the form data is sent to the "welcome.php" file.
ÉÏÊöHTMLÒ³Ãæ°üº¬ÁËÁ½¸öÊäÈë¿ò[input field]ºÍÒ»¸öÌá½»[submit]°´Å¥¡£µ±Óû§½«ÐÅÏ¢ÌîдÍê±Ï²¢µã»÷Ìá½»°´Å¥Ê±£¬±íµ¥µÄÊý¾Ý½«±»·¢ËÍÖÁ“welcome.php”Îļþ¡£
The "welcome.php" file looks like this:
“welcome.php”ÎļþÈçÏÂËùʾ£º
<html> <body> Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old. </body> </html> |
A sample output of the above script may be:
ÉÏÊö½Å±¾½«Êä³öÏÂÃæÕâ¶Î½á¹û£º
Welcome John. You are 28 years old. |
The PHP $_GET and $_POST variables will be explained in the next chapters.
PHP $_GET ºÍ $_POST±äÁ¿½«ÔÚÏÂÒ»ÕÂ×÷¾ßÌå½²½â¡£
User input should be validated on the browser whenever possible (by client scripts (JavaScript)). Browser validation is faster and you reduce the server load.
Óû§ÊäÈëµÄÐÅÏ¢Ó¦¸Ã¾¡¿ÉÄܵÄͨ¹ý¿Í»§¶Ë½Å±¾³ÌÐò£¨È磺JavaScript£©ä¯ÀÀÆ÷ÉÏÑéÖ¤£»Í¨¹ýä¯ÀÀÆ÷½øÐÐÐÅÏ¢µÄÓÐЧÐÔÑéÖ¤¿ÉÒÔÌá¸ßЧÂʲ¢¼õÉÙ·þÎñÆ÷µÄÏÂÔØÑ¹Á¦¡£
You should consider using server validation if the user input will be inserted into a database. A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error.
Èç¹ûÓû§ÊäÈëµÄÐÅÏ¢ÐèÒª´æ½øÊý¾Ý¿â£¬ÄÇôÄã±ØÐ뿼ÂÇÔÚ·þÎñÆ÷¶Ë½øÐÐÓÐЧÐÔÑéÖ¤¡£ÔÚ·þÎñÆ÷ÉÏÑéÖ¤ÐÅÏ¢ÓÐЧÐÔµÄ×îºÃ·½·¨¾ÍÊÇ°Ñ±íµ¥ÐÅÏ¢·¢¸øµ±Ç°Ò³½øÐÐÑéÖ¤£¬¶ø²»Êǵ÷µ½ÆäËûÒ³Ãæ½øÐÐÑéÖ¤¡£Í¨¹ýÉÏÊö·½·¨£¬Èç¹û±íµ¥´æÔÚ´íÎó£¬Óû§¿ÉÒÔÖ±½ÓÔÚµ±Ç°Ò³»ñÈ¡´íÎóÐÅÏ¢¡£ÕâʹµÃÎÒÃǸüÈÝÒ×·¢ÏÖ´æÔڵĴíÎóÐÅÏ¢¡£
ÆÀÂÛ (0)
All