如何使用Javascript提交POST变量?

时间:2012-01-31 18:15:32

标签: php javascript html

所以我试图通过表单提交变量和变量的名称。我从提交到按钮切换了一个按钮,因为我需要额外的验证。

无论如何,现在是按钮:

<button type="button" onclick="subForm()" name="del" id="deletebutton" value="'.$org.'">Delete</button>

这是我目前的验证:

<script type="text/javascript">
function subForm() 
{
    if(confirm("Are you sure you want to delete this?"))
        document.forms["addorg"].submit();
   else
       return false;

}
</script>

这是我在另一边的剧本:

if (isset($_POST["del"])  && ($_POST['del'] !== '')) {
    $del = mysql_real_escape_string(html2txt($_POST['del']));
    $resfile = mysql_query('SELECT file_loc from organization WHERE org_id = '.$del);
    $org_name = mysql_real_escape_string(html2txt($_POST['orgname']));

    if (!$resfile) 
        header('Location: '.$admin.'?error=query');

    while ($filerow = mysql_fetch_array($resfile)) {
        $fileplace = $filerow['file_loc'];
        unlink(".".$fileplace);
        rmdir($org_name);
    }

    mysql_query("DELETE from organization where org_id='".$del."'");
    header('Location: '.$admin);
}

它目前没有删除我想要的记录。如何将“del”名称传递给另一页?

4 个答案:

答案 0 :(得分:3)

您可以使用<input type="hidden">

echo '<input type="hidden" name="org_id" value="'.$org_id.'" />'

这应该像:

<input type="hidden" name="org_id" value="1" />

使用此代码,您可以使用以下方式访问隐藏的字段数据:

$org_id = $_POST['org_id'];

答案 1 :(得分:1)

改为使用onsubmit

<form method='POST' onsubmit='return subForm();'>

<script type="text/javascript">
function subForm() 
{
        if(confirm("Are you sure you want to delete this?"))
            return true;
       else
           return false;

}
</script>

编辑: 你也可以改变

if (isset($_POST["del"])  && ($_POST['del'] !== '')) {

    if ( !empty($_POST['del']) ) {

但我认为这一行是你的问题

$resfile = mysql_query('SELECT file_loc from organization WHERE org_id = '.$del);

$resfile = mysql_query("SELECT file_loc from organization WHERE org_id = '".$del."' ");

答案 2 :(得分:0)

查看http://www.w3schools.com/tags/tag_button.asp表示未提交按钮的“名称”,而不是“价值”或“文字”。为什么不按照建议使用隐藏类型的输入?

答案 3 :(得分:0)

我建议您重新考虑使用表单并考虑AJAX。这将解决了解单击哪个按钮并且页面无需重新加载的问题。

以下是您尝试执行的操作的示例:

<html>
<head>
    <script type="text/JavaScript">
        var xmlhttp;
        if (window.XMLHttpRequest)
            xmlhttp=new XMLHttpRequest();
        else
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

        function deleteOrganization(orgID)
        {
            xmlhttp.onreadystatechange = function()
            {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                {
                    // in your PHP file, have it echo something like "SUCCESS" or "FAIL", and the response will be alerted here
                    alert(xmlhttp.responseText);
                    refreshList();  // either call another function to update the list or return the new list after the success/fail response.
                }
            };
            xmlhttp.open("GET", "delete.php?orgID="+ orgID, true);
            // OR - xmlhttp.open("GET", "page.php?action=DELETE&orgID="+ orgID, true);
            xmlhttp.send();
        }

        function refreshList()
        {
            // either delete the row with JavaScript or make another AJAX call to get the new list after the entry was deleted.
        }
    </script>
</head>
<body>
    <button onclick="deleteOrganization('<?php echo $org; ?>')">Delete</button>
</body>
</html>