Search code examples
phphtmlmysqli

How to execute stored procedure on click Html button?


I need to execute stored procedure which is on database, to on click Html button!!

name of stored procedure: DeleteRow

    <form action="">
    <input type="button" value="Check" onclick="">
    </form>

How I can execute using php?? Is it possible?

** SQL query:**

CALL `DeleteRow`();

Solution

  • You can try something like this:

    HTML:

    <input type='button' value='Call Procedure' id='BtnId'>
    

    jQuery:

    $(document).ready(function(){
    
        $("#BtnId").on( 'click', function(){
    
            $.ajax({
                type : "POST",
                url : "callsp.php",
                success : function(text){
                    alert(text);
                }
            });
    
            return false;
        }); 
    
    });
    

    PHP: (callsp.php)

    <?php
    
    // connect to database
    $connection = mysqli_connect("hostname", "username", "password", "db", "port");
    
    // run the query
    $result = mysqli_query($connection, "CALL DeleteRow");
    
    // loop the result set
    while( $row = mysqli_fetch_array($result) ) {
      print_r( $row );
    }
    
    ?>