【问题标题】:Multiple Forms with Different Actions using Submit Buttons使用提交按钮执行不同操作的多个表单
【发布时间】:2012-06-07 17:34:23
【问题描述】:

我的 index.php 中有两个表单,我试图让他们分别提交和发布他们的数据

表格 1

echo '<form action="process.php?type=news" method="post">';
echo '<table cellspacing="0"><tr><th>';
echo 'Add News to News Feed';
echo '</th></tr><tr><td>';
echo 'Title:<br /><input name="newstitle" type="text" id="add" style="height:16px;width:525px;" size="80" maxlength="186" /><br />';
echo 'Main Body Text:<br /><textarea name="newsfeed" id="add" style="width:525px;height:78px;" maxlength="2000" ></textarea>';
echo '<input style="float:right;margin-top:5px;" id="button" type="submit" value="Submit" />';
echo '</td></tr></table></from>';

表格2

echo '<form action="process.php?type=suggest" method="post">';
echo '<table cellspacing="0"><tr><th>';
echo 'Suggest Additions to the Intranet';
echo '</th></tr><tr><td>';
echo '<textarea name="suggest" id="add" style="width:330px;height:60px;" maxlength="800" ></textarea>';
echo '<input style="float:right;margin-top:5px;" id="button" type="submit" value="Submit" />';
echo '</td></tr></table></from>';

我希望这些都在按下提交按钮后发布并执行操作,但目前第二个表单提交到第一个表单操作

我该如何解决这个问题???

编辑:我也将 .PHP 用于索引和进程页面,然后使用它将表单回显到页面上

这里还有process.php数据

$type=$_REQUEST['type'];
$suggest=$_POST['suggest'];
$newstitle=$_POST['newstitle'];
$news=mysql_real_escape_string($_POST['newsfeed']);

if ($type == "news")
{
  $sql=mysql_query("SELECT * FROM newsfeed WHERE title = ('$newstitle')");
  $number_of_rows = mysql_num_rows($sql);
  if ($number_of_rows > 0)
  {
    echo 'This Title is Already Taken';
  }
  else
  {
    $sql="INSERT INTO newsfeed (title, news) VALUES ('$newstitle','$news')";    
    if (!mysql_query($sql,$con))
    {
      die('Error: ' . mysql_error());
    }
    header('Location: index.php');
    exit;
  }
}
elseif ($type == "suggest")
{
  $sql="INSERT INTO suggestions VALUES ('$suggest')";
  if (!mysql_query($sql,$con))
  {
    die('Error: ' . mysql_error());
  }
  header('Location: index.php');
  exit;
}

【问题讨论】:

  • 你为什么要把它分成这么多的回声?
  • @Jared 它让我更容易看到表格的哪些位需要编辑
  • 你仍然可以在字符串中间换行而不添加新的回显。
  • 在 process.php 文件中你在哪里设置$type,你也应该转到准备好的查询。 您的表单也以 &lt;/from&gt; 结尾
  • @LawrenceCherone Lol 我没有看到拼写错误,谢谢,已修复

标签: php forms button submit


【解决方案1】:

可能你不是用&lt;/form&gt;关闭表单,而是用&lt;/from&gt;,所以第二个表单的代码实际上仍然是inside第一个表单,因为标签是从未关闭?

【讨论】:

  • 是的,以 而不是 结尾
【解决方案2】:

尝试为每个表单点击提交,以便在其提交按钮 html 中输入:

onclick="document.forms["myform1"].submit();" 对于表格 2 放 onclick="document.forms["myform2"].submit();"

【讨论】:

    【解决方案3】:

    我不确定你想要达到什么目标,所以这是我的假设, 1.您的表单提交错误,例如第二个表单提交了第一个表单的数据 要么 2. 你想一个接一个地同时提交。

    对于 1:

    这样做。

    <form id="form1" action="process.php" method="POST">
    <input type="hidden" name="type" value="news">
    ....
    </form>
    

    注意拼写 /form 不是 /from

    <form id="form2" action="process.php" method="POST">
    <input type="hidden" name="type" value="suggest">
    ....
    </form>
    

    注意拼写 /form 不是 /from

    【讨论】:

    • 我已经修复了它,拼写为 而不是 这样做也意味着我必须更改在 process.php 中获取 $type 的方式,但因为我也使用链接去到 process.php &lt;a href="process.php?type=something"&gt;Something&lt;/a&gt; 这会破坏目的
    【解决方案4】:

    一些提示:

    与其重复多次,一次就足够了!记住回声会减慢你的脚本。另外我不会将类型作为 $_GET 传递,对于表单来说,post 总是更安全,你可以使用隐藏字段来添加类型:

    <?php 
    echo '
    <form action="process.php" method="post">
    <input type="hidden" name="type" value="news">
    ...
    ...
    </form>';//Not </from>
    ?>
    

    然后使用 PHP 部分使用 switch case 而不是 if elses,添加功能然后与另一个 ifelse 拆分会更容易,而且默认块可用于任何不匹配的内容,如 if else 中的最后一个 else声明。

    <?php 
    $type = $_POST['type'];
    
    switch($type){
        case "news":
            ...
            break;
        case "suggest":
            ...
            break;
        default:
            //default if different type or no type is set
            break;
    }
    ?>
    

    也转移到 PDO 或为您的 mySQL 准备的 mysqli_ 函数更安全。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 2019-05-13
      相关资源
      最近更新 更多