Re: Selección múltiple, PHP y Javascriipt
| From: | Ferran Orsola | Date: | Mon, 19 Jul 2004 10:41:32 +0000 |
| Subject: | Re: Selección múltiple, PHP y Javascriipt | ||
| Groups: | php.general.es | ||
| Request: | Send a blank email to general-es+get-50296@lists.php.net to get a copy of this message | ||
Mira esto que te mando, quizas te ayude a simplificar tu funcion send:
Crear arrays de elementos HTML:
(PHP que crea el arrray en funcion de un campo hidden que especifica la
longitud):
<table width="678" border="0" cellpadding="2"
cellspacing="2" id="table">
<tr>
<td valign="top"><?=$values['drug']?></td>
<td valign="top"><?=$values['route']?></td>
<td valign="top"><?=$values['condition']?></td>
<td valign="top"><?=$values['duration']?></td>
<td colspan=2 valign="top"><?=$values['stop']?></td>
</tr>
<?php
if (isset($_POST['list_items'])) {$files=$_POST['list_items'];}
else {$files=1;}
for ($i=0;$i<$files;$i++) {
$yes="";$no="";
if ($_POST['stop'][$i]) { $yes = 'CHECKED';}
if (!$_POST['stop'][$i]) { $no = 'CHECKED';}
foreach (array('mouth','injection','suppository') as $key) {
if ($key == $_POST['route'][$i]) { $route[$key] = "SELECTED";}
else {$route[$key] = "";}
}
echo <<<__DRUG__
<tr>
<td><input type='text' name='drug[$i]'
value='{$_POST['drug'][$i]}'
onchange="submit();"></td>
<td><select name='route[$i]'>
<option value=''>select</option>
<option value='mouth' {$route['mouth']}>mouth</option>
<option value='injection'
{$route['injection']}>injection</option>
<option value='suppository'
{$route['suppository']}>suppository</option>
</td>
<td><input type='text' name='condition[$i]'
value='{$_POST['condition'][$i]}'></td>
<td><input type='text' name='duration[$i]'
value='{$_POST['duration'][$i]}'></td>
<td><input type='radio' name='stop[$i]' value='1'
$yes>{$values['yes']}<input type='radio' name='stop[$i]'
value='0'
$no>{$values['no']}</td>
<td><input type='button' value='Delete'
onclick="deleteItem($i)"></td>
</tr>
__DRUG__;
}
?>
<tr>
<td colspan=6 align='center'>
<input type='hidden' name='list_items'
value="<?=(isset($_POST['list_items']))?$_POST['list_items']
:1?>">
<input type='button' value='Add names more drugs or medicines '
onclick="addItem()">
</td>
</tr>
</table>
Resultado HTML:
<table width="678" border="0" cellpadding="2"
cellspacing="2" id="table">
<tr>
<td valign="top">Drug or Medicine (Brand if known)</td>
<td valign="top">Route (by mouth injection or as suppository)</td>
<td valign="top">For what condition was this drug prescribed?</td>
<td valign="top">For how long had you been taking this drug before your
suspected drug reaction</td>
<td colspan=2 valign="top">Did you stop this drug after your suspected
drug reaction?</td>
</tr>
<tr>
<td><input type='text' name='drug[0]' value=''
onchange="submit();"></td>
<td><select name='route[0]'>
<option value=''>select</option>
<option value='mouth' >mouth</option>
<option value='injection' >injection</option>
<option value='suppository' >suppository</option>
</td>
<td><input type='text' name='condition[0]'
value=''></td>
<td><input type='text' name='duration[0]'
value=''></td>
<td><input type='radio' name='stop[0]' value='1'
>Yes<input type='radio'
name='stop[0]' value='0' CHECKED>No</td>
<td><input type='button' value='Delete'
onclick="deleteItem(0)"></td>
</tr> <tr>
<td><input type='text' name='drug[1]' value=''
onchange="submit();"></td>
<td><select name='route[1]'>
<option value=''>select</option>
<option value='mouth' >mouth</option>
<option value='injection' >injection</option>
<option value='suppository' >suppository</option>
</td>
<td><input type='text' name='condition[1]'
value=''></td>
<td><input type='text' name='duration[1]'
value=''></td>
<td><input type='radio' name='stop[1]' value='1'
>Yes<input type='radio'
name='stop[1]' value='0' CHECKED>No</td>
<td><input type='button' value='Delete'
onclick="deleteItem(1)"></td>
</tr> <tr>
<td colspan=6 align='center'>
<input type='hidden' name='list_items' value="2">
<input type='button' value='Add names more drugs or medicines '
onclick="addItem()">
</td>
</tr>
</table>
Acceder a los arrays mediante Javascript:
<script language="javascript">
function addItem() {
document.forms[0].list_items.value++;
document.forms[0].submit();
}
function deleteItem(file) {
total = document.forms[0].list_items.value;
document.forms[0].action += "&delete=" + file;
document.forms[0].submit();
}
function send() {
var send = true;
var message = "Please check this values:\n";
var form = document.forms[0];
var limit = form.list_items.value;
for (var i=0; i<limit;i++) {
if (form.elements['drug[' + i + "]"].value.length ==0) {
message += "- " + (i+1) + "th Drug specified\n";
send = false;
}
if (form.elements['route[' + i + "]"].selectedIndex==0) {
message += "- " + (i+1) + "th Route specified\n";
send = false;
}
if (form.elements['condition[' + i + "]"].value.length ==0) {
message += "- " + (i+1) + "th Condition specified\n";
send = false;
}
if (form.elements['duration[' + i + "]"].value.length ==0) {
message += "- " + (i+1) + "th Duration specified\n";
send = false;
}
}
if (form.restart['0'].checked) {
var check = false;
for (var i=0; i<limit;i++) {
check = check || form.elements['restart_specification[' + i +
"]"].checked;
}
if (!check) {
message += "- Must select at least one restarted drug.\n";
send = false;
}
}
if (send) {
form.action = "saveSection.php?document=Section1";
form.submit();
} else {
alert (message);
}
}
</script>
Acceder a los arrays mediante PHP:
// Delete rows if total and delete exists
if (isset($_GET['delete'])) {
for ($i=$_GET['delete']; $i<count($_POST['drug']);$i++) {
$_POST['drug'][$i] = $_POST['drug'][$i+1] ;
$_POST['route'][$i] = $_POST['route'][$i+1] ;
$_POST['condition'][$i] = $_POST['condition'][$i+1] ;
$_POST['duration'][$i] = $_POST['duration'][$i+1] ;
$_POST['stop'][$i] = $_POST['stop'][$i+1] ;
}
$_POST['list_items'] --;
}
"Unknown Sender" <bogus@news.php.net> wrote in message
news:000c01c2d7b9$bc743850$aa78fea9@nicolas...
Listeros:
Como veo que nadie ha respondido a mi pregunta, y luego de buscar por muchos
lados, he resulto mi problema. Les mando mi auto respuesta, espero que a
alguien le sirva.
Son dos selecciones múltiples, de las cuales se puede pasar valores de una a
otra y envía los valores existentes en la lista2 por post.
Esta armado para MySQL.
Si alguien sabe armar esto de forma mas eficiente, agradecería un mail.
Saludos a todos.
<script language="javascript">
function exit_abm() { window.location="listar_ccostos.php"; }
function move(fbox, tbox) {
var arrFbox = new Array();
var arrTbox = new Array();
var arrLookup = new Array();
var i;
for (i = 0; i < tbox.options.length; i++) {
arrLookup[tbox.options[i].text] = tbox.options[i].value;
arrTbox[i] = tbox.options[i].text;
}
var fLength = 0;
var tLength = arrTbox.length;
for(i = 0; i < fbox.options.length; i++) {
arrLookup[fbox.options[i].text] = fbox.options[i].value;
if (fbox.options[i].selected && fbox.options[i].value != "") {
arrTbox[tLength] = fbox.options[i].text;
tLength++;
}
else {
arrFbox[fLength] = fbox.options[i].text;
fLength++;
}
}
arrFbox.sort();
arrTbox.sort();
fbox.length = 0;
tbox.length = 0;
var c;
for(c = 0; c < arrFbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrFbox[c]];
no.text = arrFbox[c];
fbox[c] = no;
}
for(c = 0; c < arrTbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrTbox[c]];
no.text = arrTbox[c];
tbox[c] = no;
}
}
function option_compress(box, field) {
field.value = "";
for (var i=0; i<box.options.length; i++) {
if (i>0)
field.value += ",";
field.value += box.options[i].value;
}
}
</script>
<FORM name="form " action="archivo.php" method="post">
<?
$selec_valor=$xxx; // nombre
variable que toma el valor
$selec_nombre="xxx"; // nombre del campo que pasa
las variables
$lista1="xxx "; // nombre de la primera
selección míltiple
$lista2="xxx"; // nombre
de la segunda selección míltiple
$selec_base="xxx"; // base a ser
consultada
$selec_campo_id="xxx"; // campo id de la
base consultada - valores multi select
$selec_campo_valor="xxx"; // campo a mostrar en
el desplegable
$selec_campo_orden="xxx"; // campo de orden del
desplegable
$selec_excluir="xxx"; //campos a ser excluidos
seleccion_multiple($selec_valor,$selec_nombre,$lista1,$lista2,$selec_base,$s
elec_campo_id,$selec_campo_valor,$selec_campo_orden,$selec_excluir);
$javascript_accion = "option_compress(this.form.".$lista2.",
this.form.".$selec_nombre."); return true;";
?>
<input type="submit" name="envio" value="Guardar"
onclick="<?=
$javascript_accion ?>">
</form>
<?
function
seleccion_multiple($selec_valor,$selec_nombre,$lista1,$lista2,$selec_base,$s
elec_campo_id,$selec_campo_valor,$selec_campo_orden,$selec_excluir) {
include( '../inc/db.php' );
?>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#CCCCCC">
<div align="center">Opciones</div></td>
<td><div align="center"></div></td>
<td bgcolor="#CCCCCC"><div
align="center">Seleccionados</div></td>
</tr>
<tr>
<td bgcolor="#CCCCCC">
<?
$selec_valor_elem = explode (",", $selec_valor);
$selec_valor_count = count ($selec_valor_elem);
if($selec_valor==""){
$selec_valor_busq = $selec_campo_id." = 0 ";
$selec_valor_busq_n = $selec_campo_id." != 0 ";
}else{
$selec_valor_busq = "";
for ($i = 0; $i < count ($selec_valor_elem); $i++) {
if($i<(count ($selec_valor_elem)-1)){
$selec_valor_busq = $selec_valor_busq."
".$selec_campo_id." = ".$selec_valor_elem[$i]." OR ";
}else{
$selec_valor_busq = $selec_valor_busq."
".$selec_campo_id." = ".$selec_valor_elem[$i]." ";
}
}
$selec_valor_busq_n = str_replace ("=", "!=", $selec_valor_busq);
$selec_valor_busq_n = str_replace ("OR", "AND", $selec_valor_busq_n);
}
?>
<select multiple size="5" name="<?= $lista1 ?>"
style="width:150">
<?
$consulta="SELECT ".$selec_campo_id.",".$selec_campo_valor." FROM
".$selec_base." WHERE ".$selec_excluir." AND ".$selec_valor_busq_n."
ORDER
BY ".$selec_campo_orden;
$result = mysql_query($consulta,$connect)
or die("Lectura de Bases de Datos (usuarios) fallida\n");
while($row = mysql_fetch_array($result)) {
?>
<option value="<?=$row[0]?>"><?=$row[0]?> <?=$row[1]?>
</option>
<? } ?>
</select></td>
<td><div align="center">
<input name="button" type="button"
onClick="move(this.form.<?= $lista2 ?>,this.form.<?= $lista1 ?>)"
value="<<">
<input name="button2" type="button"
onClick="move(this.form.<?= $lista1 ?>,this.form.<?= $lista2 ?>)"
value=">>">
</div></td>
<td bgcolor="#CCCCCC"> <select multiple size="5"
name="<?=
$lista2 ?>" style="width:150">
<?
$consulta="SELECT ".$selec_campo_id.",".$selec_campo_valor." FROM
".$selec_base." WHERE ".$selec_excluir." AND ".$selec_valor_busq."
ORDER BY
".$selec_campo_orden;
$result = mysql_query($consulta,$connect)
or die("Lectura de Bases de Datos (usuarios) fallida\n");
while($row = mysql_fetch_array($result)) {
?>
<option value="<?=$row[0]?>"><?=$row[0]?> <?=$row[1]?>
</option>
<? } ?>
</select>
<input name="<?= $selec_nombre ?>" type="hidden"
value="">
</td>
</tr>
</table>
<? } ?>