Foros del Web » Programando para Internet » PHP »

Alguien que controle PHP y Javascript?

Estas en el tema de Alguien que controle PHP y Javascript? en el foro de PHP en Foros del Web. Necesito ayuda para enviar una variable a través de javascript en un script de PHP. Adjunto el código para ver si alguien tiene la solución. ...
  #1 (permalink)  
Antiguo 12/11/2003, 06:13
Gux
 
Fecha de Ingreso: octubre-2003
Mensajes: 58
Antigüedad: 20 años, 6 meses
Puntos: 0
Alguien que controle PHP y Javascript?

Necesito ayuda para enviar una variable a través de javascript en un script de PHP. Adjunto el código para ver si alguien tiene la solución. Le he dado mil vueltas pero no consigo enviar la variable de cantidad. Las demás, esto es, tipo, descripcion y precio se envían bien, pero la de cantidad se me resiste. Alguien me puede ayudar? Muchas gracias.


carrito.php
-------------------------------------------------------------------------

<html>
<head>
<title>Carrito de Compra</title>

<link rel="stylesheet" href="estilo.css" type="text/css">
<script LANGUAGE="JavaScript">
<!--
function CargarFoto(img, ancho, alto){
derecha=(screen.width-ancho)/2;
arriba=(screen.height-alto)/2;
string="toolbar=0,scrollbars=1,location=0,statusba r=0,menubar=0,resizable=1,width="+ancho+",height=" +alto+",left="+derecha+",top="+arriba+"";
fin=window.open(img,"",string);
}
// -->
</script>
</head>
<body bgcolor="#FFFFFF" link="#32468D" vlink="#800080" class="body">
<SCRIPT LANGUAGE="JavaScript">

function buyItem(newItem, newPrice, newQuantity) {
if (newQuantity <= 0) {
rc = alert('La cantidad ingresada es incorrecta');
return false;
}
if (confirm('¿Agregar '+newQuantity+' '+newItem+' al carrito?')) {
index = document.cookie.indexOf("TheBasket");
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
}
document.cookie="TheBasket="+document.cookie.subst ring(countbegin, countend)+"["+newItem+","+newPrice+"#"+newQuantity+"]";
}
return true;
}

function resetShoppingBasket() {
index = document.cookie.indexOf("TheBasket");
document.cookie="TheBasket=.";
}
</SCRIPT>
<center>
<table border="0" cellpadding="0" cellspacing="0" width="640" class="body">
<tr>
<td width="50%"><big><b>Carrito de Compra</b></big></td>
<td width="50%" align="right">[ Carrito de Compra | <a href="estado.php">Estado</a> | <a href="formulario.php">Formulario</a> ]</td>
</tr>
<tr>
<td width="100%" colspan="2">
<hr size="1" color="#000000">
</td>
</tr>
<tr>
<td width="100%" colspan="2"></td>
</tr>
</table>
<table border="1" cellspacing="0" width="640" bgcolor="#E0E0E0" bordercolor="#FFFFFF" class="td">

<tr bgcolor="#7A8EEB" style="color: #FFFFFF; font-family: Verdana, Arial; font-size: 10px;">
<td width="100">&nbsp;</td>
<td width="290"><b>Producto</b></td>
<td width="90"><b>Precio</b></td>
<td width="120" colspan="2"><b>Cantidad</b></td>
</tr>
<tr>
<td colspan="5">&nbsp;</td>
</tr>
<form NAME="itemsform">

<?php
include("variableprods.php");
$sql="select * from $table";
$result=mysql_db_query($basedatos,$sql,$link);
while($row=mysql_fetch_array($result))
{
echo("<tr>
<td width='120' valign='top' align='center'><a href=javascript:CargarFoto('imagenes/avestruz.jpg','200','200')><img src='thumbnails/avestruz.jpg' border='0'></a></td>
<td width='340' valign='top'><a href=javascript:CargarFoto('imagenes/avestruz.jpg','200','200')><strong>$row[tipo]</strong></a><br>
<small>$row[descripcion]</small></td>
<td width='60' align='center' valign='top'>$row[precio]</td>
<td width='50' valign='top' align='center'>
<input TYPE='text' NAME='cantidad' VALUE='1' SIZE='3'>
</td>
<td width='70' valign='top'>");?>
<input TYPE='button' NAME='cbtadd' VALUE='Agregar'
onclick="buyItem('<?php echo("$row[tipo]"); ?>','<?php echo("$row[precio]"); ?>', document.itemsform.cantidad.value)">
<?php echo("</td>
</tr>");
}
?>

</form>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="640" class="body">
<tr>
<td width="100%" colspan="2">
<hr size="1" color="#000000">
</td>
</tr>
<tr>
<tr>
<td width="50%"></td>
<td width="50%" align="right">Página [<a href="carrito.php">1</a>]</td>
</tr>
</table>
</center>
</body>
</html>
  #2 (permalink)  
Antiguo 12/11/2003, 06:34
Ex Colaborador
 
Fecha de Ingreso: junio-2002
Mensajes: 9.091
Antigüedad: 21 años, 10 meses
Puntos: 16
Hola,

Fijate en lo que estas generando: un solo formulario con x cajas de texto que se llaman igual 'cantidad'. ¿Como va a saber a que caja de texto te refieres?

Tendrias que poner un nombre distinto a cada caja de texto. Lo normal es poner cantidad_0, cantidad_1, etc. Para ello deberias poner:
Código PHP:
<?php
$contador
=0;
while (
$row=mysql_fetch_array($result))
{
...
?>
<input TYPE='text' NAME='cantidad_<?php echo $contador?>' VALUE='1' SIZE='3'>
</td>
<td width='70' valign='top'>");?>
<input TYPE='button' NAME='cbtadd' VALUE='Agregar'
onclick="buyItem('<?php echo("$row[tipo]"); ?>','<?php echo("$row[precio]"); ?>', document.itemsform.cantidad_<?php echo $contador?>.value)">
<?php echo("</td>
</tr>"
);
$contador++;
}
?>
Saludos.
__________________
Josemi

Aprendiz de mucho, maestro de poco.
  #3 (permalink)  
Antiguo 12/11/2003, 07:06
Gux
 
Fecha de Ingreso: octubre-2003
Mensajes: 58
Antigüedad: 20 años, 6 meses
Puntos: 0
Funciona!
Muchas gracias Josemi. No te puedes ni imaginar las vueltas que le he dado...
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:58.