我有一个来自我的数据库的行列表,每行旁边都有删除,但无论如何,ajax总是删除列表中的第一行。
PHP CODE>>>>>>
$allowedFunctions = array(
'add_job',
'get_log',
'delete'
);
$functionName = $_GET[ 'func' ];
if( in_array( $functionName, $allowedFunctions ) && function_exists( $functionName ) )
{
$functionName();
}
PHP显示页面>>>>>>
function manage(){
dbconnect();
$qry="SELECT server,id,email,lstcheck,running,runtime,pid FROM adds";
$result=mysql_query($qry);
$i = 0;
echo"<table id='customers'>
<tbody><tr>
<th>Server</th>
<th>Id</th>
<th>Email</th>
<th>Check Date</th>
<th>Delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "
";
if($i%2 == 0)
{
$tr="<tr>";
$i++;
} else {
$tr="<tr class='alt'>";
$i++;
}
echo"$tr
<td><input type='text' name='sip' id='sip' size='10' value='".$row['server']."' /></td>
<td><input type='text' name='id' id='id' size='10' value='".$row['id']."'/></td>
<td><input type='text' name='email' id='email' size='10'value='".$row['email']."' /></td>
<td><input type='text' name='lstchk' id='lstchk' size='10'value='".$row['lstcheck']."' /></td>
<td><input type='submit' name='Submit' id='Submit' onClick=\"getAjaxInfom('delete')\" value='Remove' /></td>
</tr>";
}
echo"</tbody></table><div id='resultm'></div>";
}
删除功能&gt;&gt;&gt;&gt;&gt;
function delete() {
dbconnect();
$sip = $_GET[ 'sip' ];
$id = $_GET[ 'id' ];
$email = $_GET[ 'email' ];
$lstchk = $_GET[ 'lstchk' ];
$ok = $_GET[ 'ok' ];
mysql_query("delete from adds where id='$id'");
echo $sip .$id.$email;
}
AJAX代码&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;
function getAjaxInfom(f) { // submit buton function
var myFunction= f;
var sip = document.getElementById("sip").value;
var id = document.getElementById("id").value;
var email = document.getElementById("email").value;
var lstchk = document.getElementById("lstchk").value;
var url = "includes/func.php?func=" + myFunction + "&sip=" + sip +"&id=" + id +"&email=" + email +"&lstchk=" + lstchk +"";
request.open("GET", url, true);
request.onreadystatechange = updatePage1;
request.send(null);
}
function updatePage1() {
if (request.readyState == 4) {
if (request.status == 200) {
var response = request.responseText;
document.getElementById("resultm").innerHTML = response; //responce for submit
} else
alert("status is " + request.status);
}
}
需要一些帮助
答案 0 :(得分:0)
看起来每行中的输入字段都得到相同的ID - 这是一个非常糟糕的主意。尝试使id是唯一的,它应该运行良好。也许您可以将$i
- 值添加到每个ID id='id_".$i."'
,将$i
- 值作为第二个参数放入getAjaxInfom调用(onClick=\"getAjaxInfom('delete','".$i."')\"
)并编辑{{ 1}}做getAjaxInfom
之类的事情(以及其他值)