How to Select the Value From a List Box to Another List Box in PHP
PHP is a server side scripting language that comes with built-in features that help make the creation of dynamic user interfaces straightforward and robust. For example, PHP offers two built-in super global variables: $_POST and $_GET, that support passing values between PHP pages. These variable values are typically written based on user interaction, such as the submitting of a form's data, and stored in an associative array, where embedded PHP code can use them to alter the user interface.
Instructions
-
-
1
Open a text editor and create a new text file. Selecting "New" from the "File" menu usually creates new files. Name the file "copyListBox.php" and save the file on the web server in a location that has access to the PHP interpreter.
-
2
Add two PHP delimiters to the file ("<?php" and "?>"). These delimiters tell the PHP interpreter to treat any text placed between them as PHP code.
<?php
?>
-
-
3
Add a PHP "if" conditional statement between the "<?php" and "?>" delimiters. This statement uses the "isset()" function to verify that the $_POST array contains a value named "submit". Checking this value ensures that the conditional code does not execute if the form containing the "firstListBox" element has never been submitted. Follow the "if" statement with an open "{" and a close "}" curly brace.
<?php
if (isset($_POST['submit'])) {
}
?>
-
4
Declare a PHP variable named "$copiedValues" between the "if" statement's open and close curly braces and set the variable to the value in the $_POST array. Ultimately, the $_POST array will contain the value selected from a list box named "firstListBox".
<?php
if (isset($_POST['submit'])) {
$copiedValues = $_POST["firstListBox"];
}
?>
-
5
Use the PHP "print()" language construct to create a list box using the "<select>" and "<option>" HTML tags. This list box will display the value stored in the $copiedValues variable -- the item selected from the "firstListBox" element. Precede the "<select>" tag with the text "Second list box:". Close both the "</option>" and the "</select>" tags.
<?php
if (isset($_POST['submit'])) {
$copiedValues = $_POST["firstListBox"];
print "Second list box: <select name='secondListBox'><option>$copiedValues</option></select>";
}
?>
-
6
Add an HTML form after the close ("?>") PHP tag. Give the form a "method" attribute of "post" and set the "action" attribute to "copyListBox.php". When the form is submitted, the $copiedValues variable will be set to the item selected from the "firstListBox" list box.
<?php
if (isset($_POST['submit'])) {
$copiedValues = $_POST["firstListBox"];
print "Second list box: <select name='secondListBox'><option>$copiedValues</option></select>";
}
?>
<form method="post" action="copyListBox.php">
</form>
-
7
Add the text "First list box:" immediately following the "<form>" tag. Follow the tag with an HTML "<select>" tag with a "name" attribute of "firstListBox". Add a "</select>" tag to complete the list box element.
<?php
if (isset($_POST['submit'])) {
$copiedValues = $_POST["firstListBox"];
print "Second list box: <select name='secondListBox'><option>$copiedValues</option></select>";
}
?>
<form method="post" action="copyListBox.php">
First list box:
<select name="firstListBox">
</select>
</form>
-
8
Add two HTML "<option>" tags between the "<select>" and "</select>" tags. Give the first "<option>" tag a value attribute of "1", a text value of "1" and close the "</option>" tag. Give the second "<option>" tag a "value" attribute of "2", a text value of "2" and close the "</option>" tag.
<?php
if (isset($_POST['submit'])) {
$copiedValues = $_POST["firstListBox"];
print "Second list box: <select name='secondListBox'><option>$copiedValues</option></select>";
}
?>
<form method="post" action="copyListBox.php">
First list box:
<select name="firstListBox">
<option value="1" >1</option>
<option value="2">2</option>
</select>
</form>
-
9
Add an HTML "<input>" tag to the file immediately after the "</select>" tag. Give the tag a "type" attribute of "submit", a "name" attribute of "submit" and a "value" attribute of "Click here to copy the selection to the list box". Complete the button by adding a "</button>" close tag. Save and close copyListBox.php.
<?php
if (isset($_POST['submit'])) {
$copiedValues = $_POST["firstListBox"];
print "Second list box: <select name='secondListBox'><option>$copiedValues</option></select>";
}
?>
<form method="post" action="copyListBox.php">
First list box:
<select name="firstListBox">
<option value="1" >1</option>
<option value="2">2</option>
</select>
<input type="submit" name="submit" value="Click here to copy the selection to the list box.">
</form>
-
10
Open copyListBox.php in a web browser. Click the "Click here to copy the selection to the list box" button and verify that the item selected in the first list box is copied to the second list box.
-
1
Tips & Warnings
The PHP $_GET array can be used to submit data using the "get" method. Using this method is insecure, since sent values are visible in the URL.
Cookies can be used to pass and store data used in PHP web pages.
PHP session data can be used to store and retrieve data. When storing a large amount of data, consider using a database.
Be sure to check user data and filter it to avoid hackers.
References
Resources
- Photo Credit Comstock/Comstock/Getty Images