How to Create a PHP Email Form With Radio Buttons
Much of PHP's popularity as a scripting language is due to its broad range of built-in functions. Whatever the developer's programming need, there is probably a PHP function that addresses it. For example, PHP's $_GET and $_Post functions can access the form data in HTML. Both $_GET and $_Post take the form element name as a parameter and return the value associated with it. In the case of radio buttons, two or more form radio buttons differ in value but have the same name. A PHP script containing $_GET["name"] or $_Post["name"] can use the result like any other PHP variable -- including mail() function calls.
Things You'll Need
- Plain text editor
- Access to a Web server with PHP installed
- FTP application
Instructions
-
-
1
Launch the standard, plain-text editor application that is available on your computer.
-
2
Enter the following code into the text editor.
<html>
<head>
<body>
<form method="get" action="sendoutmail.php">
Email Address: <input type="text" name="email">
Sign Up? <input type="radio" name="choice" value="rb1" /><br />
Never? <input type="radio" name="choice" value="rb2" /><br />
Ask me later. <input type="radio" name="choice" value="rb3" /><br />
<input type="submit" name="submit" value="Submit Form">
</form>
</body>
</html>
-
-
3
Click the "File" menu. Select the "Save" option. Save under the file name "test.php."
-
4
Click the File menu. Select the "New" option. Enter the following code into the new text editor window.
<?php
$selection["rb1"]="Sign Up";
$selection["rb2"]="Never";
$selection["rb3"]="Ask Me Later";
$email = $_GET['email'];
$radiobutton = $_GET['choice'];
echo "The email address is : ".$email;
echo "";
echo "The button selected was : ".$selection[$radiobutton];
$message="You chose: ".$selection[$radiobutton];
echo "";
echo 'Your mail() function may look like this: ';
echo 'mail('. $email.', "Survey Result" , "'.$message.'")';
?>
-
5
Click the "File" menu. Select the "Save" option. Save under the file name "sendoutmail.php ."
-
6
Click on the "File" menu and select "Quit" or "Exit" to close the text editor.
-
7
Launch the FTP application and login to the Web server.
-
8
Upload the files "test.php" and "sendoutmail.php" to the root directory of the Web server.
-
9
Launch a Web browser and enter the appropriate URL to access the PHP file. Example:
http://yourdomainname.com/test.php
Replace "yourdomainname.com" with the domain name or IP address of the Web server.
-
10
Press the "Enter" key to load the URL and run the PHP file. Fill in the email address and click one of the radio buttons. Click the "Submit Form" button. The browser should display the email address you entered, which radio button you clicked and a sample mail function call.
-
1
Tips & Warnings
When creating your HTML forms, choose form element names and values that are descriptive and readily reveal their purpose. While this requires a little more thought and much more typing, it makes it easier to write a valid PHP form-handling script later. If form names or values are mismatched, the form handler may crash or produce unintended results.
Use the "Post" method rather than "Get" when you submit forms. When "Get" is used, the PHP form handler script is sent a URL containing the form element names and values. This URL can disclose information you do not want known. On the other hand, "Post" uses the HTTP post transaction protocol, which essentially hides the data submitted from view. In addition, "Get" submissions can be bookmarked, while "Post" submissions cannot.
References
Resources
- Photo Credit Comstock/Comstock/Getty Images