如果为多个提交按钮设置了

时间:2012-06-13 17:23:32

标签: php form-submit

我想知道是否有人可以帮助我。

我正在尝试运行下面的代码,我正在使用带有多个submit buttons的表单。

<?php
if (isset($_POST['type']){
    if ($_POST['type'] == 'view'){
     $url = 'updatelocation.php';

    }   elseif ($_POST['type'] == 'finds'){
        $url = 'addfinds.php';

    }   elseif ($_POST['type'] == 'image'){

    header("Location: " . $url);
}
?>

我遇到的问题是,当我运行此操作时,收到以下错误:

Parse error: syntax error, unexpected '{' in /homepages/2/d333603417/htdocs/locationsaction.php on line 2

我一直在阅读一些教程,例如this,我的代码似乎与示例匹配,所以我不确定错误在哪里。

有关其他信息,我用于触发php脚本的按钮和表单如下所示:

<form name="locations" id="locations" method="post" action="locationsaction.php">   

<td><div align="center"><input name="viewdetails" type="submit" value="view"/></div>/td>
<td><div align="center"><input name="addfinds" type="submit" value="finds"/></div></td>
<td><div align="center"><input name="addimages" type="submit" value="images"/></div></td>

我只是想知道是否有人可以看到这个,让我知道我哪里出错了?

3 个答案:

答案 0 :(得分:3)

)之后你遗漏了isset($_POST['type']) - 你还没有关闭if声明。

答案 1 :(得分:2)

您缺少右括号:

if (isset($_POST['type']) {

应该是:

if (isset($_POST['type'])) {

你也错过了最后一行的结束括号。您应该尝试正确地格式化和缩进代码。这样可以更容易地发现这样的错误。考虑这个例子:

<?php
if (isset($_POST['type'])) {
    if ($_POST['type'] == 'view') {
        $url = 'updatelocation.php';
    } elseif ($_POST['type'] == 'finds') {
        $url = 'addfinds.php';
    } elseif ($_POST['type'] == 'image'){
        $url = 'image.php';
    }

    header("Location: " . $url);
}

另一种查找方法是使用地图:

<?php
if (isset($_POST['type'])) {
    $urls = array(
        'view' => 'updatelocation.php',
        'finds' => 'addfinds.php',
        'image' => 'image.php'
    );
    $url = $urls[$_POST['type']];
    header("Location: " . $url);
}

那很干净 - 对吗?添加一个新案例就是将其添加到数组中。

答案 2 :(得分:2)

你也缺少一个结束括号:

 if (isset($_POST['type'])){ 
   if ($_POST['type'] == 'view'){ 
     $url = 'updatelocation.php'; 
   }elseif ($_POST['type'] == 'finds'){ 
     $url = 'addfinds.php'; 
   }elseif ($_POST['type'] == 'image'){ 
     $url='image.php';
   }
   header("Location: " . $url); 
 }