如果为多个提交按钮设置了is [英] If isset For Multiple Submission Buttons

查看:83
本文介绍了如果为多个提交按钮设置了is的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wonder whether someone may be able to help me please.

我正在尝试运行下面的代码,该代码与具有多个submit buttons的表单一起使用.

I'm trying to run the code below, which I'm using with a form with multiple 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);
}
?>

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

The problem I'm having is that when I run this, I receive the following error:

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

我一直在阅读一些教程,例如 ,并且我的代码似乎与示例匹配,因此我不确定错误在哪里.

I've been reading some tutorials, for example this, and my code seems to match the example so I'm not sure where the error is.

有关其他信息,下面显示了用于触发php脚本的按钮和表单:

For additional information, the buttons and form which I use to trigger the php script are shown below:

<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>

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

I just wondered whether someone could look at this please and let me know where I'm going wrong?

推荐答案

您缺少右括号:

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

应该是:

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

您还缺少最后一行的右括号.您应该真正尝试正确格式化和缩进代码.这将使发现这样的错误变得更加容易.考虑以下示例:

You are also missing a closing bracket on the last line. You should really try to format and indent your code properly. That will make it so much easier to spot errors like this. Consider this example:

<?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);
}

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

Another way to do the lookup is to use a map:

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

那很干净-对吗?向其中添加新案例只不过是将其添加到数组中而已.

That's pretty clean - right? Adding a new case to this is simply a matter of adding it to the array.

这篇关于如果为多个提交按钮设置了is的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆