PHP - problemas con listas y variables

 
Vista:

problemas con listas y variables

Publicado por Isabel (11 intervenciones) el 24/09/2007 19:44:36
Hola todos, tengo un problema con unas listas y el traspaso de variables espero me puedan colaborar.
El caso es el siguiente:
Tengo en una pagina llamada X.php un formulario para el ingreso de datos de un estudiante, en esta pagina tengo dos listas , en una de ella tengo las materias y en la otra debo pasar las materias que se elsijan en la lista uno . asi por ejemplo:

MATERIAS SELECCIONADAS
Español Ingles
Ingles Religion
Matematicas
Religion

El problema es que no se como hacer para pasar las materias seleccionadas a la lista vacia, y luego pasarlas a la pagina Y.php en la que guardo las materias seleccionadas en una base de datos MYSQL.

Podrian ayudarme?

Yo tengo este codigo en Javascript pero el problema es que no se como capturar las materias escogidas y guardarlas una por una en el registro que corresponda en la base de datos.


<!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 ( selectedText=="Svenska A"){
var xy="svenska A"
alert (xy);
<?php

$xy="Svenska A";

?>

}
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>

<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
body {
background-image: url(gris.png);
}
.style2 {color: #FFFFFF}
-->
</style></head>

<body>
<table width="755" border="8" align="center" bordercolor="#FD802C" bgcolor="#FD802C">
<tr>
<td bgcolor="#FFFFFF"><p align="right"><span class="title"><span class="style2">.</span></span><span class="style2">....</span></p>
<p align="center" class="title">REGISTRERA</p>
<form name="choiceForm" method="post" action="prueva.php">
<p> </p>
<p> </p>

<table border=0 width="414">

<tr>
<td valign="top" width=180> <br>
<select name="available" size=10 onchange="moveOver();">
<option value= "Svenska A">Svenska A
<option value= "Svenska B">Svenska B
<option value=3>Actualizaciones de productos
<option value=4>Especificaciones de productos
<option value=5>Historia de pedidos
<option value=6>Estado del pedido
<option value=7>Contactos
<option value=8>Calendario de eventos
<option value=9>Planificación
<option value=10>Notas
</select></td>
<td valign="top" width=43><p> </p>
<p> </p>
<p>
<input name="button2" type="button" style="font-size: 8pt" onClick="removeMe();" value="Eliminar">
</p></td>
<td valign="top" width="151"><font size="2">:</font><br>
<select multiple name="choiceBox" style="width:150;" size="10">
</select></td>
</tr>
<tr>
<td colspan=3 height=10>
<input name="button2" type="button" style="font-size: 8pt" onclick="saveMe();" value="Obtener valores seleccionados"></td>
</tr>
</table>
<p> </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>

</table>
</body>
</html>
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:problemas con listas y variables

Publicado por Nicolás (154 intervenciones) el 24/09/2007 21:49:20
Demasiado largo... te paso parte de algo que desarrollé... me parece que es un poco mas sencillo.


<?
include("restringido.php");
include("conex.php");
include("../funciones.php");

// INICIALIZO VARIABLES
$mensaje = "";

// TENGO QUE VER SI AGREGARON UN SERVICIO NUEVO
if (isset($_POST['Submit'])) {
[....]
$servicios = $_POST['serv'];

$mensaje = "<span class=\"alerta\">El inmueble ha sido agregado con exito</span>";

$consulta = "INSERT INTO inmuebles ( tipoPropiedad , ubicacion_provincia , ubicacion_ciudad , ubicacion_barrio , ubicacion_calle , ubicacion_provincia_ac , ubicacion_ciudad_ac , ubicacion_barrio_ac , ubicacion_calle_ac , ubicacion_numero , ubicacion_piso , departamento , antiguedad , condicion , superficie_total , superficie_cubierta , cantidad_ambientes , cantidad_dormitorios , cantidad_banios , orientacion_frente , expensas ) VALUES ( '$tipoPropiedad', '$ubicacion_provincia', '$ubicacion_ciudad', '$ubicacion_barrio', '$ubicacion_calle', '$ubicacion_provincia_ac', '$ubicacion_ciudad_ac', '$ubicacion_barrio_ac', '$ubicacion_calle_ac', '$ubicacion_numero', '$ubicacion_piso', '$departamento', '$antiguedad', '$condicion', '$superficie_total', '$superficie_cubierta', '$cantidad_ambientes', '$cantidad_dormitorios', '$cantidad_banios', '$orientacion', '$expensas' ) ";
if (!mysql_query($consulta)) {
$mensaje = "<span class=\"alerta\">Ha ocurrido un error en el servidor.<br />El inmueble NO ha sido agregado</span>";
} else {
$id_inmueble = mysql_insert_id();
// AGREGO LOS SERVICIOS
$servicios = explode("|", $servicios);
for ($i = 0; $i < count($servicios); $i++) {
mysql_query("INSERT INTO servicios_inmuebles (servicio, inmueble) VALUES (".$servicios[$i].", $id_inmueble)");
}
}
}

?>
<html>
<head>
<title>Administrador</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="estilo.css" rel="stylesheet" type="text/css">
</head>

<script language="JavaScript1.2">

function agregarServicio() {
if (document.formulario.select.selectedIndex == -1) {
alert("Seleccione el servicio que desea agregar");
} else {
document.formulario.servicios.options.add(new Option(document.formulario.select.options[document.formulario.select.selectedIndex].text, document.formulario.select.options[document.formulario.select.selectedIndex].value));
}
}

function quitarServicio() {
if (document.formulario.servicios.selectedIndex == -1) {
alert("Seleccione el servicio que desea quitar");
} else {
document.formulario.servicios.remove(document.formulario.servicios.selectedIndex);
}
}

function cargar() {
valor = "";
for (i = 0; i < document.formulario.servicios.options.length; i++) {
valor = valor + "|" + document.formulario.servicios.options[i].value;
}
document.formulario.serv.value = valor;
}

</script>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onLoad="MM_preloadImages('Imagenes/boton_salir_move.jpg')">

<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><?
include("tabla_superior.php");
?></td>
</tr>
<tr>
<td height="229" align="center" valign="top"><?php echo $mensaje; ?>
<h1>Agregar Inmueble</h1>
<form method="post" enctype="multipart/form-data" name="formulario" target="_self" id="formulario" onSubmit="cargar();">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="center" valign="top"><table width="372" border="0" cellspacing="0" cellpadding="2" style="border:solid 1px #F4B78A">
<tr>
<td height="38" colspan="2" align="center" valign="middle" bgcolor="#F4B78A"><strong>Agregar Inmueble</strong></td>
</tr>
<tr>
<td width="132" align="right">Tipo:</td>
<td width="230" align="left"><select name="tipo" class="ancho220" id="tipo">
<?php

$tiposPropiedad = mysql_query("SELECT * FROM tiposPropiedad");
while ($tipoPropiedad = mysql_fetch_array($tiposPropiedad)) {
echo " <option value=\"".$tipoPropiedad['id_tipoPropiedad']."\">".$tipoPropiedad['tipoPropiedad']."</option>\n";
}

?>
</select> </td>
</tr>

[...................]

<table width="372" border="0" cellspacing="0" cellpadding="2" style="border:solid 1px #F4B78A">
<tr>
<td height="38" align="center" valign="middle" bgcolor="#F4B78A"><strong>Servicios</strong></td>
</tr>

<tr>
<td align="center"><table width="366" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="150"><select name="select" size="8" style="width:150px;">
<?php

$servicios = mysql_query("SELECT * FROM servicios");
while ($servicio = mysql_fetch_array($servicios)) {
echo " <option value=\"".$servicio['id_servicio']."\">".$servicio['servicio']."</option>\n";
}

?>
</select> </td>
<td width="66" align="center"><input type="button" name="agregar" value="Agregar" onClick="agregarServicio();" style="width:66px;">
<br>
<input type="button" name="quitar" value="Quitar" onClick="quitarServicio();" style="width:66px;"></td>
<td width="150"><select name="servicios" size="8" style="width:150px;">
</select>
<input type="hidden" name="serv" value=""></td>
</tr>
</table></td>
</tr>
</table>
<p>
<input type="submit" name="Submit" value=" Agregar ">
</p></td>
</tr>
</table>

</form>
<p> </p></td>
</tr>
<tr>
<td height="83" background="imagenes/abajo.jpg"> </td>
</tr>
</table>
<p> </p>
<p> </p>
</body>
</html>


Aqui utilizo dos combos y un input hidden el cual se cargará con los servicios seleccionados. Espero que te sirva.
Suerte
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