Jump to content

Help with Dynamic Lists and PHP


jskenya

Recommended Posts

Ok here we go here is my code

 

The name of the file is stations.php you can see the output at http://www.pollskenya.com/stations.php

 

The problem is there are 3 dynamic drop downs on the page 2 are supposed to be dynamic i.e when the user picks an item in the first the first drop down then the page should reload and repopulate the second drop down which then reloads and re populates the final drop down...

 

Now i have managed to get the javascript to do this but i cant seem to get the forms to execute with the variebles to get the page working dynamically....

 

<?php require_once('Connections/connect.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "wards")) {
  $insertSQL = sprintf("INSERT INTO stations (stname) VALUES (%s)",
                       GetSQLValueString($_POST['station'], "text"));

  mysql_select_db($database_connect, $connect);
  $Result1 = mysql_query($insertSQL, $connect) or die(mysql_error());

  $insertGoTo = "stations.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "wards")) {
  $insertSQL = sprintf("INSERT INTO wards (constid) VALUES (%s)",
                       GetSQLValueString($_POST['constituency'], "int"));

  mysql_select_db($database_connect, $connect);
  $Result1 = mysql_query($insertSQL, $connect) or die(mysql_error());

  $insertGoTo = "stations.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "wards")) {
  $insertSQL = sprintf("INSERT INTO stations (wardid) VALUES (%s)",
                       GetSQLValueString($_POST['wards'], "int"));

  mysql_select_db($database_connect, $connect);
  $Result1 = mysql_query($insertSQL, $connect) or die(mysql_error());

  $insertGoTo = "stations.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
$province=$_GET['province'];
$province=1;
echo $row_provinces['provid'];
mysql_select_db($database_connect, $connect);
$query_constituency = "SELECT DISTINCT
constituency.constname,
constituency.constid
FROM
constituency ,
province
WHERE
constituency.provid = '$province'
ORDER BY
constituency.constname ASC
";
$constituency = mysql_query($query_constituency, $connect) or die(mysql_error());
$row_constituency = mysql_fetch_assoc($constituency);
$totalRows_constituency = mysql_num_rows($constituency);

mysql_select_db($database_connect, $connect);
$query_provinces = "SELECT * FROM province";
$provinces = mysql_query($query_provinces, $connect) or die(mysql_error());
$row_provinces = mysql_fetch_assoc($provinces);
$totalRows_provinces = mysql_num_rows($provinces);

mysql_select_db($database_connect, $connect);
$ward=$_GET['ward'];
$query_wards = "SELECT DISTINCT
wards.wardid,
wards.wardname
FROM
wards ,
constituency
WHERE
wards.constid = '$ward'
ORDER BY
wards.wardname ASC
";
$wards = mysql_query($query_wards, $connect) or die(mysql_error());
$row_wards = mysql_fetch_assoc($wards);
$totalRows_wards = mysql_num_rows($wards);

mysql_select_db($database_connect, $connect);
$query_stations = "SELECT * FROM stations";
$stations = mysql_query($query_stations, $connect) or die(mysql_error());
$row_stations = mysql_fetch_assoc($stations);
$totalRows_stations = mysql_num_rows($stations);

mysql_select_db($database_connect, $connect);
$query_wards = "SELECT * FROM wards";
$wards = mysql_query($query_wards, $connect) or die(mysql_error());
$row_wards = mysql_fetch_assoc($wards);
$totalRows_wards = mysql_num_rows($wards);

mysql_select_db($database_connect, $connect);
$query_constituency = "SELECT * FROM constituency";
$constituency = mysql_query($query_constituency, $connect) or die(mysql_error());
$row_constituency = mysql_fetch_assoc($constituency);
$totalRows_constituency = mysql_num_rows($constituency);


?><?php 
$cat=$_POST['cat'];
$subcat=$_POST['subcat'];


include 'header.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=iso-8859-1" />
<title>Wards</title><SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dd.php?cat=' + val ;
}

</script>
<script type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body>
<div align="center">
  <table width="100%" border="0" cellspacing="2" cellpadding="2">
    <tr>
      <td height="%"><div align="center">
        <form action="<?php echo $editFormAction; ?>" method="POST" name="wards" id="wards">
          <table width="100%" border="0" cellspacing="2" cellpadding="2">
            <tr>
              <td colspan="2" valign="top"><strong>Please Input the Correct Stations Information: </strong></td>
              </tr>
            <tr>
              <td valign="top"><div align="right">Province:</div></td>
              <td valign="top"><label>
              <select name="province" id="province" onchange="reload(this.form)">
                <?php
do {  
?>
                <option value="<?php echo $row_provinces['provid']?>"<?php if (!(strcmp($row_provinces['provid'], $row_provinces['provid']))) {echo "selected=\"selected\"";} ?>><?php echo $row_provinces['provname']?></option>
                <?php
} while ($row_provinces = mysql_fetch_assoc($provinces));
  $rows = mysql_num_rows($provinces);
  if($rows > 0) {
      mysql_data_seek($provinces, 0);
  $row_provinces = mysql_fetch_assoc($provinces);
  }
?>
              </select>
              </label></td>
            </tr>
            <tr>
              <td width="38%" valign="top"><div align="right">Constituency:</div></td>
              <td width="62%" valign="top"><div align="left">
                <label>
                <select name="constituency" id="constituency">
                  <?php
do {  
?>
                  <option value="<?php echo $row_constituency['constname']?>"<?php if (!(strcmp($row_constituency['constname'], $row_provinces['provid']))) {echo "selected=\"selected\"";} ?>><?php echo $row_constituency['constname']?></option>
                  <?php
} while ($row_constituency = mysql_fetch_assoc($constituency));
  $rows = mysql_num_rows($constituency);
  if($rows > 0) {
      mysql_data_seek($constituency, 0);
  $row_constituency = mysql_fetch_assoc($constituency);
  }
?>
                </select>
                </label>
</div></td>
            </tr>
            <tr>
              <td valign="top"><div align="right">Wards: </div></td>
              <td valign="top"><label>
                <select name="wards" id="wards">
                  <?php
do {  
?>
                  <option value="<?php echo $row_wards['wardname']?>"<?php if (!(strcmp($row_wards['wardname'], $row_wards['wardname']))) {echo "selected=\"selected\"";} ?>><?php echo $row_wards['wardname']?></option>
                  <?php
} while ($row_wards = mysql_fetch_assoc($wards));
  $rows = mysql_num_rows($wards);
  if($rows > 0) {
      mysql_data_seek($wards, 0);
  $row_wards = mysql_fetch_assoc($wards);
  }
?>
                </select>
              </label></td>
            </tr>
            <tr>
              <td valign="top"><div align="right">Station:</div></td>
              <td valign="top"><input name="station" type="text" id="station" /></td>
            </tr>
            <tr>
              <td valign="top"> </td>
              <td valign="top"><label>
                    <div align="left">
                      <input type="submit" name="Submit2" value="Submit">
                      </label>
                  </div></td>
            </tr>
          </table>
          
          
          
          
          
          <input type="hidden" name="MM_insert" value="const">
          <input type="hidden" name="MM_insert" value="wards">
        </form>
      </div></td>
    </tr>
    <tr>
      <td height="0"><table width="100%" border="0" cellspacing="2" cellpadding="2">
        <tr>
          <td colspan="4"><div align="center"><strong> Stations </strong></div></td>
          </tr>
        <tr>
          <td> Id </td>
          <td>Name</td>
          <td>Ward ID </td>
          <td>Action</td>
        </tr>
        <tr>
          <td width="19%" bgcolor="#999999"><?php echo $row_stations['stid']; ?></td>
          <td width="22%" bgcolor="#66FFFF"><?php echo $row_stations['stname']; ?></td>
          <td width="22%" bgcolor="#666666"><?php echo $row_stations['wardid']; ?></td>
          <td width="18%" bgcolor="#996633"><div align="center"><a href="#">Edit</a> :: <a href="#">Delete</a> </div></td>
        </tr>
      </table></td>
    </tr>
  </table>
</div>
</body>
</html>
<?php
mysql_free_result($constituency);

mysql_free_result($provinces);

mysql_free_result($wards);

mysql_free_result($stations);
?>
<?php 
include 'footer.php';
?>

Link to comment
Share on other sites

In this case i know its possible because i am not submitting the into the database the information from the form until the users makes the final text box entry.

 

I would like to summarise it as shortlisting.

 

Does anyone think they can help i can provide the SQL and requried files...

 

Thanx

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.