如何使用Javascript提交POST变量


How do I submit a POST variable with Javascript?

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

不管怎样,现在的按钮是:

<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"名称传递到另一页?

您可以使用<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'];

改为使用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."' ");

查看http://www.w3schools.com/tags/tag_button.asp表明按钮的"名称"未提交,而不是其"值"或"文本"。为什么不使用刚才建议的隐藏类型的输入呢?

我建议您重新考虑使用表单,并考虑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>