【问题标题】:How do I submit a POST variable with Javascript?如何使用 Javascript 提交 POST 变量?
【发布时间】:2012-01-31 18:15:31
【问题描述】:

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

不管怎样,现在是按钮:

<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”名称传递到其他页面?

【问题讨论】:

  • 您的查询容易受到SQL injection的攻击。
  • 显示更多的 HTML。你的&lt;button&gt;&lt;form&gt; 里面吗?
  • 您可以恢复到提交输入并将事件附加到表单的提交。无需使用按钮。
  • 我最初是这样做的,Yoa,但我有另一个按钮,我不知道如何制作一个脚本来检测这两个按钮之间的差异。

标签: php javascript html


【解决方案1】:

你可以使用&lt;input type="hidden"&gt;:

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

这应该呈现如下内容:

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

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

$org_id = $_POST['org_id'];

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我已经在使用onsubmit了,但是我有两个按钮,需要区分这两个。
    • 将其中一个按钮放在&lt;/form&gt;标签之后,则不会提交表单
    【解决方案3】:

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

    【讨论】:

    • 注意w3fools。试试MDN
    • @paislee:直到现在才知道那个网站,但他们似乎对他们的大部分问题都过于夸张了。他们让 w3schools.com 看起来完全没用,每个人都应该避免它,否则他们会养成有史以来最糟糕的习惯,但随后它会抱怨诸如“上面的示例适用于大多数浏览器,但不要依赖它” . 忘记结束标签会产生意想不到的结果或错误”只是因为他们没有提到&lt;p&gt; 中的结束标签是可选的?让我休息一下。很多专业人士都从 w3schools.com 开始,并且做得很好。
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 2011-11-02
      • 2011-05-24
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 2014-01-22
      • 2014-01-20
      相关资源
      最近更新 更多