簡體   English   中英

如何從HTML按鈕單擊調用PHP函數?

[英]How to call a PHP function from a HTML button click?

有人可以幫我提供一些Javascript,當我單擊按鈕時可以讓我調用函數嗎?

我知道這已經發布過了,但是我看到的每個答案都太模糊了,我已經在這里呆了8個小時了:(

請記住,我是javascript初學者。

<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <?php include 'connect.php'; ?>
    <table border cellpadding=5>
        <div>
            <tr>
                <th>Report</th>
                <th></th>
            </tr>
            <tr>
                <td>Students with highest scores</td>
                <td>
                    <button type= button>Generate report</button>
                </td>

        </div>
    </table>
    <?php   

        function highestScore()
        {
            $data = mysql_query("SELECT t.Test_name, s.Student_firstname, s.Student_surname, sc.Result
                                 FROM Tests t 
                                 JOIN Scores sc ON t.id_Tests = sc.Tests_id_Tests
                                 JOIN Students s ON sc.Students_id_Students = s.id_Students
                                 WHERE t.id_Tests = 1
                                 ORDER BY sc.Result DESC");
                    if(!$data)
                    {
                        die("Invalid Query: " . mysql_error());
                    }
            Print"<table border cellpadding=5>";
            while($info = mysql_fetch_array($data))
            {
            Print"<tr>";
            Print "<th>Test:</th> <td>" . $info['Test_name'] . "</td> ";
            Print "<th>First Name:</th> <td>" . $info['Student_firstname'] . "</td> ";
            Print "<th>Surname:</th> <td>" . $info['Student_surname'] . "</td> ";
            Print "<th>Result:</th> <td>" . $info['Result'] . "</td> ";
            }
            Print "</table>";
        }

    ?>
</body>

我想使用我創建的“生成報告”按鈕來執行“ highestScore”功能。

該函數從mySQL數據庫創建一個值表。

最終將有更多的按鈕顯示不同的表格。

任何幫助表示贊賞。

HTMLJavascript在瀏覽器上運行,而PHP在服務器上運行。 您將必須使用AJAX或刷新頁面來進行另一個服務器調用。 這是使用AJAX而不刷新頁面的方法。 $ .ajax docs

  1. 使用以下代碼創建一個新的PHP頁面: query.php

     <?php $data = mysql_query("SELECT t.Test_name, s.Student_firstname, s.Student_surname, sc.Result FROM Tests t JOIN Scores sc ON t.id_Tests = sc.Tests_id_Tests JOIN Students s ON sc.Students_id_Students = s.id_Students WHERE t.id_Tests = 1 ORDER BY sc.Result DESC"); if(!$data) { die("Invalid Query: " . mysql_error()); } Print"<table border cellpadding=5>"; while($info = mysql_fetch_array($data)) { Print"<tr>"; Print "<th>Test:</th> <td>" . $info['Test_name'] . "</td> "; Print "<th>First Name:</th> <td>" . $info['Student_firstname'] . "</td> "; Print "<th>Surname:</th> <td>" . $info['Student_surname'] . "</td> "; Print "<th>Result:</th> <td>" . $info['Result'] . "</td> "; } Print "</table>"; ?> 
  2. 使用按鈕的click事件運行ajax請求:(將以下腳本添加到HTML頁面)

     $(function(){ $('button').click(function(){ $.ajax({ url:'query.php', success:function(response){ alert(response); } }); // this will alert the code generated in example.php }); }); 

代替使用按鈕,而是使用帶有提交按鈕的表單。 通過POST發送請求,然后使用isset($ _ POST ['report'])在PHP中進行檢測,然后顯示報告。

<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <?php include 'connect.php'; ?>
    <table border cellpadding=5>
        <div>
            <tr>
                <th>Report</th>
                <th></th>
            </tr>
            <tr>
                <td>Students with highest scores</td>
                <td>
            <form method="POST">
                            <input type="submit" name="report">Generate report</button>
            </form>
                </td>

        </div>
    </table>
    <?php   

        function highestScore()
        {
            $data = mysql_query("SELECT t.Test_name, s.Student_firstname, s.Student_surname, sc.Result
                                 FROM Tests t 
                                 JOIN Scores sc ON t.id_Tests = sc.Tests_id_Tests
                                 JOIN Students s ON sc.Students_id_Students = s.id_Students
                                 WHERE t.id_Tests = 1
                                 ORDER BY sc.Result DESC");
                    if(!$data)
                    {
                        die("Invalid Query: " . mysql_error());
                    }
            Print"<table border cellpadding=5>";
            while($info = mysql_fetch_array($data))
            {
            Print"<tr>";
            Print "<th>Test:</th> <td>" . $info['Test_name'] . "</td> ";
            Print "<th>First Name:</th> <td>" . $info['Student_firstname'] . "</td> ";
            Print "<th>Surname:</th> <td>" . $info['Student_surname'] . "</td> ";
            Print "<th>Result:</th> <td>" . $info['Result'] . "</td> ";
            }
            Print "</table>";
        }

    if (isset($_POST['report'])) {
        highestScore();
    }

    ?>
</body>

原始按鈕在純HTML中沒有意義。 什么都不做 -除了看起來像一個按鈕。

您可以將其與JavaScript結合使用

<form action="input_button.htm">

    <textarea cols="20" rows="4" name="field1"></textarea>

    <input type="button" name="Text 1" value="Add some new Text"
      onclick="this.form. field1.value='Some new Text'">

</form>

通過單擊該按鈕可以執行JavaScript-並替換名為field1textarea

所以,你想這一切都在一個頁面上,所以你首先需要的是form的HTML標簽周圍的按鈕,我已經叫我的“提交”,並設置動作網頁本身的包裹<?php echo $_SERVER['PHP_SELF']; ?> <?php echo $_SERVER['PHP_SELF']; ?> 單擊按鈕時,它將執行您的php代碼

腳本

<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php include 'connect.php'; ?>
<table border cellpadding=5>
    <div>
        <tr>
            <th>Report</th>
            <th></th>
        </tr>
        <tr>
            <td>Students with highest scores</td>
            <td>
            <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <!-- Calls for the PHP script on the page-->
                <button type= button>Generate report</button>
                </form>
            </td>

    </div>
</table>
<?php  
    //Script that is called

    if(isset($_POST['submit'])){ //The ID of the form we used is 'submit' so when that is clicked it will execute this
    function highestScore()
    {
        $data = mysql_query("SELECT t.Test_name, s.Student_firstname, s.Student_surname, sc.Result
                             FROM Tests t 
                             JOIN Scores sc ON t.id_Tests = sc.Tests_id_Tests
                             JOIN Students s ON sc.Students_id_Students = s.id_Students
                             WHERE t.id_Tests = 1
                             ORDER BY sc.Result DESC");
                if(!$data)
                {
                    die("Invalid Query: " . mysql_error());
                }
        Print"<table border cellpadding=5>";
        while($info = mysql_fetch_array($data))
        {
        Print"<tr>";
        Print "<th>Test:</th> <td>" . $info['Test_name'] . "</td> ";
        Print "<th>First Name:</th> <td>" . $info['Student_firstname'] . "</td> ";
        Print "<th>Surname:</th> <td>" . $info['Student_surname'] . "</td> ";
        Print "<th>Result:</th> <td>" . $info['Result'] . "</td> ";
        }
        Print "</table>";
    }


    highestScore(); //executes your function you created
    }

?>
</body>

HTML和JavaScript在瀏覽器上運行,而PHP在服務器上運行。 您必須編寫Ajax或Java腳本。在Java腳本中,您可以調用php頁面並在該頁面中做您的事情,這將是更好的方法。您還可以傳遞變量。

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function removeItem(oButton) 
{
var hello = "hello";
var world = "world";
window.location.href = "trytest.php?w1=" + hello + "&w2=" + world;//Your page location
}       
</script>
</head>
<body>
<form>
<h2>This is the test </h2>
<button type="show" onclick="removeItem(this); return false;">Test</button>
</body>
</html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM