php - Insert values from database to xml table and output them with ajax in html -


i have search box user can type in name , display "firstname", "username", "lastname", "email", "accountnumber". far have been able data database, make xml structure of (it 1 of requirements in school). question how can echo values come search box xml table , output result html table?

code database (file called ajax-search.php): (i know using mysql , fix later)

<?php  header("content-type: text/xml"); //create database connection $db = mysql_connect("127.0.0.1","root",""); if (!$db) {     die('could not connect db: ' . mysql_error()); }  //select database mysql_select_db("bank",$db);  $ssearchfor = $_get['ssearchfor'];  $sql = "select * customers name '%$ssearchfor%'"; $result = mysql_query($sql, $db) or die(mysql_error());  //create simplexmlelement object $xml = new simplexmlelement('<xml/>');   //add each column value node of 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 xml file $fp = fopen("employeedata.xml","a+");  //$fp = fopen("php://output","a+");  //write xml nodes fwrite($fp,$xml->asxml()."\r\n" );  //close database connection fclose($fp);  mysql_close($db); ?> 

code xml, (file called 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 final script ajax on index page:

$("#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 since lost right now.

cient side:

you forgot add searchlink in url!

    $("#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 on .php file. i've commented lines deal file saving:

<?php  header("content-type: text/xml"); //create database connection $db = mysql_connect("127.0.0.1","root",""); if (!$db) {     die('could not connect db: ' . mysql_error()); }  //select database mysql_select_db("bank",$db);  if(isset($_post['ssearchfor']))     $ssearchfor = $_post['ssearchfor']; else     $ssearchfor = "";  $sql = "select * customers name '%$ssearchfor%'"; $result = mysql_query($sql, $db) or die(mysql_error());  //create simplexmlelement object $xml = new simplexmlelement('searchresults');   //add each column value node of 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']); }  // can close bd mysql_close($db);  //create xml file //$fp = fopen("employeedata.xml","a+");  //$fp = fopen("php://output","a+");  //write xml nodes //fwrite($fp,$xml->asxml()."\r\n" );  //close file //fclose($fp);  echo $xml->asxml();  ?> 

hope helps , luck!


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -