簡體   English   中英

PHP Ajax回顯HTML表

[英]PHP Ajax echo HTML table

將表格中的一些數據從PHP輸出到HTML時出現問題。 當我點擊Submit時,輸出沒有真正格式化為HTML表。 代碼已更新,因此現在一切正常。 在此處輸入圖片說明

<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html401/loose.dtd">
<html>
    <head>
        <title>AJAX Database</title>
    </head>
    <body>
        Type in the location <input type="text" id="name">
        <input type="submit" id="name-submit" value="Check Inventory">
        <div id="name-data"></div>

        <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
        <script src="js/global.js"></script>
    </body>
</html>

<div id="name-data"></div>就像是單擊“提交”按鈕后所有輸出的占位符。
這是PHP代碼。 我試圖在PHP代碼之外添加HTML標記,但無法正常工作。

<?php
    // '.post' could be '.get' here but global.js has to be the same, .post is faster
    if(isset($_POST['name']) === true && empty($_POST['name']) === false) {
        require '../db/connect.php';
        // id, location, quantity, count_sheet, remark
        $query = mysql_query("
            SELECT `sheet0_100`.`id`, `sheet0_100`.`location`, 
                `sheet0_100`.`quantity`, `sheet0_100`.`count_sheet`, `sheet0_100`.`remark`
            FROM `sheet0_100`
            WHERE `sheet0_100`.`location` = '" . mysql_real_escape_string(trim($_POST['name'])) . "'
        ");
        // echo (mysql_num_rows($query) !== 0) ? mysql_fetch_row($query) : 'Location not found..';
        if(mysql_num_rows($query) !== 0) {
            // Find out how many rows are available
            $rowsFound = @ mysql_num_rows($query);

            // Report how many rows were found
            echo "{$rowsFound} records found matching your criteria. \n";

            echo "There are $rowsFound items in this location.\n" . "\xA";
            echo "\n<table>";
            // Display a table contains all the items
            while ($rowsFound = mysql_fetch_row($query)) {
                // ... start a TABLE row ...
                echo "\n<tr>";
                // ... and print out each of the attributes in that row as a
                // separate TD (Table Data).
                foreach($rowsFound as $data)
                    echo "\n\t<td> {$data} </td>";
                    // Finish the row
                    echo "\n</tr>";
            }
            // Then, finish the table
            echo "</table>";
        }
        else {
            echo "Location not found..";
        }


    }
?>

我的JS文件,

$('input#name-submit').on('click', function() {
    var name = $('input#name').val();
    if ($.trim(name) != '') {
        // '.post' could be '.get' here but name.php has to be the same, .post is faster
        $.post('ajax/name.php', {name: name}, function(data) {
            // $('div#name-data').text(data);
            // jQuery('#name-data').html(RESPONSE);
            $('div#name-data').html(data);
        });
    }
});

使用jQuery('#name-data').html(RESPONSE);

請參閱: http//api.jquery.com/html/

如果要使用php,則應將文本存儲在這樣的變量中:

if(mysql_num_rows($query) !== 0) {
        // Find out how many rows are available
        $rowsFound = @ mysql_num_rows($query);

        // Report how many rows were found
        echo "{$rowsFound} records found matching your criteria. \n";

        echo "There are $rowsFound items in this location.\n" . "\xA";
        $table = "<table>";
        // Display a table contains all the items
        while ($rowsFound = mysql_fetch_row($query)) {
            // ... start a TABLE row ...
             $table .="<tr>";
            // ... and print out each of the attributes in that row as a
            // separate TD (Table Data).
            foreach($rowsFound as $data)
                 $table .= "<td> {$data} </td>";
                // Finish the row
                 $table .= "</tr>";
        }
        // Then, finish the table
         $table .= "</table>";
         echo $table;
    }
    else {
        echo "Location not found..";
    }

暫無
暫無

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

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