gpt4 book ai didi

javascript - onclick在php上显示MySQL fetch echo的值

转载 作者:行者123 更新时间:2023-11-29 11:06:04 25 4
gpt4 key购买 nike

我有一个带有 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>";
?>

enter image description here

数字146 147是贷款ID,所以基本上我需要的是点击ID号,然后它会将ID号传递给我(我点击的任何数字)这样我就可以根据 loan_id 运行新查询。每个 loan_id 都有更多信息存储在另一个表中。我正在使用 Bootstrap 模式,如果这很重要的话。

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

关于javascript - onclick在php上显示MySQL fetch echo的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41388994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com