In this sample program I will show you how to select a value from a drop down menu in HTML and extract the data in an XML file. This will be my first time to write a program that uses XML and PHP. According to Wikipedia.org XML is defined as Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format which is both human-readable and machine-readable. It is defined by the W3C's XML 1.0 Specification and by several other related specifications, all of which are free open standards.
The code is very easy to follow and understand I hope you will find my work useful in your programming projects and assignments. If you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com.
People here in the Philippines can contact me at my mobile number 09173084360.
Thank you very much and Happy Programming.
Program Listing
index.php
<!-- Selecting a value in PHP and XML -->
<!-- June 7, 2015 Sunday -->
<!-- Written By: Mr. Jake R. Pomperada, MAED-IT -->
<!-- Email Address: jakerpomperada@gmail.com -->
<!-- : jakerpomperada@yahoo.com -->
<html>
<title>Selecting a value in PHP and XML
</title>
<style>
h1 {
font-family:arial;
color:blue;
};
</style>
<body bgcolor="lightgreen">
<br><br>
<h1>
SELECTING A VALUE IN PHP AND XML
</h1>
<form method="post" action="">
<select id="select" class="select" name="filter1" style="text-align:center;">
<option value="1" <?php if(@$_POST['filter1'] == '1') { echo 'selected = \"selected\"'; } ?>>First Record</option>
<option value="2" <?php if(@$_POST['filter1'] == '2') { echo 'selected = \"selected\"'; } ?>>Second Record</option>
<option value="3" <?php if(@$_POST['filter1'] == '3') { echo 'selected = \"selected\"'; } ?> >Third Record</option>
<option value="4" <?php if(@$_POST['filter1'] == '4') { echo 'selected = \"selected\"'; } ?>>Fourth Record</option>
</select>
<input type="submit" id="submit" name="submit" value="ok" title="Click here to select your choice.">
</form>
<br>
<?php
if(isset($_POST['submit'])){
if($_POST['filter1'] == "1")
{
$users = simplexml_load_file("1.xml");
foreach ($users->person as $item)
{
if ($item->id == 1) {
echo "<font color='blue' face='arial' size='5'>Title : ".$item->title."<br />";
echo "Name : ".$item->name." <br />";
echo "Address : ".$item->address." <br /> </font>";
}
}
}
else if ($_POST['filter1'] == "2")
{
$users = simplexml_load_file("1.xml");
foreach ($users->person as $item)
{
if ($item->id == 2) {
echo "<font color='blue' face='arial' size='5'>Title : ".$item->title." <br />";
echo "Name : ".$item->name." <br />";
echo "Address : ".$item->address." <br /> </font>";
}
}
}
else if ($_POST['filter1'] == "3")
{
$users = simplexml_load_file("1.xml");
foreach ($users->person as $item)
{
if ($item->id == 3) {
echo "<font color='blue' face='arial' size='5'> Title : ".$item->title." <br />";
echo "Name : ".$item->name." <br />";
echo "Address : ".$item->address."</font> <br />";
}
}
}
else if ($_POST['filter1'] == "4")
{
$users = simplexml_load_file("1.xml");
foreach ($users->person as $item)
{
if ($item->id == 4) {
echo "<font color='blue' face='arial' size='5'> Title : ".$item->title." <br />";
echo "Name : ".$item->name." <br />";
echo "Address : ".$item->address."</font> <br />";
}
}
}
else {
echo "No Selected Records.";
}
}
?>
</body>
</html>
1.xml
<?xml version="1.0"?>
<information>
<person>
<id> 1 </id>
<title> First Record </title>
<name> Ana Maria Tan </name>
<address> Cubao, Quezon City </address>
</person>
<person>
<id> 2 </id>
<title> Second Record </title>
<name> David Smith </name>
<address> Washington Street, Iloilo City </address>
</person>
<person>
<id> 3</id>
<title> Third Record </title>
<name> Linda Lee </name>
<address> Hongkong, China </address>
</person>
<person>
<id> 4 </id>
<title> Fourth Record </title>
<name> Adam Stone </name>
<address> West Avenue, United Kingdom </address>
</person>
</information>