簡體   English   中英

onclick在php上顯示MySQL fetch echo的值

[英]onclick display the value of MySQL fetch echo on php

我有一個表,其中有一個loan_id ,當我獲取信息時,就可以了。 這個問題是我需要單擊loan_id號,然后根據id號顯示結果。

<?php
$data = mysql_query("select * from request, users where request.user_id = users.id and request.user_id = $user_id")  or die(mysql_error());
Print "<table border cellpadding=3>";    
while($info = mysql_fetch_array( $data )) {
    Print "<tr class='my_loans_tr'>"; 
    echo ("<td  id='demo' onclick='myfunction(this) class='weblex-show- detail' data-toggle='modal' data-target='#myModalw'>"  .$info['loan_id'] . "</td> ");
    Print " <td class='admin_amount'>".$info['amount'] . "</td> "; 
    Print " <td class='admin_points'>".$info['points'] . "</td> ";   
    Print " <td class='admin_date'>".$info['req_date'] . " </td>"; 
    Print " <td class='admin_status'>".$info['status'] . " </td>"; 
    Print " <td class='admin_cancelled'>".$info['cancelled_loan'] . " </td></tr>"; 
    Print "</tr>"; 
}  
Print "</table>";  
?>

在此處輸入圖片說明

數量146 147是貸款的ID,所以基本上我需要的是點擊的ID號,然后它通過我的ID號(任意數量的我點擊)這樣的話我可以運行基礎上,一個新的查詢loan_id 每個loan_id都有更多信息存儲在另一個表中。 如果這很重要,我正在使用引導模式。

我嘗試使用JavaScript,但我得到的最遠的是提醒相同的ID:

<script type="text/javascript">
    $(document).ready(function(){
        $("#demo").click(function(){
            alert("get content.");
        });
    });
</script>

最后,我需要php中的值,以便我可以運行其他MySQL查詢,

我不是100%知道您要做什么,但是我認為這是您想要的:

/functions/getUserRequest.php

這是清理請求的功能。 最好在使用前將其包括在頁面上。 這是可選的。

<?php
function getUserRequest($con,$user_id)
    {
        # Since mysql_ is deprecated/removed from new version, I will use PDO
        # safely binding the value
        $query = $con->prepare("select * from request, users where request.user_id = users.id and request.user_id = :0");
        $query->execute(array(":0"=>$user_id));
        while($result = $query->fetch(PDO::FETCH_ASSOC)) {
            $row[] = $result;
        }

        return (isset($row))? $row : array();
    }

/functions/connection.php

這是數據庫連接,它將使用define()值作為連接憑據。 這是可選的,但應以更完整的方式實現。 在PHP7中刪除了mysql_ mysql_*

function connection()
    {
        return new \PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
    }

/index.php

# Here you would add a config with DB defines and such
# Add the functions
include_once(__DIR__.'/functions/connection.php');
include_once(__DIR__.'/functions/getUserRequest.php');
# Create connection
$con  = connection();
?>
<table border="1" cellpadding="3">
<?php
# Since Ids are supposed to be unique, you should make an auto-incrementer
$i = 0;
# Pass id here
foreach(getUserRequest($con,123) as $info) {
?>
    <tr class='my_loans_tr'>
        <?php
        /*
        ** Here is where the incrementing happens
        ** Also, pass the whole element to the js function
        */
        ?>
        <td  id='demo<?php echo $i ?>' onclick='myfunction(this)' class='weblex-show- detail' data-toggle='modal' data-target='#myModalw'><?php echo $info['loan_id'] ?></td>
        <td class='admin_amount'><?php echo $info['amount'] ?></td> 
        <td class='admin_points'><?php echo $info['points'] ?></td>     
        <td class='admin_date'><?php echo $info['req_date'] ?></td> 
        <td class='admin_status'><?php echo $info['status'] ?></td>
        <td class='admin_cancelled'><?php echo $info['cancelled_loan'] ?></td>
    </tr>
<?php
    $i++;
}
?>
</table>

然后,您的JS函數將啟動:

<script type="text/javascript">
function myfunction(obj)
    {
        // This should get the value between the <td>Value</td>
        var getValue = obj.innerHTML;
        // You should then be able to use AJAX to retrieve data with
        // inner value (or however you want to use this value)...
        alert(getValue);
    }
</script>


編輯:

由於您現在嘗試使用jQuery,因此您的ID都是唯一的,因此需要觸發對該類的點擊。

<script type="text/javascript">
    $(document).ready(function(){
        // You need to be listening for the click on the class "detail"
        $(".detail").click(function(){
            // This captures the current selected DOM object
            var obj      = $(this);
            // This will extract the value inside
            var objValue = obj.text();
            // This is where you send the data to a new page to get a response
            $.ajax({
                url: '/page/to/ajax/dispatcher.php',
                type: 'post',
                data: {
                    'id':objValue
                },
                success: function(response) {
                    // You can see the response in your console log
                    console.log(response);
                    // To update your html, you can just receive it from
                    // your ajax dispatch page and place it into the modal (or wherever)
                    $('#myModalw').html(response);
                }
            });
        });
    });
</script>

暫無
暫無

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

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