PHP & MySQL Tutorial
- Creating Dynamic Content II - Form Values
Each type of HTML form input field sends the data as key-value pairs:
- key: input name
- value: input field content
The following example contains two text input fields called username and book, and a set of radio buttons called level. Only one radio button can be selected, and its value will be sent to the server when the form is submitted.
<html> <body> <form action="survey.php" method="post"> <b>User Name:</b> <input type="text" size="10" name="username"><br /><br /> <input type="submit" value="Submit">> <br /> <br /> <b>Select your level of PHP skill:</b> <br /> <input type="radio" name="level"><br /><br /> <input type="submit" value="Submit"> value="beginner">Beginner <input type="radio" name="level"><br /><br /> <input type="submit" value="Submit"> value="low">Low <input type="radio" name="level"><br /><br /> <input type="submit" value="Submit"> value="middle">Middle <input type="radio" name="level"><br /><br /> <input type="submit" value="Submit"> value="high">High <input type="radio" name="level"><br /><br /> <input type="submit" value="Submit"> value="guru">Guru <br /><br /> <b>What's you favorite CS Book:<b> <input type="text" size="30" name="book"><br /><br /> <input type="submit" value="Submit"> </form> </body> </html>
When HTML form data is submitted with the post method, PHP adds each submitted key-value pair to its special $_POST[] array.
Any submitted value can be referenced by adding its key within quotes between the square brackets. In the example, the level value can be referenced by $_POST['level'].
In the following example for the PHP script begins by assigning each submitted value to a variable, named as its key. The script checks for null values and uses the input values in the created output.
#survey.php <html> <head><title>Submitted Values</title></head> <body> <?php $username = $_POST['username']; $level = $_POST['level']; $book = $_POST['book']; if($username != null) { echo "Thanks for joining the survey: $username <hr>" ; } if(($level != null) && ($book !=null )) { echo "Your skill level of PHP is \"<b>$level</b>\" <br />" ; echo "Your favorite CS book is \"<b>$book</b>\""; } ?> <br /> <img src="images/books.gif" alt="books_gif"> </body> </html>
The submitted data is stored in PHP variables and these variables can be manipulated using the script.
The example below allows the user to enter two numbers with operator. When the form is submitted to the server, these values are sent to a PHP form handler called operation.php.
<html> <body> <form action="operation.php" method="post"> Number A: <input type="text" name="num_a" size="7"> Number B: <input type="text" name="num_b" size="7"> <br /> Operator: <input type="radio" name="op" value="add">+ <input type="radio" name="op" value="sub">- <input type="radio" name="op" value="mul">* <input type="radio" name="op" value="div">/ <br /><hr><br /> <input type="submit" value="Calculate"> <input type="reset" value="Reset"> </form> </body> </html>
#operation.php <html> <head><title>Operations</title></head> <body> <?php $numA = $_POST['num_a']; $numB = $_POST['num_b']; $op = $_POST['op']; if( is_numeric($numA) && is_numeric($numB) ) { if($op != null) { switch ( $op) { case "add" : $result = $numA + $numB; $operator = "+"; break; case "sub" : $result = $numA - $numB; $operator = "-"; break; case "mul" : $result = $numA * $numB; $operator = "*"; break; case "div" : $result = $numA / $numB; $operator = "/"; break; } echo "<h1>$numA $operator $numB = $result</h1>"; } } else echo "Invalid input!" ; ?> <br /> <img src="images/oper_math.jpg" alt="oper_math_jpg"> </body> </html>
The form handler uses is_numeric() to ensure that two numbers have been typed in. It takes a value to be evaluated, and returns true only when the value is numeric.
A switch statement determines the type of operation and performs the operation. Then, it writes out the result or send the user a message for invalid input.
The following example shows how we can play with strings using PHP's string functions. The function name is sent to the server when the form is submitted as the value associated with the key strfnc.
<html> <body> <form action="strfnc.php" method="post"> <b>Enter any text: </b> <br /> <textarea name="txt" rows="3" cols="50"</textarea> <br /> <input type="radio" name="strfnc" value="strlen">Return string length <input type="radio" name="strfnc" value="strrev">Reverse string <input type="radio" name="strfnc" value="strtoupper">Convert string to uppercase <input type="radio" name="strfnc" value="strtolower">Convert string to lowercase <input type="radio" name="strfnc" value="ucwords">Make first letter of string uppercase <br /><hr><br /> <input type="submit" value="Play"> </form> </body> </html>
Here is the php script, stringfnc.php:
<html> <head><title>String Functions</title></head> <body> <?php $quote =$_POST['txt']; $string_function = $_POST['strfnc']; echo ($string_function($quote) ); ?> </body> </html>
The form handler takes the function name from the string_function variable and calls it with the string from the quote key as its argument:
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization