How to capture a variable of a while to delete a specific data

1

Hi, thanks, first of all, I'm new to programming and I've really searched and searched and I have not gotten information about it. This is what happens, I need to delete a field from the database and a confirmation appears but I can not capture the id that I want to be able to erase it .. help porfa here the code:

    while (list($id,$nombre,$imagen)=mysql_fetch_array($query))
     { 
        echo "$nombre";
        echo "$imagen";
        echo "<a title='Presione aca si desea borrar' href='borrar.php?id=$id' onclick='preguntar();return false;'>eliminar</a>";
        echo "</br>";
     }

and this is my javascript code

       <script language="Javascript">
   function preguntar()
     {  
      eliminar=confirm("¿Deseas eliminar este registro?");
      if (eliminar){
          //Redireccionamos si das a aceptar
       window.location.href = "borrar.php?id=<?php echo $id; ?>";                                                                                           
       else
      alert('No se ha podido eliminar el registro...')
     }

      </script>

I need to capture the id of record 3 for example and save it in a php variable and pass it by url to the window.location of js.

    
asked by Alhuka 24.11.2016 в 00:47
source

1 answer

2

You could send the id by parameter to the javascript function

echo "<button onclick='preguntar($id)'>Eliminar</button>";

And then in the js function:

<script language="Javascript">
   function preguntar(id)
     {  
      if (confirm("¿Deseas eliminar este registro?")){
          //Redireccionamos si das a aceptar
          window.location.href = "borrar.php?id=" + id;                                                                                           
      }else
          alert('No se ha podido eliminar el registro...')
     }

</script>
                                    
answered by 24.11.2016 / 00:51
source