0

我在将 GET 变量传递到另一个 PHP 页面时遇到了问题。我有一个简单的目录。我想使用 ajax 删除行。Ajax 在带有表单的简单示例上完美运行。

这是一个标记:

    while($row=mysql_fetch_array($query))
    {
/*here are rows(content) */

        <form action='#' method='get' onsubmit='deleteContent(); return false'>
        <td><input type='hidden' name='delete' value='$row[id_content]' id='delete' />
        <input type='submit' class='del'  name='delete' title='Delete' value='Delete' /></td>
        </tr></form>";  
    }

当我按下提交时,它总是返回 1。这是 DeleteContent

  function deleteContent()//za brisanje dela
{
    try
    {
        xhr = new XMLHttpRequest();
    }
    catch (e)
    {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (xhr == null)
    {
        alert("Vaš brskalnik ne podpira AJAX-a!");
        return;
    }
    var url = "delete.php?delete=" + document.getElementById('delete').value;

    xhr.onreadystatechange = handler2; 
    xhr.open("GET", url, true);
    xhr.send(null);
}

function handler2()
{
    if (xhr.readyState == 4)
    {
        if (xhr.status == 200)

            document.getElementById("delete").innerHTML = xhr.responseText;
        else
            alert("error!");
    }
}

这是delete.php

            <?php
            echo "<span>Content delete</span><br/>";
            if($_GET['delete'])
            {echo $_GET['delete'];}

    ?>

有谁知道问题出在哪里?

4

2 回答 2

1

写入属性Value

<input type='hidden' 
       name='delete' 
       value="<?php echo $row['id_content'] ?>" 
       id='delete' />
于 2012-07-09T08:01:53.487 回答
0

ID 在文档中必须是唯一的。

document.getElementById('delete')将始终指向同一个元素。
将使用的表单作为参数提供给 deleteContent(),您将能够检索正确的值:

<form action='#' method='get' onsubmit='return deleteContent(this);'>

……

function deleteContent(form)//za brisanje dela
{
    try
    {
        xhr = new XMLHttpRequest();
    }
    catch (e)
    {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (xhr == null)
    {
        alert("Vaš brskalnik ne podpira AJAX-a!");
        return;
    }
    var url = "delete.php?delete=" + form.delete.value;

    xhr.onreadystatechange = handler2; 
    xhr.open("GET", url, true);
    xhr.send(null);
    return false;
}

还有什么:您的标记似乎无效,请使用例如

while($row=mysql_fetch_array($query))
    {
/*here are rows(content) */

        echo "<tr><td>
              <form action='#' method='get' onsubmit='return deleteContent(this);'>
               <input type='hidden' name='delete' value='{$row[id_content]}' id='delete' />
               <input type='submit' class='del'  name='delete' title='Delete' value='Delete' />
              </form>
             </td></tr>";  
    }
于 2012-07-09T08:22:29.860 回答