doujing6053
2019-01-28 18:09
浏览 651
已采纳

每次页面加载时触发按钮onclick事件

I have a button which has a onclick attribute which calls a function. My problem is that whenever the page loads it automatically triggers the onclick event without me clicking on anything.

I've tried different variatons of syntax but nothing worked. I swapped 'button' for 'input type=button' but that didn't help anything.

this is in books.php

$sql = "SELECT books.id, books.name as bookname, authors.name as authorname, autori.surname, genre, description, stock FROM books JOIN authors ON books.author_id=authors.id ORDER BY books.name ASC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table><tr><th>Name of the book</th><th>Author</th><th>Copies available</th></tr>";

    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["bookname"]."</td><td>".$row["authorname"]." ".$row["surname"]."</td><td>".$row["stock"]."</td>";

        if (isAvailable($row["id"]) && isset($_SESSION["id"])) {
            ?>
            <td><input type="button" value="Borrow" class="button" id="btnBorrow" onclick="<?php borrowBook($row["id"])?>"></td></tr>

and I'm calling the function borrowBook from functions.php which looks like this.

function borrowBook($idbook) {
    $servername = "aaa";
    $username = "bbb";
    $password = "ccc";
    $dbname = 'ddd';
    $iduser = $_SESSION["id"];

    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "UPDATE books SET stock = stock - 1 where id = " . $idbook;
    $conn->query($sql);

    $sql = "INSERT INTO reservations(id, dateBorrowed, dateReturn, returned, kniha_id, uzivatel_id) VALUES (NULL, NOW(), DATE_ADD(NOW(), INTERVAL 34 DAY), 0, $idbook, $iduser)";
    $conn->query($sql);
}

So the SQL query and everything actually works. When I check the database I actually get new entries and everything is as expected. The only problem I'm having is that the button's onclick event is always triggered on every page load and I can't seem to fix it. From searching online everybody is using stuff like JavaScript or jQuery so it didn't really help me.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanbiyi7319 2019-01-28 19:33
    已采纳

    Hi and welcome to Stack Overflow!

    Looks like you are trying to call php function from the client (browser). This is however impossible.

    The way the PHP works is, that it prepares the content for the client and sends it to the client. After it is send, you cannot interact with the PHP code anymore. What you need to do is make client send another request.

    My problem is that whenever the page loads it automatically triggers the onclick event without me clicking on anything.

    The page load does not trigger onclick event. The PHP looks for all <?php and runs the code inside it even before it is sent to client.

    How to do it?

    You need to change the infrastructure a bit. For the beginning i'd suggest not using JS at all, but instead create second PHP page, that just does the borrowBook using GET parameter (you can expand it later) (See PHP's $_GET)

    First you need to actually create the second page (let's call it borrowBook.php) This page will get book's id using GET parameter (let's call that bookid)

    This page's code may look something like this (Note: code is not tested)

    <?php
    borrowBook($_GET["bookid"]);
    header("Location: /books.php");
    ?>
    

    And now you need to change original code's line

    <input type="button" value="Borrow" class="button" id="btnBorrow" onclick="<?php borrowBook($row["id"])?>">

    To something like this

    <a href="/borrowBook.php?bookid=<?= $row["id"] ?>">Borrow</a>

    What this does is, that PHP sees <?= and run the code inside it (in this case replaces the <?= ?> section with value of $row["id"]. Which if id is 1 will result in this:

    <a href="/borrowBook.php?bookid=1">Borrow</a>

    Sorry for my bad english.

    打赏 评论
  • duanliang9288 2019-01-28 18:14

    You should use js (or jquery) in onclick handler that call your php-script with ajax. PHP scripts works only in server. Like this (jquery example):

    <button id="handled-button">Click Me</button>
    <script>
    $('#handled-button').click(function() {
    $.get('/myscript.php');
    });
    </script>
    

    And in myscript.php call your function.

    打赏 评论

相关推荐 更多相似问题