【问题标题】:onclick display the value of MySQL fetch echo on phponclick 在 php 上显示 MySQL fetch echo 的值
【发布时间】:2017-05-14 07:51:17
【问题描述】:

我有一张带有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 查询,

【问题讨论】:

  • 为什么在第一个查询中不使用 JOIN 来获取所有数据,而不必进行另一个查询?
  • 我想到了,2个不同的表,请求为每个贷款创建loan_id的位置,collected(table2) 贷款ID也在这里,但收集了一个字段名称,所以我可以有loanid 147和1 -10 收集的属于loan_id 147 的id,必须是toggle,不能让所有信息可见

标签: javascript php jquery mysql twitter-bootstrap


【解决方案1】:

我不是 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() 值作为连接凭据的数据库连接。这是可选的,但应该以更完整的方式实现。 mysql_* 在 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>

然后你的 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>

【讨论】:

  • 非常感谢 js 是我最需要的,pdo 我正在阅读它,因为我打算开始使用它,目前正在使用 mysql 和 mysqli
  • 你读了我的,因为我知道我正在搜索如何将此值传递给 php 变量
  • 你必须使用Ajax,我建议使用jQuery ajax,它非常简单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-08
  • 2015-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多