使用提交按钮的多个表单具有不同的操作

时间:2012-06-03 04:18:14

标签: php forms button submit

我的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;
}

4 个答案:

答案 0 :(得分:2)

可能是因为您没有使用</form>关闭表单,而是关注</from>,所以第二种形式的代码实际上仍然是内部第一种形式因为标签永远不会关闭?

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

FOR 1:

这样做。

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

注意拼写     /表格不是/来自

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

注意拼写     /表格不是/来自

答案 3 :(得分:0)

一些指示:

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

<?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中的最后一个言。

<?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_函数更安全。