|
Posted by sahm on 01/21/08 11:14
HI every one
I have problem for deleting data from table using ajax
this code to insert user data and show the data in table using ajax it
work fine but for another code that use to delete data and show the
date in same table it dose not work
and this are my codes
this code for show user data
/////////////////////////////////////////////
<form method="get" action="insert_user.php">
<tr>
<td align=left width='15%'>
<tr>
<td align=left><label>Employee Name:</label></td>
<td align=left width=90%><input type="text"
id="employee_name" value="" size=30></td>
</tr>
<tr>
<td align=left>User Name:</td>
<td align=left><input type="text" id="user" value=""
size=30></td>
</tr>
<tr>
<td align=left>Password:</td>
<td align=left><input type="password" id="password" value=""
size=30></td>
</tr>
<tr>
<td align=left>E-Mail:</td>
<td align=left><input type="text" id="e_mail" value=""
size=30></td>
</tr>
<tr>
<td align="left">User Department:</td>
<td align="left">
<select id="department">
<option value="Management">Management</option>
<option value="Accounting">Accounting</option>
<option value="Sales">Sales</option>
</select>
</td>
</tr>
</td>
</tr>
<tr>
<td colspan=2 align=left>
<input type="reset" value="Reset">
<input type="button" value="Submit"
onclick="insertUser(employee_name.value, user.value, password.value,
e_mail.value, department.value)">
</td>
</tr>
</form>
<tr>
<td colspan="2" align="left">
<h2>User Table</h2>
</td>
</tr>
<tr>
<td colspan='2' align='left'>
<form method="get" action="delete_user.php">
<span id='employee'>
<table border=1 width='500'>
<tr>
<th>Employee Name</th>
<th>Employee Department</th>
</tr>
<?
$sql = "select * from user";
$result = mysql_query($sql);
if(@mysql_num_rows($result)!=0)
{
while ($data = mysql_fetch_assoc($result))
{
echo "<tr>\n";
echo "<td>\n";
echo $data['employee_name'];
echo "</td>\n";
echo "<td>\n";
echo $data['employee_Department'];
echo "</td>\n";
echo "<td>\n";
//echo "<input type='button' value='Delete'
onclick='deleteUser(".$data['employee_name'].")'>\n";
echo<<<HERE
<input type='button' value='Delete'
onclick="deleteUser($data[employee_name])">\n
HERE;
echo "</td>\n";
echo "</tr>\n";
}
}
?>
</table>
</span>
</form>
//////////////////////////////////////////
and this is for ajax inser data
////////////////////////////////////////////
var xmlHttp;
function insertUser(emp, user, pass, email, depar)
{
if (emp.length==0)
{
document.getElementById("employee").innerHTML=""
return
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="insert_user.php"
url=url+"?employee_name="+emp+"&user="+user+"&password="+pass
+"&e_mail="+email+"&department="+depar
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("employee").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
//////////////////////////
and this php for insert user data
/////////////////////////////////////////
$sql = "insert into user VALUES(null, '$employee_name', '$user',
'$password', '$e_mail', '$department')";
$result = mysql_query($sql);
if($result)
{
$sql = "select * from user";
$result = mysql_query($sql);
if(@mysql_num_rows($result)!=0)
{
echo "<table border=1 width='500'>";
echo "<tr>";
echo "<th>Employee Name</th>";
echo "<th>Employee Department</th>";
echo "</tr>";
while ($data = mysql_fetch_assoc($result))
{
echo "<tr>\n";
echo "<td>\n";
echo $data['employee_name'];
echo "</td>\n";
echo "<td>\n";
echo $data['employee_Department'];
echo "</td>\n";
echo "<td>\n";
echo "<input type='button' value='Delete' onclick='deleteUser(".
$data['employee_name'].")'>\n";
echo "</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
}
//////////////////////////////////
and this is for ajax deleting data
var xmlHttp;
function deleteUser(emp)
{
alert("HI2");
if (emp.length==0)
{
document.getElementById("employee").innerHTML=""
return
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp1==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="delete_user.php"
url=url+"?employee_name="+emp
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("employee").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
///////////////////////
and this for php delete user data
//////////////////////////////////
$sql = "delete from user where employee_name ='$employee_name'";
$result = mysql_query($sql);
if($result)
{
$sql = "select * from user";
//$result =
$result = mysql_query($sql);
if(@mysql_num_rows($result)!=0)
{
echo "<table border=1 width='500'>";
echo "<tr>";
echo "<th>Employee Name</th>";
echo "<th>Employee Department</th>";
echo "</tr>";
while ($data = mysql_fetch_assoc($result))
{
echo "<tr>\n";
echo "<td>\n";
echo $data['employee_name'];
echo "</td>\n";
echo "<td>\n";
echo $data['employee_Department'];
echo "</td>\n";
echo "<td>\n";
echo "<input type='button' value='Delete' onclick='deleteUser(".
$data['employee_name'].")'>\n";
echo "</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
}
////////////////////////
and
thank you for your help
Best
Salim
Navigation:
[Reply to this message]
|