dongyuan3094 2015-07-03 12:23
浏览 19
已采纳

PHP开关不用整数切换[关闭]

I have an HTML form and some PHP, but it always echo's "100 TEST" regardless of the case submitted. The default case works fine.

HTML:

<form class="page-search" action="page.php" method="post">
     <input type="text" name="page" autofocus maxlength="3" placeholder="100" style="width: 50px;">
     <input type="submit" style="visibility: hidden;">
</form>

PHP:

<?php 
$pageid = isset($_POST['page']);

switch ($pageid) {

case '100':
echo '100 TEST';
break;

case '200':
echo '200 TEST';
break;

case '300':
echo '300 TEST';
break;

default: 
echo 'DEFAULT';
break;

}
?>

Have I missed something really obvious? I'm kicking myself for needing to ask the question but can't seem to figure this one out!

  • 写回答

3条回答 默认 最新

  • dongwuxie5112 2015-07-03 12:29
    关注

    Change

    $pageid = isset($_POST['page']);
    

    To

    $pageid = isset($_POST['page']) ? $_POST['page'] : 'default value if you need one';
    

    Since the code is checking the result of the isset() method which will be a boolean, but the switch is interested in the $_POST['page'] value itself.


    As mentioned in my comment here is an alternative for the example code:

    echo isset($_POST['page']) ? $_POST['page'] . ' TEST' : 'DEFAULT';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)