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!

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

isset Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

change your code to this

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

isset will return the boolean value. So here it makes the issue.

You are not closed ur close bracis for switch also, isset is checked but value is not assigned for variable $pageid

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

switch ($pageid) {

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

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

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

 default: 
 echo 'DEFAULT';
 break;
}