在$ _POST中添加参数?

时间:2011-04-02 16:31:52

标签: php post

我一直在尝试使用$_GET来尝试管理已标记帐户的日志文件,但这种新情况并不完全正常。

if (isset($_GET['marked'])) {
        if ($_GET['marked'] == 'read') {
            if (isset($_POST['mark'])) {
                foreach ($_POST['mark'] as $mark) {
                    mysql_query("UPDATE logged SET status = '1' WHERE timeLogged = $mark");
                    echo "<meta http-equiv='refresh' content='0;url=/admincpanel/flagged.php'>";
                }
            }
        }
        else if ($_GET['marked'] == 'unread') {
            if (isset($_POST['mark'])) {
                foreach ($_POST['mark'] as $mark) {
                    mysql_query("UPDATE logged SET status = '0' WHERE timeLogged = $mark");
                    echo "<meta http-equiv='refresh' content='0;url=/admincpanel/flagged.php'>";
                }
            }
        }
        else if ($_GET['marked'] == 'save') {
            if (isset($_POST['mark'])) {
                foreach ($_POST['mark'] as $mark) {
                    mysql_query("UPDATE logged SET status = '2' WHERE timeLogged = $mark");
                    echo "<meta http-equiv='refresh' content='0;url=/admincpanel/flagged.php'>";
                }
            }
        }
    }

我有按钮:Mark as <a href=\"?marked=read\" onclick=\"submitForm();\"><i>read</i></a><a href=\"?marked=unread\" onclick=\"submitForm();\">/unread</a>

我正在使用复选框来选择我想要的行和之前没有$ _GET的默认submitForm();工作得很好。

这有效:Mark as <a href=\"#\" onclick=\"submitForm();\"><i>read</i></a>

    if (isset($_POST['mark'])) {
        foreach ($_POST['mark'] as $mark) {
            mysql_query("UPDATE logged SET status = '1' WHERE timeLogged = $mark");
            echo "<meta http-equiv='refresh' content='0;url=/admincpanel/flagged.php'>";
        }
    }

如何让我的其他标记(读取,未读,保存)起作用?提交表单并获取结果似乎是$_POST的唯一工作,所以我必须向$_POST添加更多参数吗?

如果它有助于任何这是javascript:

function submitForm() {
    document.logs.submit();
}

总结一下,我正在尝试将读取更新状态设置为1,未读状态为0,并保存状态2.使用$_GET无效 - 或者我做错了。那么我该怎样做才能使这项工作?

谢谢。

3 个答案:

答案 0 :(得分:2)

HY 尝试在表单中添加隐藏字段:

<input type="hidden" name="marked" id="marked" value="" />

然后,按钮:

`标记为

<a href=\"?marked=read\" onclick=\"submitForm('read');\"><i>read</i></a><a href=\"?marked=unread\" onclick=\"submitForm('unread');\">/unread</a>`

然后是JavaScript函数:

function submitForm(mark) {
  document.getElementById('marked').value = mark;
  document.logs.submit();
}`

然后,在php脚本中使用$ _REQUEST ['marked'] insted of $ _GET

答案 1 :(得分:1)

href="?marked=read"永远不会发送到服务器,因为您正在提交表单。

要解决此问题,您可以向submitForm()函数添加一个参数,并将此参数存储为名为“marked”的隐藏表单输入的值。

像这样:

Mark as <a href="#" onclick="submitForm('read')">read</a>

function submitForm(marked) {
  // TODO: Setup hidden input field with the marked argument
  document.logs.submit();
}

答案 2 :(得分:1)

尝试使用这个PHP代码,更简洁一点:

if (isset($_GET['marked'])) {
    if ($_GET['marked') == 'read')
        $status = 1;
    elseif ($_GET['marked'] == 'unread')
        $status = 0;
    else
        $status = 1;
    }
if (isset($_POST['mark'])) {
    foreach ($_POST['mark'] as $mark) {
        mysql_query("UPDATE logged SET status = $status WHERE timeLogged = $mark");
        echo "<meta http-equiv='refresh' content='0;url=/admincpane/flagged.php'>";
        }
    }