Do not call the function

1

I intend to do a record deletion from a table with php and javascript, but when calling the function that the Delete does, it does not do anything.

With this code I call the function

<?php
  if (isset($_POST["delete-contrato-select"])) { 
       $idContratoSelect = $_POST['id_tipo_contratoDelete'];
       $CbContratoController -> delete($idContratoSelect);
  }
?>

This is my function where the Delete is

<?php

 class CbContratoController {

   function delete($id_tipo_contrato){ 
      require_once ('conex/conex.php'); 
      $sqlDelete="DELETE FROM tipo_contrato WHERE id_tipo_contrato = '".$id_tipo_contrato."'";  
      $res = $conex->query($sqlDelete);
   }

 }

?>

And here I get the registration id

 function deleteCbContrato(id_tipo_contrato){     
  document.formDeleteContrato.id_tipo_contratoDelete.value = id_tipo_contrato;                

  $('#myModalDelete').on('shown.bs.modal', function () {
      $('#myInput').focus()
  });


}

Delete button

 <button id="delete-contrato-modal" name="delete-contrato-modal" type="button" class="btn btn-link btn-danger btn-just-icon remove" onclick="deleteCbContrato('<?php echo($row_herramientas['id_tipo_contrato']); ?>');"><i class="material-icons" data-target="#myModalDelete" data-toggle="modal" >close</i></button>   

Modal code where I get the id_type _contract

 <div class="modal fade" id="myModalDelete" tabindex="-1" role="dialog" aria-labelledby="myModalDeleteLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalDeleteLabel">Eliminación de Registro</h4>
                    </div>

                    <form role="form" name="formDeleteContrato" method="post" action="contratos.php">
                        <div class="modal-body">                                    
                                <div class="input-group">
                                <label for="id_tipo_contrato">¿Desea eliminar el registro seleccionado?</label>
                                </div>       
                                <div class="input-group">
                                 <label for="id_tipo_contrato">Registro: </label>
                                 <input type="text" readonly class="form-control" id="id_tipo_contratoDelete" name="id_tipo_contratoDelete" >                                        
                                </div>
                        </div>

                        <div class="modal-footer">
                                <button id="delete-contrato-select" name="delete-contrato-select" type="submit" class="btn btn-primary">Aceptar</button>                                        
                                <button id="cancel"type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>   
                        </div>

                    </form>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal --> 
    
asked by Tallulah 22.10.2018 в 17:16
source

2 answers

0

Let's say you have an App class inside the file "App.php" ;

class App
{

public function hello(){
    echo "hello word";
}

}

To call that method from another site you have to create an object of the App class;

require("App.php");  //Indicas que vas autilizar ese archivo

$app = new App();  //guardas en $app una instancia de la clase App

$app->hello(); //eso deberia ejecutar el metodo hola mundo!

I hope that with this example you will have a little idea for your problem.

    
answered by 22.10.2018 в 17:30
0

If you are not going to instantiate the class "CbContratoController" from where you call the delete method, then you must make that method ("delete") within the class static, like this:

<?php

class CbContratoController {

  public static function delete($id_tipo_contrato){
    ...
  }
}

And you would call it like this:

<?php

require_once('CbContratoController.php');

if (isset($_POST["delete-contrato-select"])) { 
  $idContratoSelect = $_POST['id_tipo_contratoDelete'] ;
  CbContratoController::delete($idContratoSelect); 
}

But the best practice would be to make an instance of "CbContratoController", like this:

<?php

require_once('CbContratoController.php');

if (isset($_POST["delete-contrato-select"])) { 
  $idContratoSelect = $_POST['id_tipo_contratoDelete'];
  $contratoCtrl = new CbContratoController();
  $contratoCtrl->delete($idContratoSelect); 
}

And your delete method should no longer be static:

<?php

class CbContratoController {

  public function delete($id_tipo_contrato){
    ...
  }
}
    
answered by 22.10.2018 в 17:34