How to execute a function written in php that receives data from text boxes from a button in another form


I have searched for a number of pages and I almost have the correct answer, but something is missing: what happens is that I execute a function (Save ()) on a page (register_client.php), now, what I want to do is that on my other page (client.php) by a button I just run the function more not that I address to the second page and execute it. I could achieve it but, when executing it, it's as if I was not pulling the data from the text boxes. And I get a database error that would be logical not to type the ID, but if I type it. THIS IS THE CODE:


function Guardar()
    $Conexion = new mysqli('localhost','root','12345678','prayci');
  $name= $_POST["Name"];
  $direccion= $_POST["Direccion"];
  $mysql = 'CALL Registrar_Cliente(?, ?, ?, @p_mensaje);';
  $stmt = $Conexion->prepare($mysql);
  //variable de salida
   $select = mysqli_query($Conexion,'SELECT @p_mensaje');
   $result = mysqli_fetch_assoc($select);
   $mensaje = $result['@p_mensaje'];
   echo ($mensaje);


     $(function() {
                      $("#submit").click(function() {
                       alert("<?php  echo Guardar();?>");

Thank you in advance.

asked by Jose Enrique Puente Arca 27.11.2018 в 03:30

1 answer


I think what you have to use is AJAX.

$('#IDFORMULARIO').submit(function(event){   //donde #IDFORMULARIO es el id del formulario de registro
      var parametros = $(this).serialize(); //esto se encarga de obtener todas los datos ingresados
      $.ajax({ //la llamada ajax
        type: "POST", 
        url: "registrar_cliente.php", //el archivo donde se encuentra el código a ejecutar
        data: parametros, //los datos ingresas en el formulario lo envías aquí
        success: function(data) {
          $('#resultado').html(data); //donde #resultado es el div donde se muestra el mensaje
      event.preventDefault(); //previene que el boton submit realice su comportamiento normal
<form role="form" id="IDFORMULARIO" method="post" autocomplete="off">

  <input id="name" type="text" class="validate" name="name" required>
  <label for="name">Name</label>
  <input type="text" class="validate" name="DNI">
  <label for="DNI">DNI</label>
  <input type="text" class="validate" name="Direccion">
  <label for="Direccion">Direccion</label>
    <input class="btn right" type="submit" value="Registrar">
<div id="resultado"></div>
answered by 27.11.2018 / 05:08