linuxStat PHP Documentation
PHP Function - array_key_exists
I would like to go back to the example I gave for check boxes.
After the user selects one or more of the five choices and
presses the Submit button, we execute a PHP program in a file called
getinvestorsinterest.php to echo back the selections. However, this
program echos back blanks for the check boxes not selected by the user.
I would like to modify the PHP program so that it echos back only the selections
made. It detects the selections not made and echos nothing for them.
Corresponding to the selections not made there are no entries in the _GET map.
This can be detected by using the PHP function array_key_exists().
We will include the modified program in a file called echoonlyselected.php.
An HTML document containing the above check boxes is the following.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<title>Executed on Dell Server, Intel Processor, created on HP laptop</title>
</head>
<body bgcolor="#0F243E" text="#FFFF00" lang=EN-US>
<form action="echoonlyselected.php" method="get">
Investment Interest:<br><br>
<input type="checkbox" name="InvIntDS" value="Domestic Stock">Domestic stock funds<br>
<input type="checkbox" name="InvIntGS" value="Global Stock">Global stock funds<br>
<input type="checkbox" name="InvIntDB" value="Domestic Bond">Domestic bond funds<br>
<input type="checkbox" name="InvIntGB" value="Global Bond">Global bond funds<br>
<input type="checkbox" name="InvIntMM" value="Money Market">Money market funds<br>
<br><br>
<input type="submit" name="Submit" value="Submit" size="30">
</form>
</body>
</html>
The following is the PHP code I will save in echoonlyselected.php file.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<title>Executed on Dell Server, Intel Processor, created on HP laptop</title>
</head>
<body bgcolor="#0F243E" text="#FFFF00" lang=EN-US>
<?php
# Example of check box data in _GET map
print "Your interests are:<br>";
$nameExists = array_key_exists("InvIntDS", $_GET);
if ($nameExists) {
print "{$_GET["InvIntDS"]}<br>";
}
$nameExists = array_key_exists("InvIntGS", $_GET);
if ($nameExists) {
print "{$_GET["InvIntGS"]}<br>";
}
$nameExists = array_key_exists("InvIntDB", $_GET);
if ($nameExists) {
print "{$_GET["InvIntDB"]}<br>";
}
$nameExists = array_key_exists("InvIntGB", $_GET);
if ($nameExists) {
print "{$_GET["InvIntGB"]}<br>";
}
$nameExists = array_key_exists("InvIntMM", $_GET);
if ($nameExists) {
print "{$_GET["InvIntMM"]}<br>";
}
?>
</body>
</html>
Click the following link to execute the above PHP code
by my server's PHP interpreter.
Try the above example Next Index
If you would like to learn more about array_key_exists,
click the following link.
More on array_key_exists