简体   繁体   English

如何使用Javascript提交POST变量?

[英]How do I submit a POST variable with Javascript?

So I am trying to submit a variable and the name of the variable via a form. 所以我试图通过表单提交变量和变量的名称。 I switched a button from submit to button because I need additional validation. 我从提交到按钮切换了一个按钮,因为我需要额外的验证。

Anyway, here's the button now: 无论如何,现在是按钮:

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

Here's my current validation: 这是我目前的验证:

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

}
</script>

And here's my script on the other side: 这是另一方面的脚本:

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);
}

It is not currently deleting the records that I want. 它目前没有删除我想要的记录。 How do I pass along the "del" name to the other page? 如何将“del”名称传递给其他页面?

You can use <input type="hidden"> : 您可以使用<input type="hidden">

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

This should render something like: 这应该呈现如下:

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

Using this code you can access the hidden field data using: 使用此代码,您可以使用以下方法访问隐藏的字段数据

$org_id = $_POST['org_id'];

instead use onsubmit 而是使用onsubmit

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

and

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

}
</script>

edit: you can also change 编辑:你也可以改变

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

to

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

but i think this line is your problem 但我认为这一行是你的问题

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

try 尝试

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

Looking at http://www.w3schools.com/tags/tag_button.asp suggests that the 'name' of a button isn't submitted as opposed to its 'value' or 'text'. 查看http://www.w3schools.com/tags/tag_button.asp表明,未提交按钮的“名称”而不是“值”或“文本”。 Why not use an input of type hidden as just suggested? 为什么不使用隐藏类型的输入?

I suggest you rethink using a form at all and consider AJAX . 我建议你重新考虑使用表格并考虑AJAX This would solve the problem of knowing which button was clicked and the page doesn't need to reload. 这将解决了解单击哪个按钮并且页面不需要重新加载的问题。

Here is a sample of what you're trying to do: 以下是您要尝试的示例:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM