Change your ajax to this, it will work but you will have to handle the three values in the comprar.php
function comprar()
{
var id_txt = document.forms["comprar"]["id_txt"].value;
var nombre = document.forms["comprar"]["nombre"].value;
var precio = document.forms["comprar"]["precio"].value;
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
xmlhttp.open("POST","comprar.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("id_txt=" + id_txt + "&nombre=" + nombre + "&precio=" + precio + "");
}
In the comprar.php page:
<?php
$id_txt = $_POST['id_txt'];
$nombre = $_POST['nombre'];
$precio = $_POST['precio'];
?>
UPDATE:
Look at l̕aͨŵƦȆ̴̟̟͙̞ͩ͌͝ƞCͭ̏ȇ ƇhƐȓ0nè response and change your form or you wont get the right values, your values are not the variables in php because you did't put them inside the tags and you didnt echo them to the page.
example:
with tags outside:.
<?php echo '<input name="precio" type="hidden" value="'.$descripcion.'" />'; ?>
or with tags inside:
<input name="precio" type="hidden" value="<?php echo $descripcion;?>" />
For more than one form you can create other functions like comprar2() and then get the variables from the second form, just like you did in the first.
here is the answer to your second question : Just create a variable with a number set to zero and every time a new form is created you add one to the number, like a for loop but with the while your code stays like this:
<?php
$consulta=mysql_query("select * from productos");
$i = 0;
while($filas=mysql_fetch_array($consulta)){
$id=$filas['id'];
$imagen=$filas['imagen'];
$nombre=$filas['nombre'];
$descripcion=$filas['descripcion'];
$precio=$filas['precio'];
?>
<tr>
<td><?php echo $id ?></td>
<td><img src="<?php echo $imagen; ?>" width="70" height="70"></td>
<td><?php echo $nombre ?></td>
<td><?php echo $descripcion ?></td>
<td><?php echo $precio ?></td>
<td><form action ="javascript:comprar(<?php echo $i; ?>)" method="post" name="comprar">
<input name="id_txt" type="hidden" value="<?php echo $id;?>" />
<input name="nombre" type="hidden" value="<?php echo $nombre;?>" />
<input name="precio" type="hidden" value="<?php echo $descripcion;?>" />
<input name="cantidad" type="hidden" value=1 />
<input type="submit" name="Comprar" value="Comprar" /></form></td>
</tr>
<?php $i = $i + 1; } ?>
Now change the script to handle the number of the form:
function comprar(nr_form)
{
var id_txt = document.forms["comprar"]["id_txt"].value;
var nombre = document.forms["comprar"]["nombre"].value;
var precio = document.forms["comprar"]["precio"].value;
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
xmlhttp.open("POST","comprar.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("id_txt=" + id_txt + "&nombre=" + nombre + "&precio=" + precio + "&nr_form=" + nr_form +"");
}
now in the comprar.php you have the number of the form that was clicked in nr_form variable
<?php
$nr_form = $_POST['nr_form'];
$id_txt = $_POST['id_txt'];
$nombre = $_POST['nombre'];
$precio = $_POST['precio'];
?>