PHP Simplification of Certain Form FieldS

PHP Simplification of Certain Form Fields   <SELECT NAME="table2">   <OPTION Value=2>2</OPTION>   <OPTION Value=3>3</OPTION>   <OPTION Value=4>4</OPTION>   <OPTION Value=5>5</OPTION>   <OPTION Value=6>6</OPTION>   <OPTION Value=7>7</OPTION>   <OPTION Value=8>8</OPTION>   <OPTION Value=9>9</OPTION>   <OPTION Value=10>10</OPTION>   <OPTION Value=11>11</OPTION>   <OPTION Value=12>12</OPTION> </SELECT>  <SELECT NAME="Table2"> <?php   for ($i=2; $i <= 12; $i++)     echo "  <OPTION Value=$i>$i</OPTION>\n"; ?> </SELECT>   2 <input type=radio name="table3" value="2" checked> 3 <input type=radio name="table3" value="3"> 4 <input type=radio name="table3" value="4"> 5 <input type=radio name="table3" value="5"> 6 <input type=radio name="table3" value="6"> 7 <input type=radio name="table3" value="7"> 8 <input type=radio name="table3" value="8"> 9 <input type=radio name="table3" value="9"> 10 <input type=radio name="table3" value="10"> 11 <input type=radio name="table3" value="11"> 12 <input type=radio name="table3" value="12">  <?php $checked = 5; for ($i=2; $i <= 12; $i++)   echo "$i <input type=radio name=\"table3\" value=$i" .            (($i == $checked) ? ' checked' : '') . ">\n"; ?>  URL Parameters - GET method  Additional data can be passed to a script via parameters indicated on the URL line:  http://server/scriptname.cgi?parameters  These parameters normally come in two possible formats:      keyword=value pairs with multiple values separated with ampersands (&); to include spaces, substitute plus signs (+)      simple character sequences with multiple values separated with plus signs (+)  scriptname.cgi?this=that&name=value+with+spaces  scriptname.cgi?value1+value2+value3  Depending upon which of these formats is used for the parameter data, different PHP super-global variables can be used:  $_GET       used with name=value pairs  $_GET will be an associative array with the names being the indices $_SERVER       used with simple character sequences  $_SERVER['argc'] contains the number of character sequences;  $_SERVER['argv'] contains an array of the actual character sequence values  scriptname.cgi?this=that&name=value+with+spaces  $_GET = Array (   [this] => that   [name] => value with spaces )    scriptname.cgi?value1+value2+value3  $_SERVER['argc'] = 3  $_SERVER['argv'] = Array (   [0] => value1   [1] => value2   [2] => value3 )  Combining POST data with URL arguments  Even with posting forms data, it is also possible to include URL arguments on the ACTION= field on the form. This data will be passed to the executing script just as with the GET method.  <form method=POST action="script.cgi?value1+value2"> <input name=Field1 value="Field1 data"> <input type=submit name=Button value="Press Me"> </form>    $_POST = Array {   [Field1] = Field1 data   [Button] = Press Me }  $_SERVER['argc'] = 2  $_SERVER['argv'] = Array (   [0] => value1   [1] => value2 )  Submitting Form Data with GET method  Although less-commonly used, the submit method for form data can also be GET rather than the normal POST.  <form method=GET action="script.cgi"> <input name=Field1 value="Field1 data"> <input type=submit name=Button value="Press Me"> </form>  When script is called, the URL will appear as:  script.cgi?Field1=Field1+data&Button=Press+Me  $_GET = Array (   [Field1] => Field1 data   [Button] => Press Me )  $_POST = Array { }  In earlier versions of servers, URL arguments were limited to ~100 characters, basically eliminating the practical use of the GET posting method, especially when TEXTAREA fields were involved. Today this limitation has virtually been eliminated, and it no longer is considered a limitation of the GET posting method.  However, since the resultant $_GET array when using the GET posting method has the same appearance as the $_POST array when using the POST posting method, the GET posting method is considered (by DonR) to be unnecessary.  Code example demonstrating the various ways a script can be called, along with various parameter-passing techniques: http://students.csci.unt.edu/~donrclass/getVSpost.cgi  URL encoding of Special Characters  Since the decoding of the URL must include special separating characters (for example, + for space, & for separating GET fields), what happens when you would like to use those characters (or other non-alphabetic characters) as data within the URL argument?  You have to use a special hexadecimal-based encoding notation to represent these special characters. Their general format is:  %hexvalue  Some examples are:  %2B - plus sign %26 - ampersand %3D - equal sign  script.cgi?parm=This+has+a+plussign+%2B  $_GET = Array {   [parm] = This has a plussign + }  When a URL argument is produced inside a PHP script, the PHP function urlencode() should be used to properly encode any non-alphabetic characters found in the argument:  <?php  $data = '# @ $ +'; echo '<a href="script.cgi?parm=' . urlencode($data) . '">';  ?>    <a href="script.cgi?parm=%23+@+%24+%2B">
Share on Google Plus

About M

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment