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

时间:2022-09-25 15:52:10

I have a search box where user can type in a name and it will display "firstname", "username", "lastname", "email", "accountnumber". So far I have been able to get the data from database, make xml structure of it (it was one of the requirements in the school). The question is how can I echo the values that come from search box into the xml table and then output the result into the HTML table?

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

Code for the database (file is called ajax-search.php): (I know I am using mysql and I will fix that later)

数据库代码(文件名为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);
?>

Code for the xml, (file is called xmltable.xml):

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>

And the final script for the ajax is on the index page:

并且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);
                });
            });
        }
    });
});

I appreciate all the help since I am really lost right now.

我很感激所有的帮助,因为我现在真的迷失了。

1 个解决方案

#1


0  

Cient side:

You forgot to add searchLink in your url!

你忘了在你的网址中添加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);
            });
        }
    });
});

Server side:

Use this on your .PHP file. I've commented the lines that deal with file saving:

在.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();

?>

Hope it helps and good luck!

希望它有所帮助,祝你好运!

#1


0  

Cient side:

You forgot to add searchLink in your url!

你忘了在你的网址中添加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);
            });
        }
    });
});

Server side:

Use this on your .PHP file. I've commented the lines that deal with file saving:

在.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();

?>

Hope it helps and good luck!

希望它有所帮助,祝你好运!