1 (edited by m0ly 2009-03-02 9:39:30 PM)

Topic: Multiple select

Hello. Nowhere could not find this example, therefore, written myself and want to show you.
PunBB bbcode test
Examples
------------------------------------------------------------------------------------------------------------------
HTML:

<select name="region" id="dat" onchange="xajax_region(this.value);return false;">
<option value="0">Please select</option><option value="1">Russia</option><option value="2">Germany</option>
</select>
<div id="reg"><select name="city" class="select_c">
<option value="">Please select</option>
</select></div>

Registry xajax function to "xajax.comand.php"

<?
require_once ("xajax/xajax_core/xajax.inc.php");
$xajax = new xajax("xajax.suite.php");
$xajax->registerFunction("region"); 
?>

and coding function to "xajax.suite.php"

<?
include("xajax.comand.php");
function region($country) {
    $objResponse = new xajaxResponse();
    $objResponse->setCharacterEncoding('windows-1251');
    $objResponse->assign("reg","innerHTML","<select name='city' class='select_c'><option value=''>Please wait ...</option></select>");
    // ----
    $country = (isset($country)) ? mysql_real_escape_string($country) : '';  // <select name="region" id="dat" 
    // $country (int) - intval() function
    if($country == '0') { // <option value="0">
    $objResponse->assign("reg","innerHTML","<select name='city' class='select_c'><option value=''>&larr; Please select</option></select>");
    } else { // <option value="id">
    $sql_ = mysql_query("SELECT `id` FROM `global_city` WHERE `id_region`='".$country."' LIMIT 1");
    if (mysql_num_rows($sql_)=='1') {
    $reg = "<select name='city' class='select_c'><option value=\"0\">Select ...</option>";
    $sql3 = mysql_query("SELECT * FROM `global_city` WHERE `id_region`='".$country."' ORDER BY `city`");
    while($row2 = mysql_fetch_array($sql3)){
    $reg .= "<option value=\"".$row2['id']."\">".$row2['city']."</option>";
    }
    $reg .= "</select>";
                $objResponse->assign("reg","innerHTML","$reg");
    } elseif(mysql_num_rows($sql_)!=1) {
    $objResponse->assign("reg","innerHTML","<select name='city' class='select_c'><option value=''>No data</option></select>");
    }
    }
    return $objResponse;
}
$xajax->processRequest();
?>

MySql Table

CREATE TABLE `global_city` (
  `id` int(100) NOT NULL auto_increment,
  `id_region` int(100) NOT NULL,
  `city` varchar(80) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM;

INSERT INTO global_city (id, id_region, city) VALUES (1, 1, "Moscov");
INSERT INTO global_city (id, id_region, city) VALUES (2, 1, "Peterburg");
INSERT INTO global_city (id, id_region, city) VALUES (3, 1, "Volgograd");
INSERT INTO global_city (id, id_region, city) VALUES (4, 2, "Berlin");
INSERT INTO global_city (id, id_region, city) VALUES (5, 2, "Munich");
INSERT INTO global_city (id, id_region, city) VALUES (6, 2, "Köln");

Re: Multiple select

hmm ok, but i would change something :

include("xajax.comand.php");
function region($country) {
    $objResponse = new xajaxResponse();
    $objResponse->setCharacterEncoding('windows-1251');
    //country will always exist, so there is no need to check if it's set..
    //the second thing is if you're sending int value, there is just one simple check :
    $country = intval($country); //if something is wrong, country will be 0
    $isError = true;
    if($country!=0) { // <option value="id">
            //here there is no need to check this out... cause, if user specified something that doesn't exists... then nothing wil be changed
            $reg = "<select name='city' class='select_c'><option value=\"0\">Select ...</option>";
            $sql3 = mysql_query("SELECT * FROM `global_city` WHERE `id_region`='".$country."' ORDER BY `city`");
            $isError = !(bool)mysql_num_rows($ssql3);
            while($row2 = mysql_fetch_array($sql3)){
                    $reg .= "<option value=\"".$row2['id']."\">".$row2['city']."</option>";
            }
            $reg .= "</select>";
            $objResponse->assign("reg","innerHTML","$reg");
    }
    if($isError)
    {
            $objResponse->alert('an alert here that selection is not valid');
    }
    return $objResponse;
}

$xajax->processRequest();

the second thing is your db
id` int(100) NOT NULL auto_increment, <- int 100 this will not be: a digit with 100 of zeros big_smile...
instead it will be just int(11) (11 digits will be shown) the number in the () is not the int length it's just for display purposes...
if you need something bigger than int just use: bigint smile
hope it helped you
p.s.
you should also consider adding index on the id_region column

Re: Multiple select

hello how do I copy it and the  connection worked and I  also need help for an example of autocomplete with  xajax


thanks

Re: Multiple select

I can read well in English but I write very bad English but I thought this would help those who do not understand English. Again sorry for my english.

<?
//llamamos a la clase de xajax
require('xajax_core/xajax.inc.php');


//creamos el objeto
$xajax = new xajax();

//Registramos la funciones que queremos llamar desde nuestro codigo xajax
$xajax->registerFunction("miFuncion");
//$xajax->debugOn();

//Creamos la funcion que recibira un valor cuando el usuario pulse cualquier tecla dentro del TextBox
function miFuncion($arg)
{
$objResponse = new xajaxResponse();

//Conectamos con la db
$link = mysql_connect('localhost','xajaxuser','xxxxxx');

//Abrimos la db para realizar las consultas
if(!@mysql_select_db('pruebadb',$link)){
    print "No hay acceso a la db";
    exit();
    }

/*
Si hemos abierto la db consultamos la tbl
En este ejemplo dejo una consulta simple para ver su funcionamiento
*/
$sqlquery = mysql_query("SELECT CamposConsultar FROM tblConsultar WHERE descripcion LIKE '%$arg%'");

$newContent = "Elige uno<br>";

while($datos=mysql_fetch_array($sqlquery)){
    $newContent .= $datos[descripcion].'<br>';
    $objResponse->assign("divIDamostrar","innerHTML", $newContent);
}


//Devuelve un resultado del objeto
return $objResponse;
}

$xajax->processRequest();


?>
<!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" />
<? $xajax->printJavascript(); ?>
<title>Ejemplo Sencillo TextBox</title>
</head>

<body>
<input name="textobuscar" type="text" id="textobuscar" onkeydown="xajax_miFuncion(this.value);"/>
<div id="divIDamostrar"></div>
</body>
</html>