drhqkz3455 2016-12-30 00:18
浏览 43
已采纳

onclick在php上显示MySQL fetch echo的值

I have a table that has a loan_id and when I fetch the info, it is ok. This problem is I need to click on the loan_id number then let it display result based on the id number.

<?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>";  
?>

enter image description here

The number 146 147 are loan id's, so basically what I need is to click on the id number then it passes me the id number (any number I click) so I can then run a new query based on the loan_id. Each loan_id has more info stored in another table. I'm using bootstrap modal, if that matters.

I tried JavaScript but the furthest I got was to alert the same id:

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

in the end i need the value in php so i can run other MySQL query,

  • 写回答

1条回答 默认 最新

  • doudou0111 2016-12-30 04:40
    关注

    I am not 100% sure what you are trying to do, but I think this is what you are looking for:

    /functions/getUserRequest.php

    This is a function to clean up the request. Best to just include it on the page before using it. This is optional.

    <?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

    Here is the database connection which would use define() values for the connection credentials. This is optional, but should be implemented in a more complete fashion. mysql_* is removed in PHP7.

    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>
    

    Then your JS function would start:

    <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>
    


    EDIT:

    Since you are trying to use jQuery now, you need to trigger your clicks on the class since your ids are all unique.

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号