PHP - listas multiples php

 
Vista:

listas multiples php

Publicado por isa (11 intervenciones) el 07/10/2007 20:32:47
hola amigos tengo un problema con un par de listas: Sucede que tengo dos lista, en una de ellas tengo las materias (Ingles, matematicas, español, etc) y la otra lista esta vacia, e ella pongo las materias que son seleccionadas.
Las amterias de la lista uno, las traigo de una base de datos.
Lo que quiero hacer y no se como, es capturar las materias que han sido seleccionadas y guardarlas en mi base de datos.
Adjunto mi codigo el cual funciona para pasar las materias de una lista a la otra, lo que me falta es capturarlas y guardarlas, COMO LO HAGO????

Mira este es mi codigo, el funciona perfectamente pasando las materias de una lista a la otra, el problema esta en que no se como recoger las materias seleccionadas y guardarlas una por una en la base de datos.

Gracias por tu ayuda

>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">

<!--


// Control flags for list selection and sort sequence

// Sequence is on option value (first 2 chars - can be stripped off in form processing)

// It is assumed that the select list is in sort sequence initially

var singleSelect = true; // Allows an item to be selected once only

var sortSelect = true; // Only effective if above flag set to true

var sortPick = true; // Will order the picklist in sort sequence


// Initialise - invoked on load

function initIt() {

var selectList = document.getElementById("SelectList");

var pickList = document.getElementById("PickList");

var pickOptions = pickList.options;

pickOptions[0] = null; // Remove initial entry from picklist (was only used to set default width)

selectList.focus(); // Set focus on the selectlist

}


// Adds a selected item into the picklist

function addIt() {

var selectList = document.getElementById("SelectList");

var selectIndex = selectList.selectedIndex;

var selectOptions = selectList.options;

var pickList = document.getElementById("PickList");

var pickOptions = pickList.options;

var pickOLength = pickOptions.length;

// An item must be selected

if (selectIndex > -1) {

pickOptions[pickOLength] = new Option(selectList[selectIndex].text);

pickOptions[pickOLength].value = selectList[selectIndex].value;

// If single selection, remove the item from the select list

if (singleSelect) {

selectOptions[selectIndex] = null;

}

if (sortPick) {

var tempText;

var tempValue;

// Sort the pick list

while (pickOLength > 0 && pickOptions[pickOLength].value < pickOptions[pickOLength-1].value) {

tempText = pickOptions[pickOLength-1].text;

tempValue = pickOptions[pickOLength-1].value;

pickOptions[pickOLength-1].text = pickOptions[pickOLength].text;

pickOptions[pickOLength-1].value = pickOptions[pickOLength].value;

pickOptions[pickOLength].text = tempText;

pickOptions[pickOLength].value = tempValue;

pickOLength = pickOLength - 1;

}

}

}

}


// Deletes an item from the picklist

function delIt() {

var selectList = document.getElementById("SelectList");

var selectOptions = selectList.options;

var selectOLength = selectOptions.length;

var pickList = document.getElementById("PickList");

var pickIndex = pickList.selectedIndex;

var pickOptions = pickList.options;

if (pickIndex > -1) {

// If single selection, replace the item in the select list

if (singleSelect) {

selectOptions[selectOLength] = new Option(pickList[pickIndex].text);

selectOptions[selectOLength].value = pickList[pickIndex].value;

}

pickOptions[pickIndex] = null;

if (singleSelect && sortSelect) {

var tempText;

var tempValue;

// Re-sort the select list

while (selectOLength > 0 && selectOptions[selectOLength].value < selectOptions[selectOLength-1].value) {

tempText = selectOptions[selectOLength-1].text;

tempValue = selectOptions[selectOLength-1].value;

selectOptions[selectOLength-1].text = selectOptions[selectOLength].text;

selectOptions[selectOLength-1].value = selectOptions[selectOLength].value;

selectOptions[selectOLength].text = tempText;

selectOptions[selectOLength].value = tempValue;

selectOLength = selectOLength - 1;



}

}

}

}

function saveMe() {
var strValues = "";
var boxLength = document.choiceForm.choiceBox.length;
var count = 0;
if (boxLength != 0) {
for (i = 0; i < boxLength; i++) {
if (count == 0) {
strValues = document.choiceForm.choiceBox.options[i].value;
}
else {
strValues = strValues + "," + document.choiceForm.choiceBox.options[i].value;
}
count++;
}
}
if (strValues.length == 0) {
alert("No has hecho ninguna selección");
}
else {
alert("Aquí están los valores seleccionados:\r\n" + strValues);
}
}

-->

</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Kathi O'Shea ( Kathi.O'[email protected]) -->
<!-- Web Site: http://www.web-savant.com/users/kathi/asp -->

<!-- Begin
function moveOver()
{
var boxLength = document.choiceForm.choiceBox.length;
var selectedItem = document.choiceForm.available.selectedIndex;
var selectedText = document.choiceForm.available.options [selectedItem].text;
var selectedValue = document.choiceForm.available.options[selectedItem].value;
var i;
var isNew = true;
if (boxLength != 0) {
for (i = 0; i < boxLength; i++) {
thisitem = document.choiceForm.choiceBox.options [i].text;
if (thisitem == selectedText) {
isNew = false;
break;
}
}
}

if (isNew) {
newoption = new Option(selectedText, selectedValue, false, false);
document.choiceForm.choiceBox.options[boxLength] = newoption;
}
document.choiceForm.available.selectedIndex=-1;
}
function removeMe() {
var boxLength = document.choiceForm.choiceBox.length;
arrSelected = new Array();
var count = 0;
for (i = 0; i < boxLength; i++) {
if (document.choiceForm.choiceBox.options[i].selected) {
arrSelected[count] = document.choiceForm.choiceBox.options[i].value;
}
count++;
}
var x;
for (i = 0; i < boxLength; i++) {
for (x = 0; x < arrSelected.length; x++) {
if (document.choiceForm.choiceBox.options[i].value == arrSelected[x]) {
document.choiceForm.choiceBox.options[i] = null;
}
}
boxLength = document.choiceForm.choiceBox.length;
}
}
function saveMe() {
var strValues = "";
var boxLength = document.choiceForm.choiceBox.length ;
var count = 0;
if (boxLength != 0) {
for (i = 0; i < boxLength; i++) {
if (count == 0) {
strValues = document.choiceForm.choiceBox.options[i].value;

}
else {
strValues = strValues + "," + document.choiceForm.choiceBox.options[i].value;
}
count++
}
}

if (strValues.length == 0) {
alert("No has hecho ninguna selección");
}
else {
alert("Aquí están los valores seleccionados:\r\n" + strValues);

}
}

// End -->

</script>
<form name="choiceForm" method="post" action="prueva.php">

//aqui traigo las materias de la base de datos <?php

$result = mysql_query("select namn_amne from amne WHERE (namn_amne > '')", $link);
echo "<SELECT name='available'size='6' onchange='moveOver()';>";
while($row = mysql_fetch_row($result)) {
echo "<OPTION value='$row[0]'>$row[0] </OPTION> ";
}
echo "</SELECT>"

?>
<input name="button2" type="button" style="font-size: 8pt" onClick="removeMe();" value="Eliminar">
<?php
//aqui pongo las materias seleccionadas
echo "<SELECT name='choiceBox'size='6'>";

echo "<OPTION value='$row[0]'>$row[0] </OPTION> ";

echo "</SELECT>"

?>
</p>

<p>
<input name="button22" type="button" style="font-size: 8pt" onClick="saveMe();" value="Obtener valores seleccionados">
</p>
<p> </p>
<p><!-- This form is inoperational! Provisional users can't create forms NAME="theform" ID="theform" onSubmit="return false" -->
</p>

<input name="Submit" type="submit" value="Spara">
</form>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:listas multiples php

Publicado por eduardo (11 intervenciones) el 08/10/2007 22:36:53
ya tienes casi todo listo lo que tienes es que recuperar de la lista cuales fueron las seleccionadas

es decir seria algo mo esto:

$choiceBox = $_GET["choiceBox "];
for ($i = 0; $i <= count($choiceBox); $i++) {
echo $choiceBox[$i]."</BR>";
}

pero antes debes hacer lo siguiente
1._debes declarar el select de la forma

echo "<SELECT name='choiceBox[]' id='choiceBox' size='6' multiple='true'>";

2._ademas coloca el boton no como tipo submit sino como button

<input name="Submit" type="button" value="Spara" onclick="hacerSubmit()">

y la function javascript es :

function hacerSubmit(){
var indiceOrder=0;
for(indiceOrder;indiceOrder<document.choiceForm.choiceBox.length;indiceOrder++){
document.choiceForm.choiceBox.options[indiceOrder].selected=true;
}
document.choiceForm.submit();
}

con esto puedes obtener cuales son las materias seleccionadas.

ATTE:
Eduardo S
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:listas multiples php

Publicado por isa (11 intervenciones) el 22/10/2007 14:04:30
Hola Eduardo , efectivamente me funciono el codigo que me pasaste, ahora lo que no se hacer es meterlas a la base de datos pues tengo este codigo y no me funciona.

for ($i = 0; $i <= count($choiceBox); $i++) {
$query2="insert into amne_v (amne_v) values ('$choiceBox[$i].'WHERE (epnummer=($epnummer)";

// $query3="UPDATE amne_v SET $choiceBox[$i]. WHERE (epnummer=($epnummer)";
echo $choiceBox[$i].;
echo "</BR>";

No se cual utilizar query2 o query3 aunque ninguno de los dos me funciona

Podrias ayudarme??
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar