Call a php function

0

Good I have the following doubt I have the following functions PHP in a file, the thing is that I want that by pressing a button (delete) the function is called delete and delete the record. How can you do that without calling a funcion js . Greetings

// funciones.php
function eliminarUsuario($rut){
$consulta ="DELETE FROM usuario WHERE rut ='$rut' ";
$conexion = conectarServidor();
    $query = $conexion->query($consulta);
    if ($query) {
        echo "Eliminado Correctamente";
    }

    else{
        echo "Error";
    }

}

formularioEliminarUsuario.php

 <button type="submit"   class="btn red">Eliminar</button>
    
asked by Felipe Larraguibel 02.11.2018 в 01:44
source

2 answers

1

You can use a form to send the data through POST / GET, the information is sent and does not require javascript: (although it changes the page)

<form action="eliminar.php" method="POST">
    <input type="text" name="rut">
    <button type="submit"   class="btn red">Eliminar</button>
</form>

Meanwhile, the destination file would contain something like this:

<?php

include_once('funciones.php')

if($_POST && $_POST['rut'])
{
    eliminarUsuario($_POST['rut']);
}

This file simply calls the function if the desired information exists (in this case through POST). It should be noted that this file can also be accessed through js without problems.

    
answered by 02.11.2018 / 01:58
source
0

Your button must be inside a form tag from which the file is indicated when the submit is executed.

<form action="funciones.php" method="POST">
<input type="text" name="valor">
<button type="submit"   class="btn red">Eliminar</button>
</form>

In your question you do not specify where you take the value of $rut but I assume it is a field of the same form.

Add a section of code in your funciones.php file where you capture the value and then send the function call.

if(!isset($_POST['valor'])){
   eliminarUsuario($_POST['valor']);
}
    
answered by 02.11.2018 в 02:04