Wednesday, July 24, 2013

Dynamic populating the drop down list based on the selected value of first list (Cascading Dropdownlist) Self submission

HOW TO BEGIN THIS SIMPLE PROJECT

  • Create the folder job in your htdocs directory or wwwroot directory
  • create a file with the name test.php inside the job directory
  • create a folder inside job directory, call it connections where you will create the file conn.php containing your database connection string. so create the file conn.php inside the connections folder/directory.
  • lastly copy and past the below code in the file test.php, save it and you are done.
  • lastly test the project by simply opening any of the explorer and type this 
  • http://localhost/job/test.php.

THE INPUT FORM (test.php)


  THE INCLUDE FILE conn.php

create the file conn.php inside the folder called connections that is connections/conn.php. do change your database username and password to based on your server configuration eg apache server

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_conn = "localhost";
$database_conn = "jobglo";
$username_conn = "root";
$password_conn = "root";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR);
?>

 

 

THE FORM SCRIPT (test.php)

copy and paste this code on your editor and save with test.php
<?php require_once('Connections/conn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_conn, $conn);
$query_rstate = "SELECT  DISTINCT stateid, state FROM `state` order by state asc";
$rstate = mysql_query($query_rstate, $conn) or die(mysql_error());
$row_rstate = mysql_fetch_assoc($rstate);
$totalRows_rstate = mysql_num_rows($rstate);

$colname_rslga = "-1";
if (isset($_GET['stateid'])) {
  $colname_rslga = $_GET['stateid'];
}
mysql_select_db($database_conn, $conn);
$query_rslga = sprintf("SELECT DISTINCT lga,lgaid, stateid FROM lga WHERE stateid = %s", GetSQLValueString($colname_rslga, "int"));
$rslga = mysql_query($query_rslga, $conn) or die(mysql_error());
$row_rslga = mysql_fetch_assoc($rslga);
$totalRows_rslga = mysql_num_rows($rslga);
?>
<?php
require_once('connect/conn.php');


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<SCRIPT language=JavaScript>
function reload(form){
var val=form.stateid.options[form.stateid.options.selectedIndex].value;
self.location='test.php?stateid=' + val ;
}
</script>




</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="200" border="0" cellspacing="3" cellpadding="3">
    <tr>
      <td>State:</td>
      <td><label for="stateid"></label>
        <select name="stateid" id="stateid" onchange="reload(this.form)">
          <option  value=  <?php if (!(strcmp("", $row_rstate['stateid']))) {echo "selected=\"selected\"";} ?>>Select One</option>
          <?php
do { 
?>
          <option value="<?php echo $row_rstate['stateid']?>"<?php if (($row_rstate['stateid']==$_GET['stateid'])) {echo "selected=\"selected\"";} ?>><?php echo $row_rstate['state']?></option>
          <?php
} while ($row_rstate = mysql_fetch_assoc($rstate));
  $rows = mysql_num_rows($rstate);
  if($rows > 0) {
      mysql_data_seek($rstate, 0);
      $row_rstate = mysql_fetch_assoc($rstate);
  }
?>
        </select></td>
    </tr>
    <tr>
      <td>LGA</td>
      <td><label for="lga"></label>
        <select name="lga" id="lga">
          <option value="">Select One</option>
          <?php
do { 
?>
          <option value="<?php echo $row_rslga['lgaid']?>"><?php echo $row_rslga['lga']?></option>
          <?php
} while ($row_rslga = mysql_fetch_assoc($rslga));
  $rows = mysql_num_rows($rslga);
  if($rows > 0) {
      mysql_data_seek($rslga, 0);
      $row_rslga = mysql_fetch_assoc($rslga);
  }
?>
      </select></td>
    </tr>
  </table>
</form>
</body>
</html>
<?php
mysql_free_result($rstate);

mysql_free_result($rslga);
?>

CREATE A DATABASE CALLED jobglo and create the two tables below with the data below