【问题标题】:php processing a dynamically generated formphp处理动态生成的表单
【发布时间】:2012-04-17 15:47:20
【问题描述】:

我正在使用 php 生成一个显示博客/线程项目的 html 页面,并且我正在使用 javascript 来显示/隐藏一些细节。问题是我正在为每组隐藏内容生成唯一 ID,其中包含处理输入的表单。在处理表单时,我需要知道编辑了哪个博客项目——我想使用 $_POST。我对 javascript 很陌生,我想可能有一个我可以在那里使用的解决方案。

我希望帖子将文本保存到 mysql 数据库(因此调用我正在工作的 php 函数之一)并告诉我文本是什么以及 threadId 是什么。

这是 php 代码片段,其中 $threadDetailItem 是一个数组,其中包含我的线程数据。

   foreach ($threadData as $threadDetailItem)
   {
    // display main line (a bunch of code here ...)


    // append button to edit or delete the post for admin
    if ( isset ($_SESSION['isAdmin']) && $_SESSION['isAdmin'] == 'Y'){
        // edit link opens content, and delete pops up a confirmation box
        $el = sprintf ("editThreadLink_%d", $threadDetailItem['blogThreadId']);
        $ec = sprintf ("editThreadContent_%d", $threadDetailItem['blogThreadId']);
        $link1 = sprintf ("<a id=\"%s\" href=\"javascript:toggle('%s', '%s');\">+</a>", $el, $ec, $el);
        $msg .= sprintf ("<li id=\"field6\">%s</li>\n", $link1);
    } 
    $msg .= "</ul>\n";
    echo $msg;

    // now that the row is printed, lets add the hidden content if admin so they can edit
    if ( isset ($_SESSION['isAdmin']) && $_SESSION['isAdmin'] == 'Y'){
        // hidden content to enable editing of the posting
        $msg = sprintf ("<div id=\"%s\" style=\"display: none\">\n", $ec);
        echo $msg;

        echo "<form name=\"form\" method=\"post\" action=\"\">\n";
        $msg = sprintf ("<textarea id=\"%s\" name=\"%s\">%s</textarea>\n", 
            $ec, $ec, $threadDetailItem['threadTitle']);
        echo $msg;
        $msg = sprintf ("<button type=\"submit\"> %s</button>\n", $lang->get('BLOG POST'));
        echo $msg;
        echo "</form>\n";
        echo "</div>";
    }
}

非常感谢有关处理此事件的好方法的建议。提前致谢。

数据中的字段有:blogThreadId、threadTitle、username、createdOn、lastUpdated、displayed(未使用)和threadDetails(包含发帖信息的数组)。

【问题讨论】:

  • 你要处理什么事件??还添加样本$threadData数据
  • 我正在考虑在我的表单中添加一个隐藏字段,这将使常规帖子能够知道treadId,这是用户更新字段时的事件。
  • $threadData 是一个实例,可以在我的问题底部放入的调试函数中打印出来。谢谢。
  • 你想在点击按钮时将表单输入值保存到数据库中,对吧?
  • 是的 - 我会将值保存到数据库,然后重新显示页面。我想我可以使用隐藏字段来做到这一点,并且当/如果可行时会返回。

标签: php javascript post get


【解决方案1】:

我可以使用 $_POST 和隐藏字段中的 ID 来让我的 php 脚本知道正在编辑哪个线程。它正在工作

【讨论】:

    猜你喜欢
    • 2020-04-13
    • 1970-01-01
    • 2018-12-16
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    相关资源
    最近更新 更多