如何在PHP中单击鼠标从html表中检索行?

时间:2016-11-12 04:23:10

标签: php jquery html

我正在使用PHP在HTML表格中显示一些记录。我试图这样做,以便当我点击一行时,我可以在变量中存储 itemID (已被点击)。出于某种原因,当我点击我想要的行时,JavaScript中的警报不起作用也不会发生。我错过了一些微不足道的事情吗?

<?php
    $con=mysqli_connect("localhost","root","mypassword","myDB");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    echo "<font color = 'darkgreen'> Connected to database. </font> <br>";

    $result = mysqli_query($con,"SELECT * FROM Inventory");

    echo "<table border='1'>
    <tr>
    <th>Item ID</th>
    <th>Item Name</th>
    </tr>";

    while($row = mysqli_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $row['itemID'] . "</td>";
    echo "<td>" . $row['itemName'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";

    mysqli_close($con);
    ?>

    <script type="text/javascript">
    $("tr.table").click(function() {
        var tableData = $(this).children("td").map(function() {
            return $(this).text();
        }).get();

        alert("Your data is: " + $.trim(tableData[0]) + " , " + $.trim(tableData[1]));
    });
    </script>

2 个答案:

答案 0 :(得分:3)

def main():
    # Getting input for Balance
    balance = float(input("Balance: $ "))
    # Getting input for Interest Rate
    intRate = float(input("Interest Rate (%) : "))
    # Getting input for Number of Years
    years = int(input("Years: "))
    newBalance = calcBalance(balance, intRate, years)

    print ("New baance:  $%.2f"  %(newBalance))

def calcBalance(bal, int, yrs):
    newBal = bal
    for i in range(yrs):
        newBal = newBal + newBal * int/100
    return newBal

# Program run
main()

def BankBalance():
    InputB = 1000
    return InputB
    print("Your initial balance is $1000")

def Interest():
    InputI = 0.05
    return InputI
    print("The rate of interest is 5%")

def CountNumber():
    InputN = float(input("Please enter the number of times per year you would like your interest to be compounded: "))
    return InputN

def Time():
    InputT = float(input("Please enter the number of years you need to compund interest for:"))
    return InputT

def Compount_Interest(InputB, InputI, InputT, InputN):
    Cinterest = (InputB * (1+(InputI % InputN))**(InputN * InputT))
    print("The compound interest for %.InputT years is %.Cinterest" %Cinterest)

B = BankBalance()
I = Interest()
N = CountNumber()
T = Time()
Compount_Interest(B, I, N, T)

答案 1 :(得分:0)

parcelable PackageStats;

请在您的表中写入并创建java脚本函数以显示警报

echo "<tr onclick='showAlert($row['itemID'],$row['itemName']);'>";

这是提醒的方式之一。请试试这个。这对你有用。它适用于我的情况。