php - 将值从数据库插入到 xml 表,并在 html 中使用 ajax 输出它们

标签 php mysql ajax xml

我有一个搜索框,用户可以在其中输入姓名,它将显示 "firstname"、"username"、"lastname"、"email"、"accountnumber"。到目前为止,我已经能够从数据库中获取数据,制作它的 xml 结构(这是学校的要求之一)。问题是如何将来自搜索框的值回显到 xml 表中,然后将结果输出到 HTML 表中?

数据库代码(文件名为 ajax-search.php):(我知道我正在使用 mysql,稍后我会修复它)

<?php 
header("Content-type: text/xml");
//Create Database connection
$db = mysql_connect("127.0.0.1","root","");
if (!$db) {
    die('Could not connect to db: ' . mysql_error());
}

//Select the Database
mysql_select_db("bank",$db);

$sSearchFor = $_GET['sSearchFor'];

$sql = "SELECT * FROM customers WHERE name LIKE '%$sSearchFor%'";
$result = mysql_query($sql, $db) or die(mysql_error());

//Create SimpleXMLElement object
$xml = new SimpleXMLElement('<xml/>');


//Add each column value a node of the XML object

while($row = mysql_fetch_assoc($result)) {
    $mydata = $xml->addChild('mydata');
    $mydata->addChild('Id',$row['id']);
    $mydata->addChild('Name',$row['name']);
    $mydata->addChild('user_name',$row['user_name']);
    $mydata->addChild('last_name',$row['last_name']);
    $mydata->addChild('email',$row['email']);
    $mydata->addChild('account_number',$row['account_number']);

}

//Create the XML file
$fp = fopen("employeeData.xml","a+");

//$fp = fopen("php://output","a+");

//Write the XML nodes
fwrite($fp,$xml->asXML()."\r\n" );

//Close the database connection
fclose($fp);

mysql_close($db);
?>

xml 代码(文件名为 xmltable.xml):

<?xml version="1.0" encoding="utf-8"?>
<searchresults>
  <name>test</name>
  <username>test</username>
  <lastname>test</lastname>
  <email>test.test@gmail.com</email>
  <accountnumber>93207802685726</accountnumber>
</searchresults>

ajax 的最终脚本在索引页面上:

$("#btnSearch").click(function () {
    var sSearchFor = $("#txtSearch").val();
    var searchLink = "ajax-search.php?sSearchFor=" + sSearchFor;
    $.ajax({
        type: "GET",
        url: "xmltable.xml",
        cache: false,
        dataType: "xml",
        success: function (xml) {
            $(xml).find('searchresults').each(function () {
                $(this).find("name").each(function () {
                    var name = $(this).text();
                    alert(name);
                });
            });
        }
    });
});

感谢所有帮助,因为我现在真的很迷茫。

最佳答案

客户端:

您忘记在您的网址中添加 searchLink!

    $("#btnSearch").click(function () {
    var searchLink = "ajax-search.php";
    $.ajax({
        type: "POST",
        url: searchLink,
        data: {sSearchFor : $("#txtSearch").val() },
        cache: false,
        dataType: "json",
        success: function (xml) {
            $(xml).find('searchresults').find('result').each(function () {
                var name = $(this).find("name").text();
                alert(name);
            });
        }
    });
});

服务器端:

在您的 .PHP 文件中使用它。我评论了处理文件保存的行:

<?php 
header("Content-type: text/xml");
//Create Database connection
$db = mysql_connect("127.0.0.1","root","");
if (!$db) {
    die('Could not connect to db: ' . mysql_error());
}

//Select the Database
mysql_select_db("bank",$db);

if(isset($_POST['sSearchFor']))
    $sSearchFor = $_POST['sSearchFor'];
else
    $sSearchFor = "";

$sql = "SELECT * FROM customers WHERE name LIKE '%$sSearchFor%'";
$result = mysql_query($sql, $db) or die(mysql_error());

//Create SimpleXMLElement object
$xml = new SimpleXMLElement('searchresults');


//Add each column value a node of the XML object

while($row = mysql_fetch_assoc($result)) {
    $result= $xml->addChild('result');
    $result->addChild('id',$row['id']);
    $result->addChild('name',$row['name']);
    $result->addChild('username',$row['user_name']);
    $result->addChild('lastname',$row['last_name']);
    $result->addChild('email',$row['email']);
    $result->addChild('accountnumber',$row['account_number']);
}

// You can close BD now
mysql_close($db);

//Create the XML file
//$fp = fopen("employeeData.xml","a+");

//$fp = fopen("php://output","a+");

//Write the XML nodes
//fwrite($fp,$xml->asXML()."\r\n" );

//Close file
//fclose($fp);

echo $xml->asXML();

?>

希望对您有所帮助,祝您好运!

关于php - 将值从数据库插入到 xml 表,并在 html 中使用 ajax 输出它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29945936/

相关文章:

mysql - 您使用什么工具通过 JDBC 连接到 AWS Athena

javascript - 在 PHP 中发布选项文本而不是值

ajax - $jquery - $.ajax XML - .find .text

php - 将 Redis 用于超时队列或排行榜

php - PHP解析/语法错误;以及如何解决它们

mysql - 聚合函数GROUP BY和SUM返回的数据不一致?

mysql - MySQL 在 Ubuntu 16.04 上的安装错误

php - MySQL 为请求的每个字段返回 2 个值

php - 从远程服务器运行 PHP 代码?

javascript - 需要平均值 PHP、Jquery、Ajax