PHP - problema la recibir los datos de los list box

 
Vista:
Imágen de perfil de Hfr
Val: 36
Ha disminuido su posición en 52 puestos en PHP (en relación al último mes)
Gráfica de PHP

problema la recibir los datos de los list box

Publicado por Hfr (93 intervenciones) el 18/06/2016 16:48:09
Hola chicos.
bueno la otra vez cree una dos list box para paar datos entre ellos. pero estoy teneindo un problema cuando recibo los datos.
de los list box.

cuando paso del list 1 al list box 2 y envio los datos funciona bien,
pero cuado paso del list 1 al list box 2 y devuelvo deñl dos al 1 asuminedo que uno se equivoco y envio la opcion equivocada.


enhtonces si en total en lisbox 2 quedao 3 elementos
el for tan solo muestra un elemento y no los 3 o el total con el que halla quedado

1
2
3
4
for ($d=0; $d<count($dato); $d++)
{
	echo "dato nuermo".$d."  ".$dato[$d]."<br>";
}

y esto afecta un insert dinamico para guardar eso datos.



aquí les dejo el código completo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<html>
 
<head>
 
<SCRIPT language="javascript">
		function addRow(tableID) {
 
			var table = document.getElementById(tableID);
 
			var rowCount = table.rows.length;
			var row = table.insertRow(rowCount);
 
			var cell1 = row.insertCell(0);
			var element1 = document.createElement("input");
			element1.type = "checkbox";
			element1.name="chkbox[]";
			cell1.appendChild(element1);
 
 
				var cell2 = row.insertCell(1);
			cell2.innerHTML = rowCount + 1;
 
			var cell3 = row.insertCell(2);
			var element2 = document.createElement("input");
			element2.type = "text";
			element2.name = "mts[]";
			cell3.appendChild(element2);
 
		}
 
		function deleteRow(tableID) {
			try {
			var table = document.getElementById(tableID);
			var rowCount = table.rows.length;
 
			for(var i=0; i<rowCount; i++) {
				var row = table.rows[i];
				var chkbox = row.cells[0].childNodes[0];
				if(null != chkbox && true == chkbox.checked) {
					table.deleteRow(i);
					rowCount--;
					i--;
				}
 
 
			}
			}catch(e) {
				alert(e);
			}
		}
 
	</SCRIPT>
<script>
function listbox_move(listID,direction){
	var listbox=document.getElementById(listID);
	var selIndex=listbox.selectedIndex;if(-1==selIndex){alert("Please select an option to move.");
	return;
}
 
var increment=-1;if(direction=='up')
increment=-1;
else
increment=1;
if((selIndex+increment)<0||(selIndex+increment)>(listbox.options.length-1))
{
	return;
	}
var selValue=listbox.options[selIndex].value;
var selText=listbox.options[selIndex].text;listbox.options[selIndex].value=listbox.options[selIndex+increment].value
listbox.options[selIndex].text=listbox.options[selIndex+increment].text
listbox.options[selIndex+increment].value=selValue;listbox.options[selIndex+increment].text=selText;listbox.selectedIndex=selIndex+increment;
}
 
 
function listbox_moveacross(sourceID,destID){
	var src=document.getElementById(sourceID);
	var dest=document.getElementById(destID);
	for(var count=0;count<src.options.length;count++)
	{
		if(src.options[count].selected==true)
		{
	var option=src.options[count];
	var newOption=document.createElement("option");
	newOption.value=option.value;
	newOption.text=option.text;
	newOption.selected=true;
 
	try
	{
	dest.add(newOption,null);
	src.remove(count,null);
	}
	catch(error){
 
		dest.add(newOption);src.remove(count);
 
		}
 
count--;
}
}
}
function listbox_selectall(listID,isSelect)
{var listbox=document.getElementById(listID);
for(var count=0;count<listbox.options.length;count++)
{
	listbox.options[count].selected=isSelect;
	}
	}
 
</script>
</head>
 
 
<body>
 
<form ACTION ="XX.PHP" METHOD ="POST">
 
	<select id="s" size="10" multiple="m[]"  >
 
<option value="dato1">dato1</option>
<option value="dato2">dato2</option>
<option value="dato3">dato3</option>
<option value="dato4">dato4</option>
<option value="dato5">dato5</option>
<option value="dato6">dato6</option>
<option value="dato7">dato7</option>
<option value="dato8">dato8</option>
<option value="dato9">dato9</option>
 
</select>
 
	<a href="#" onclick="listbox_moveacross('s', 'd')">&gt;&gt;</a>
<a href="#" onclick="listbox_moveacross('d', 's')">&lt;&lt;</a>
<select id="d" size="10" multiple=""  name ="p[]" >
 
	</select>
	<br>
	<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
 
	<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
 
	<TABLE id="dataTable" >
		<TR>
			<TD><INPUT type="checkbox" name="chk"/></TD>
	<TD>1</TD>
			<TD> <INPUT type="text" /> </TD>
		</TR>
	</TABLE>
 
	<BR>
	<INPUT TYPE="submit" value="click">
	</form>
</body>
 
</html>
 
 
 
<?php
 
include ('php/con_mysql.php');
 
 
 
$link = new conexion();
$conn = $link->conectar();
 
 
$idnnja='1234567890';
$dato =$_POST['p'];
 
for ($d=0; $d<count($dato); $d++)
 
	{
		echo "dato nuermo".$d."  ".$dato[$d]."<br>";
	}
 
$metas = $_POST['mts'];
$realizacion ="JASLÑKDJASLKD";
 
 
 
 
 
$query ='INSERT INTO pl_vidasaludable (id_nnja,realizacion,';
 
for ($i=0, $size = count($dato); $i<$size;$i++){
    $query.= ' dato'.($i+1).', metas'.($i+1);
    if ( $size > $i+1 ){
        $query .= ',';
    }
}
$query .= ') VALUES ('.$idnnja.',\''.$realizacion.'\''; // aqui ingreso el id pero falta la coma
for ($i=0, $size = count($dato); $i<$size;$i++){
    if ($i == 0){ // como solo sera al inicio del array coloco como condicion que sea en el primer indice
        $query.= ', '; // aca ingreso la coma
    }
    $query .= '\''.$dato[$i].'\', \''.$metas[$i].'\'';
    if ( $size > $i+1 ){
        $query .= ', ';
    }
}
$query.= ');';
$inser=mysqli_query($conn,$query);
 
if ($inser)
 
	{
		echo "exito<BR>";
	}
 
	else {
 
		echo "<BR>no insertado<BR>";
	}
 
	echo $query;
 
 
 
?>
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
Imágen de perfil de kip
Val: 2.325
Plata
Ha disminuido 1 puesto en PHP (en relación al último mes)
Gráfica de PHP

problema la recibir los datos de los list box

Publicado por kip (877 intervenciones) el 18/06/2016 17:52:57
Hola, parece que el problema es de la funcion de javascript que mueve los datos de un select a otro:

1
2
3
4
5
6
7
if(src.options[count].selected==true)
{
var option=src.options[count];
var newOption=document.createElement("option");
newOption.value=option.value;
newOption.text=option.text;
newOption.selected=true;

Al parecer en la linea donde le das la propiedad true al atributo selected del nuevo option da problemas, ademas estas colocando mal los atributos al select:

1
2
<select id="s" size="10" multiple="m[]" > //no es asi
<select id="s" size="10" multiple name="m[]" > //asi es correcto

Ahora para solucionar el problema, te recomiendo crear un evento que al darle click al boton para enviar los datos al script php se seleccionen todos los option del select donde esta la info que necesitas, algo asi:

1
2
3
4
5
6
7
8
function seleccion(){
            var sel = document.getElementById('d');
            for (i = 0; i < sel.length; i++) {
                //if (sel[i].value == elemento) {
                sel[i].selected = true;
                //}
            }
        }

Y en el sumbit coloques en el atributo evento onclick llames a la funcion:

1
<INPUT TYPE="submit" value="click" onclick="seleccion();">

Prueba de esa forma, nos cuentas si se soluciona el problema.

Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
Imágen de perfil de Hfr
Val: 36
Ha disminuido su posición en 52 puestos en PHP (en relación al último mes)
Gráfica de PHP

problema la recibir los datos de los list box

Publicado por Hfr (93 intervenciones) el 20/06/2016 15:27:49
ola kip quedo bn la funcion de los list box.
ahora los campos dibujados
solo trae 7 de 9

dato nuermo de la meta0 ASDASD
dato nuermo de la meta1 ASD
dato nuermo de la meta2 ASDASD
dato nuermo de la meta3 ASD
dato nuermo de la meta4 SD
dato nuermo de la meta5 ASD
dato nuermo de la meta6 ASDASD
dato nuermo de la meta7 ASDASD

al imprimirlos con el for.

yu el insert pues queda con 7..
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
Imágen de perfil de Hfr
Val: 36
Ha disminuido su posición en 52 puestos en PHP (en relación al último mes)
Gráfica de PHP

problema la recibir los datos de los list box

Publicado por Hfr (93 intervenciones) el 20/06/2016 20:22:28
no men ya.
realice un test dedes la dbo hasta la vista y era un maldito campo que modificaron y y no me avisaron..
y por eso se toteba. al insert.
las funciones de addRow y deleteRow
return false;
les adicione el return false (en el ambiente de producción. )
para no enviar el form con los botones jejeje.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<html>
 
<head>
 
<SCRIPT language="javascript">
 
function seleccion(){
            var sel = document.getElementById('d');
            for (i = 0; i < sel.length; i++) {
                //if (sel[i].value == elemento) {
                sel[i].selected = true;
                //}
            }
        }
		function addRow(tableID) {
 
			var table = document.getElementById(tableID);
 
			var rowCount = table.rows.length;
			var row = table.insertRow(rowCount);
 
			var cell1 = row.insertCell(0);
			var element1 = document.createElement("input");
			element1.type = "checkbox";
			element1.name="chkbox[]";
			cell1.appendChild(element1);
 
 
				var cell2 = row.insertCell(1);
			cell2.innerHTML = rowCount + 1;
 
			var cell3 = row.insertCell(2);
			var element2 = document.createElement("input");
			element2.type = "text";
			element2.name = "mts[]";
			cell3.appendChild(element2);
 
 
		}
 
		function deleteRow(tableID) {
			try {
			var table = document.getElementById(tableID);
			var rowCount = table.rows.length;
 
			for(var i=0; i<rowCount; i++) {
				var row = table.rows[i];
				var chkbox = row.cells[0].childNodes[0];
				if(null != chkbox && true == chkbox.checked) {
					table.deleteRow(i);
					rowCount--;
					i--;
				}
 
 
			}
			}catch(e) {
				alert(e);
			}
		}
 
 
 
	</SCRIPT>
<script>
function listbox_move(listID,direction){
	var listbox=document.getElementById(listID);
	var selIndex=listbox.selectedIndex;
	if(-1==selIndex)
	{
		alert("Please select an option to move.");
	return;
}
 
var increment=-1;if(direction=='up')
increment=-1;
else
increment=1;
if((selIndex+increment)<0||(selIndex+increment)>(listbox.options.length-1))
{
	return;
	}
var selValue=listbox.options[selIndex].value;
var selText=listbox.options[selIndex].text;listbox.options[selIndex].value=listbox.options[selIndex+increment].value
listbox.options[selIndex].text=listbox.options[selIndex+increment].text
listbox.options[selIndex+increment].value=selValue;listbox.options[selIndex+increment].text=selText;listbox.selectedIndex=selIndex+increment;
}
 
 
function listbox_moveacross(sourceID,destID){
	var src=document.getElementById(sourceID);
	var dest=document.getElementById(destID);
	for(var count=0;count<src.options.length;count++)
	{
		if(src.options[count].selected==true)
{
var option=src.options[count];
var newOption=document.createElement("option");
newOption.value=option.value;
newOption.text=option.text;
newOption.selected=true;
	try
	{
	dest.add(newOption,null);
	src.remove(count,null);
	}
	catch(error){
 
		dest.add(newOption);src.remove(count);
 
		}
 
count--;
}
}
}
function listbox_selectall(listID,isSelect)
{
	var listbox=document.getElementById(listID);
for(var count=0;count<listbox.options.length;count++)
{
	listbox.options[count].selected=isSelect;
	}
	}
 
</script>
</head>
 
 
<body>
 
<form ACTION ="XX.PHP" METHOD ="POST">
 
 
 
	<select id="s" size="10" multiple name ="p[]"  >
 
 
 
 
 
<option value="dato1">dato1</option>
<option value="dato2">dato2</option>
<option value="dato3">dato3</option>
<option value="dato4">dato4</option>
<option value="dato5">dato5</option>
<option value="dato6">dato6</option>
<option value="dato7">dato7</option>
<option value="dato8">dato8</option>
<option value="dato9">dato9</option>
 
</select>
 
	<a href="#" onclick="listbox_moveacross('s', 'd')">&gt;&gt;</a>
<a href="#" onclick="listbox_moveacross('d', 's')">&lt;&lt;</a>
<select id="d" size="10" multiple=""  name ="p[]" >
 
 
	</select>
	<br>
	<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
 
	<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
 
	<TABLE id="dataTable" >
		<TR>
			<TD><INPUT type="checkbox" name="chk"/></TD>
	<TD>1</TD>
			<TD> <INPUT type="text" name="mts[]"/> </TD>
		</TR>
	</TABLE>
 
	<BR>
	<INPUT TYPE="submit" value="click" onclick="seleccion();">
	</form>
</body>
 
</html>
 
 
 
<?php
 
include ('php/con_mysql.php');
 
 
 
$link = new conexion();
$conn = $link->conectar();
 
 
$idnnja='1234567890';
$dato =$_POST['p'];
$metas = $_POST['mts'];
for ($d=0; $d<count($metas); $d++)
 
	{
		echo "dato nuermo de la meta".$d."  ".$metas[$d]."<br>";
	}
 
	for ($d=0; $d<count($dato); $d++)
 
	{
		echo "dato nuermo".$d."  ".$dato[$d]."<br>";
	}
 
 
$realizacion ="JASLÑKDJASLKD";
 
 
 
 
 
$query ='INSERT INTO pl_vidasaludable (id_nnja,realizacion,';
 
for ($i=0, $size = count($dato); $i<$size;$i++){
    $query.= ' dato'.($i+1).', metas'.($i+1);
    if ( $size > $i+1 ){
        $query .= ',';
    }
}
$query .= ') VALUES ('.$idnnja.',\''.$realizacion.'\''; // aqui ingreso el id pero falta la coma
for ($i=0, $size = count($dato); $i<$size;$i++){
    if ($i == 0){ // como solo sera al inicio del array coloco como condicion que sea en el primer indice
        $query.= ', '; // aca ingreso la coma
    }
    $query .= '\''.$dato[$i].'\', \''.$metas[$i].'\'';
    if ( $size > $i+1 ){
        $query .= ', ';
    }
}
$query.= ');';
$inser=mysqli_query($conn,$query);
 
if ($inser)
 
	{
 
		echo "exito<BR>";
	}
 
	else {
 
		echo "<BR>no insertado<BR>";
	}
 
	echo $query;
 
 
?>
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