$ _GET总是返回1

时间:2012-07-09 07:58:48

标签: php html ajax forms html-table

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

这是一个标记:

    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'];}

    ?>

有谁知道问题出在哪里?

2 个答案:

答案 0 :(得分:1)

编写Value属性的

<input type='hidden' 
       name='delete' 
       value="<?php echo $row['id_content'] ?>" 
       id='delete' />

答案 1 :(得分: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>";  
    }