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

每次页面加载时触发按钮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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)