将行内容显示到表单

时间:2016-10-11 03:08:25

标签: php html sql

我有一个显示搜索数据的表格。 我想要发生的是当我点击该行后,点击该行的数据将以表格形式显示。

显示表格: 的search.php

echo '<table id="patientTable" class="w3-table-all w3-hoverable">'
echo '<tr class="w3-yellow"><th>Admission No</th><th>Hospital No</th><th>Patient Name</th></tr>';
                while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC) ) {
                    echo '<tr onclick=patientClick(this)>';
                        foreach($row as $key=>$value) {
                        echo '<td>',$value,'</td>';
                        //echo $i;
                    }
                    echo '</tr>';
                }
            echo '</table><br />';

尝试连接到表单的脚本

function patientClick(x){
    var rowInd = x.rowIndex;
    var adNo = document.getElementById("patientTable").rows[rowInd].cells[0].innerHTML
    var hosNo = document.getElementById("patientTable").rows[rowInd].cells[1].innerHTML;
    var patname = document.getElementById("patientTable").rows[rowInd].cells[2].innerHTML;
    alert(adNo +"-"+ hosNo +"-"+ patname);//check to display row content

    var jsObj = {patname:patname, adNo:adNo, hosNo:hosNo}
    $.post('addPN.php', {data:jsObj}, function(data){
        $('#displayPatient').html(data);
    });
}

要显示的表单,以便保存更多数据 addPN.php

    <?php
    $arr = json_decode(json_encode($_POST["data"]),true);
    $adNo = $arr[0]['adNo'];
    $hosNo = $arr[1]['hosNo'];
    $patname = $arr[2]['patname'];

    echo $adNo;//check to display
    echo $hosNo;//check to display
    echo $patname;//check to display
?>
<html>
    <body>
        <form action = "savePN.php" id = "savePNotes" method = "POST">
            <input type = "submit" id = "updateButton" value = "Save">
            <input type = "text" readonly = "true" id = "adNo" name = "adNo" value = "<?php echo $adNo;?>">
            <input type = "text" readonly = "true" id = "hosNo" name = "hosNo" value = "<?php echo $hosNo;?>">
            <input type = "text" readonly = "true" id = "patname" name = "patname" value = "<?php echo $patname;?>">
            <input type = "text" id = "cn" name = "cn" placeholder = "Contact Number">
            <input type = "text" id = "cadd" name = "cadd" placeholder = "Address">
            <input type = "text" id = "ctype" name = "ctype" placeholder = "Type">
        </form>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"] . "<br>";
     }
} else {
     echo "0 results";
}

$conn->close();
?>

http://www.w3schools.com/php/php_mysql_select.asp

答案 1 :(得分:0)

使用php作为客户端这是一种不好的做法,但是ajax是一种无法在服务器端立即重定向的通信方式

before_validation :downcase_subdomain

在你的服务器端你应该解码你的json

使用它  $ data = function patientClick(x){ var rowInd = x.rowIndex; var adNo = document.getElementById("patientTable").rows[rowInd].cells[0].innerHTML var hosNo = document.getElementById("patientTable").rows[rowInd].cells[1].innerHTML; var patname = document.getElementById("patientTable").rows[rowInd].cells[2].innerHTML; alert(adNo +"-"+ hosNo +"-"+ patname);//check to display row content //here you must construct some JSON object for your data var jsObj = { pathname: pathname, adNo: adNo, hosNo: hosNo } $.post('searchPatient.php', JSON.stringify({ data : jsObj}) , function(data){ $("#displayPatient").html(data); }); }  echo $ data [&#34; adNo&#34;];

我认为你的结构不好,我认为不可能做你想做的事,因为表格来自php和不同的文件,如果你只使用普通的html表单可能它可以使用,所以可以轻松操作表单使用DOM