繁体   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