2016-12-30 25 views
0

我有一個表格,其中有一個loan_id,當我獲取信息時,它是可以的。這個問題是我需要點擊loan_id號碼,然後讓它顯示基於id號碼的結果。onclick在php上顯示MySQL獲取回顯的值

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

146147是貸款的ID,所以基本上我需要的是對身份證號碼點擊然後將其傳遞我的ID號(任意數量的我點擊)這樣的話我可以運行基於loan_id的新查詢。每個loan_id有更多的信息存儲在另一個表中。如果有問題,我使用引導模式。

我試過JavaScript,但我得到了最遠的是提醒同一ID:

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#demo").click(function(){ 
      alert("get content."); 
     }); 
    }); 
</script> 
到底我需要在PHP中的價值,所以我可以運行其他MySQL查詢

+1

爲什麼在第一次查詢中不使用JOIN來獲取所有數據,因此還沒有做另一個查詢? – Hokusai

+0

我想到了它,2個不同的表,請求哪裏loan_id創建每個貸款,收集(表2)貸款ID在這裏,但有一個字段名稱收集,所以我可以有loanid 147和1-10收集ID屬於loan_id 147,必須切換,無法顯示所有信息 –

回答

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()值的連接憑據數據庫連接。這是可選的,但應該以更完整的方式實施。在PHP7中刪除了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> 
+0

非常感謝js是我最需要的,pdo我正在閱讀它,因爲我打算開始使用它,目前正在使用mysql和mysqli –

+0

你讀我的我的,因爲我知道我正在尋找如何將這個值傳遞給一個PHP varialble –

+0

你必須使用Ajax,我會建議使用jQuery ajax,這是非常簡單的。 – Rasclatt