簡體   English   中英

使用php Javascript滾動表格中的記錄

[英]scroll through the records in a table with php Javascript

我有一個html表,通過php(mysql)打印數據。 我希望通過按下每個記錄旁邊的按鈕,我會顯示相關記錄的值並打印顯示的值。

我幾乎成功了,但每次按下時我只拿第一個記錄按鈕,而不是同一行。

 <?php
            $data = 'sito';
            $db = mysqli_connect('localhost', 'root', '', $data);
            $result= mysqli_query ($db, "SELECT * FROM prodotti");
            echo"<table id='tabella' width='800' border='1'>";
                echo"<tr>
                  <th >Codice</th>
                  <th >Nome</th>
                  <th >Giacenza</th>
                  <th> Azioni </th>
                  <th >Data consegna</th>
                </tr>";
                while($row=mysqli_fetch_array($result)){
           echo"<tr>
                  <td id='cod'>".$row['Codice']."</td>
                  <td id='nom'>".$row['Nome']."</td>
                  <td id='gia'>".$row['Giacenza']."</td>
                  <td id='dat'>
                    <button id='pulsante'  onClick='giacenza()'>pulsante</button>
                  </td>
                  <td id='datac'>";
                        $dat=$row['Data consegna'];
                        $data=substr($dat,8,2)."-".substr($dat,5,2)."-".substr($dat,0,4);
                        echo $data."</td>
                </tr>";}
            echo"</table>";
    ?>

這里的php代碼抱歉

var d = document.createElement("div");
d.setAttribute("id", "modifica");
var String = "<textarea id='textarea' rows='1' cols='3'></textarea> <input type='text' id='input'></input>          <button type='submit' id = 'boton'>aggiorna </button>";
d.innerHTML = String;
var g = document.getElementById("gia").textContent;
document.body.appendChild(d);
document.getElementById("textarea").innerHTML = g; 

您放在每條記錄旁邊的按鈕都是相同的。 您不會以任何方式知道您按下哪個按鈕。 嘗試這樣的事情:

<button id='pulsante'  onClick='giacenza(".$row['Codice'].")'>pulsante</button>

並更改您的giacenza javascript函數以將Codice作為參數並使用它來識別正在操作的記錄。

我認為Codice是你的物品代碼或記錄中獨有的東西。 如果你沒有這樣的領域你需要提出一些東西。

您還需要其他任何內容的唯一標識符,例如您正在操作的文本框。 而不是使用"MyBox" id使用"MyBox_".$row['Codice'] id "MyBox_".$row['Codice']

我建議將值作為參數傳遞給函數

PHP代碼

<?php
    $data = 'sito';
    $db = mysqli_connect('localhost', 'root', '', $data);
    $result= mysqli_query ($db, "SELECT * FROM prodotti");
    echo"<table id='tabella' width='800' border='1'>";
    echo"<tr>
            <th >Codice</th>
            <th >Nome</th>
            <th >Giacenza</th>
            <th> Azioni </th>
            <th >Data consegna</th>
          </tr>";
    while($row=mysqli_fetch_array($result)){
       echo"<tr>
              <td id='cod'>".$row['Codice']."</td>
              <td id='nom'>".$row['Nome']."</td>
              <td id='gia'>".$row['Giacenza']."</td>
              <td id='dat'>
              <!-- Next line has the change -->
                <button id='pulsante' onClick='giacenza(/"".$row['Giacenza']."/")'>pulsante</button>
              </td>
              <td id='datac'>";
                    $dat=$row['Data consegna'];
                    $data=substr($dat,8,2)."-".substr($dat,5,2)."-".substr($dat,0,4);
                    echo $data."</td>
            </tr>";
    }
    echo"</table>";
?>

和你的javascript代碼:它不是從元素中獲取值,而是使用傳遞參數中的值

function giacenza(g){
   var d = document.createElement("div");
   d.setAttribute("id", "modifica");
   var String = "<textarea id='textarea' rows='1' cols='3'></textarea> <input type='text' id='input'></input>          <button type='submit' id = 'boton'>aggiorna </button>";
   d.innerHTML = String;
   document.body.appendChild(d);
   document.getElementById("textarea").innerHTML = g;
}

暫無
暫無

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

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