-1

I'm trying to figure out how to call a PHP function with a html button as i understand so far i can't use javascript since it client side but would it work with jquery and how so?

Here my button

<button type="button" class="btn btn-default" data-dismiss="modal">Yes</button>

And here is my function in php

function confirmCloseTicket($conn) {

if (isset($_GET['deleteticketid'])) {

  $id = $_GET['deleteticketid'];
  $statuschange = "Closed Ticket";

  $sql = "INSERT INTO closedtickets SELECT * FROM ticket WHERE ticketID = $id;";
  $sql .= "DELETE FROM ticket WHERE ticketID = $id;";
  $sql .= "UPDATE closedtickets SET status = '$statuschange' WHERE closedtickets.ticketID = $id;";

  $result = mysqli_multi_query($conn, $sql);

          if($result) {
            header("location: http://localhost/ticketSystem/viewticket.php");
            exit();

            }else {
               echo "failed";
            }
    }

}
Mr Vitx
  • 19
  • 4
  • You need to do `ajax` to do this thing like int this post : https://stackoverflow.com/questions/15757750/how-can-i-call-php-functions-by-javascript – jean-max Feb 05 '22 at 11:24
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Feb 05 '22 at 15:23
  • Yes i'm aware of that this is just a test product and not offical on offical website – Mr Vitx Feb 05 '22 at 15:32

1 Answers1

0

I figure it out with this method:

Starting of to create a button using <a> tag and give it AN ID "Verify-Email" you can call it what ever you want just remember it!

HTML:

<a href="#" id="verify-email" class="float-right">Verify Now!</a>
  1. In jquery or inside a <script></script> tag i call ID "#verify-email". also using # it tells is a ID and not a class, if you were using a class it would look like this.

    ".verify-email"

  2. Then i'm using a .click(function(e){ a bassic click do function.

  3. then i'm using e.preventDefault(); to prevent the page to refresh while we doing this click function by pressing the button from the page.

  4. then i'm using a $.ajax({ }); reuqest to call it to POST Method.

  5. as you can see inside my Ajax i have URL, method and DATA, what i have done i have called an action : 'verify-email' see step 6. how i GET that POST inside php.

jquery:

$("#verify-email").click(function(e) {
    e.preventDefault();
    $(this).text('Please Wait...');
    
    $.ajax({
        url: 'assets/php/process.php',
        method: 'POST',
        data: { action: 'verify-email' },
        success:function(response){
            $("#verifyEmailAlert").html(response);
            $("#verify-email").text('Verify Now');
        }
    });
});
  1. inside here i have a isset function by verify super global variable $_POST['action] and also checking it again with 'verify-email' both of those values need to be same as $.ajax method

process.php:

if (isset($_POST['action']) && $_POST['action'] == 'verify-email') {
    // need to create a varible for "verify-email"
    $Getemail = $_POST['verify-email'];

    then call the function
}
Mr Vitx
  • 19
  • 4
  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Tyler2P Feb 14 '22 at 17:18
  • Hi, i have edit my answer, thanks for the feedback – Mr Vitx Feb 14 '22 at 19:43