sábado, 21 de maio de 2011

list Menu - Ajax

Este post serve para me recordar a recriar uma página que permite fazer uma sequência de pesquisas entre tabelas interligadas sem sair da página. Para melhor compreender este esquema recorri a 3 tabelas interligadas: distritos, concelhos e freguesias.



<script src="concelhos.js"></script>
<script src="distritos.js"></script>

distritos.php

<?php require_once('Connections/ligarajax.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_ligarajax, $ligarajax);
$query_rsdist = "SELECT * FROM distritos";
$rsdist = mysql_query($query_rsdist, $ligarajax) or die(mysql_error());
$row_rsdist = mysql_fetch_assoc($rsdist);
$totalRows_rsdist = mysql_num_rows($rsdist);
?>
<html>
<head>
<script src="concelhos.js"></script>
<script src="distritos.js"></script>
<title>
Search
</title></head>
<body>
<form>
<select name="search" id="search" onChange="searchUser(this.value)">
  <option value="value">Escolhe o Distrito</option>
  <?php
do {
?>
  <option value="<?php echo $row_rsdist['iddist']?>"><?php echo $row_rsdist['dist']?></option>
  <?php
} while ($row_rsdist = mysql_fetch_assoc($rsdist));
  $rows = mysql_num_rows($rsdist);
  if($rows > 0) {
      mysql_data_seek($rsdist, 0);
 $row_rsdist = mysql_fetch_assoc($rsdist);
  }
?>
</select>
</form>
<div id="searchresult" name="searchresult"> Aguarda concelhos...</div>

<p>E aqui continua o site</p>
</body>
</html>
<?php
mysql_free_result($rsdist);
?>

distritos.js
var xmlHttp
function searchUser(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="concelhos.php"
url=url+"?iddist="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("searchresult").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

concelhos.js
var xmlHttp
function searchUser2(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="freguesias.php"
url=url+"?idconc="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("searchresult2").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

concelhos.php
<?php require_once('Connections/ligarajax.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;
}
}

$colname_rsconcelhos = "-1";
if (isset($_GET['iddist'])) {
  $colname_rsconcelhos = $_GET['iddist'];
}
mysql_select_db($database_ligarajax, $ligarajax);
$query_rsconcelhos = sprintf("SELECT * FROM concelhos WHERE iddist = %s", GetSQLValueString($colname_rsconcelhos, "int"));
$rsconcelhos = mysql_query($query_rsconcelhos, $ligarajax) or die(mysql_error());
$row_rsconcelhos = mysql_fetch_assoc($rsconcelhos);
$totalRows_rsconcelhos = mysql_num_rows($rsconcelhos);

mysql_select_db($database_ligarajax, $ligarajax);
$query_nto = "SELECT * FROM concelhos";
$nto = mysql_query($query_nto, $ligarajax) or die(mysql_error());
$row_nto = mysql_fetch_assoc($nto);
$totalRows_nto = mysql_num_rows($nto);
?>
<!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=windows-1252" />
<title>Untitled Document</title>
<script src="concelhos.js"></script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<select name="search2" id="search2" onChange="searchUser2(this.value)">
  <option value="value">Escolhe o concelho</option>
  <?php
do {  
?>
  <option value="<?php echo $row_rsconcelhos['idconc']?>"><?php echo $row_rsconcelhos['conc']?></option>
  <?php
} while ($row_rsconcelhos = mysql_fetch_assoc($rsconcelhos));
  $rows = mysql_num_rows($rsconcelhos);
  if($rows > 0) {
      mysql_data_seek($rsconcelhos, 0);
 $row_rsconcelhos = mysql_fetch_assoc($rsconcelhos);
  }
?>
</select>
</form>
<div id="searchresult2" name="searchresult2"> Aguarda freguesias...</div>

</body>
</html>
<?php
mysql_free_result($rsconcelhos);

mysql_free_result($nto);
?>

freguesias.php
<?php require_once('Connections/ligarajax.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;
}
}

$colname_rsfreguesias = "-1";
if (isset($_GET['idconc'])) {
  $colname_rsfreguesias = $_GET['idconc'];
}
mysql_select_db($database_ligarajax, $ligarajax);
$query_rsfreguesias = sprintf("SELECT * FROM freguesias WHERE idconc = %s", GetSQLValueString($colname_rsfreguesias, "int"));
$rsfreguesias = mysql_query($query_rsfreguesias, $ligarajax) or die(mysql_error());
$row_rsfreguesias = mysql_fetch_assoc($rsfreguesias);
$totalRows_rsfreguesias = mysql_num_rows($rsfreguesias);

$colname_rsconcelhos = "-1";
if (isset($_GET['idconc'])) {
  $colname_rsconcelhos = $_GET['idconc'];
}
mysql_select_db($database_ligarajax, $ligarajax);
$query_rsconcelhos = sprintf("SELECT * FROM concelhos WHERE iddist=( SELECT concelhos.iddist FROM  concelhos WHERE concelhos.idconc=%s)", GetSQLValueString($colname_rsconcelhos, "int"));
$rsconcelhos = mysql_query($query_rsconcelhos, $ligarajax) or die(mysql_error());
$row_rsconcelhos = mysql_fetch_assoc($rsconcelhos);
$totalRows_rsconcelhos = mysql_num_rows($rsconcelhos);
?>
<!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=windows-1252" />
<title>Untitled Document</title>
<script src="concelhos.js"></script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <p>
    <label for="search"></label>
    <select name="search2" id="search2" onChange="searchUser2(this.value)">
  <option value="value">Escolhe o concelho</option>
  <?php
do {  
?>
  <option value="<?php echo $row_rsconcelhos['idconc']?>"><?php echo $row_rsconcelhos['conc']?></option>
  <?php
} while ($row_rsconcelhos = mysql_fetch_assoc($rsconcelhos));
  $rows = mysql_num_rows($rsconcelhos);
  if($rows > 0) {
      mysql_data_seek($rsconcelhos, 0);
 $row_rsconcelhos = mysql_fetch_assoc($rsconcelhos);
  }
?>
</select>
  </p>
  <p>
    <select name="idfreg" id="idfreg">
      <option value="value">Escolhe a freguesia</option>
      <?php
do {  
?>
      <option value="<?php echo $row_rsfreguesias['idfreg']?>"><?php echo $row_rsfreguesias['freg']?></option>
      <?php
} while ($row_rsfreguesias = mysql_fetch_assoc($rsfreguesias));
  $rows = mysql_num_rows($rsfreguesias);
  if($rows > 0) {
      mysql_data_seek($rsfreguesias, 0);
 $row_rsfreguesias = mysql_fetch_assoc($rsfreguesias);
  }
?>
    </select>
  </p>
</form>
<form name="form5" method="post" action="">

  <input name="textfield" type="text" id="textfield" value="<?php echo $_REQUEST['iddist'];?>">
 <input name="textfield" type="text" id="textfield" value="<?php echo $_REQUEST['idconc'];?>">
</form>
</body>
</html>
<?php
mysql_free_result($rsfreguesias);

mysql_free_result($rsconcelhos);
?>


Sem comentários: