Question

I have somes forms, and when I push one button of there I want to get in javascript wich is the form of this button I pushed. Or get the value's form in php code with the var $_POST?

<?php include ('conexion.php');?>

<!DOCTYPE html PUBLIC "=//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/199/xhtml">

<head>
<meta hhtp-equiv="Content-Type" content="text/html; charset="utf-8" />
<title>Tienda</title>

<script>
function inicializar()
{
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
}
else{// code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
}
}
function comprar()
{
var xmlhttp;
var fieldValue = document.comprar.elements[0].value;
xmlhttp=inicializar();

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("p="+fieldValue);
}
</script>

</head>

<body>

<div id="myDiv"><p>Carrito: 0 productos 0,00&#8364 </p></div>

<p>LISTADO DE PRODUCTOS</p>

<table width="200" border="0" id="myTable">
<tr>
    <td bgcolor="#D6D6D6">ID</td>
    <td bgcolor="#D6D6D6">IMAGEN</td>
    <td bgcolor="#D6D6D6">NOMBRE</td>
    <td bgcolor="#D6D6D6">DESCRIPCION</td>
    <td bgcolor="#D6D6D6">PRECIO</td>
    <td bgcolor="#D6D6D6">AGREGAR</td>
</tr>
<tbody>
<?php 

$consulta=mysql_query("select * from productos");

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()" method="post" name="comprar">
        <input name="id_txt" type="hidden" value="'.$id.'" />
        <input name="nombre" type="hidden" value="'.$nombre.'" />
        <input name="precio" type="hidden" value="'.$descripcion.'" />
        <input name="cantidad" type="hidden" value=1 />
        <input type="submit" name="Comprar" value="Comprar" /></form></td>
    </tr>   

<?php } ?>
</tbody>    
</table>
</body>
</html>

I have the code "comprar.php" and it is possible get the value of forms here?

Was it helpful?

Solution

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'];
?>

OTHER TIPS

You need to enable php on those variables within your form.

<form action ="javascript:comprar()" 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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top