If isset For Multiple Submission Buttons

旧巷老猫 提交于 2019-12-23 04:38:05

问题


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

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.

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?


回答1:


You are missing a closing parenthesis:

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

Should be:

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.




回答2:


You are missing a ) after isset($_POST['type']) - you aren't closing the if statement.




回答3:


You are also missing a closing brace:

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


来源:https://stackoverflow.com/questions/11020130/if-isset-for-multiple-submission-buttons

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!