PHP - ayuda con listas

 
Vista:

ayuda con listas

Publicado por isabel (11 intervenciones) el 01/10/2007 16:37:17
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:ayuda con listas

Publicado por Diego Romero (1450 intervenciones) el 01/10/2007 19:45:26
El tag SELECT solo sirve para pasar un solo valor de la lista. Es decir, se le muestra al usuario una serie de valores y se le pide que seleccione uno de ellos (o ninguno), no sirve para capturar listas de valores.

Ahora bien, lo que puedes hacer es un "truco" con PHP. Esta línea:

<SELECT name='choiceBox'size='6'>

Escríbela así:

<SELECT name='choiceBox[]' size='6'>

Pero me temo que tendrás que modificar el código JavaScript para que acepte ese nuevo nombre, o ponerle un id a ese tag y referenciarlo por id en JavaScript en vez de por nombre.
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