xtree使用 结合ajax操作数据库

关键:在xtree添加节点时,调用添加节点方法的同时,调用ajax的打开php文件,操作数据库

1.前台xtree  (官网 http://webfx.eae.net/dhtml/xtree/index.html)

<script>
//调用方法用ajax操作add_dbdata.php
var xmlHttp
function add_dbdata(str){
		
	//alert(str);
	if (str=="")
	{
	document.getElementById("txtHint").innerHTML="";
	return;
	}
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","add_dbdata.php?q="+str,true);
	xmlhttp.send();
}


function addNode() {
	var str=prompt("添加节点名称"); //点击节点时,可以弹出对话框,手动写节点的名称
  

	if (tree.getSelected()) {
		tree.getSelected().add(new WebFXTreeItem(str));
		add_dbdata(str);//调用方法用ajax操作add_dbdata.php,add_adbdata为操作数据库
	}
	//var strr=str;
	//alert(strr);
}

function addNodes() {
	var str=prompt("添加目录名称");

	if (tree.getSelected()) {
		var foo = tree.getSelected().add(new WebFXTreeItem(str));
		var bar = foo.add(new WebFXTreeItem('Sub 1'));
		var fbr = foo.add(new WebFXTreeItem('Sub 2'));
	}
}

function delNode() {
	if (tree.getSelected()) {
		tree.getSelected().remove();
	}
}
</script>

2、add_dbdata.php  (PHP文件 连接数据库,操作数据库)

<?php
header("Content-type:text/html;charset=utf8");
$q=$_GET["q"];
$q=(int)$q;
echo "<script>alter('kkkkkkk');</script> ";


$con = mysql_connect('localhost', 'root', '');
if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }

mysql_select_db("t2ecmall", $con);
mysql_query("set names utf8");

//$sql = " SELECT * FROM ecm_acategory ";
$sql = " insert into ecm_acategory (cate_id) values(999)";
echo $sql;
$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
</tr>";

//$row=mysql_fetch_array($result))!==false
//$row = mysql_fetch_array($result)
while($row=mysql_fetch_array($result))
 {
 echo "<tr>";
 echo "<td>" . $row['cate_id'] . "</td>";
 echo "<td>" . $row['cate_name'] . "</td>";
 echo "<td>" . $row['parent_id'] . "</td>";
 echo "<td>" . $row['code'] . "</td>";
 echo "</tr>";
 }
echo "</table>";

mysql_close($con);
?>


你可能感兴趣的:(xtree使用 结合ajax操作数据库)