Re: Using if(isset()) with $_GET and switch and default

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 2010-05-21 at 16:18 -0400, Adam Richardson wrote:

> On Fri, May 21, 2010 at 3:24 PM, MuFei <mufei4u@xxxxxxxxx> wrote:
> 
> > Hi,
> > I'm still new to PHP and everyday I learn something new.
> > Today I was trying to make some script using the switch statement but
> > I have some issue that I don't know how to get around it.
> > It's a simple test script that contain only one file, it has some
> > questions that I want only one question appear per time then go to the
> > next question when clicking on the submit button, I used switch for
> > that, but when I get the first page, I got a notice which says:
> > Notice: Undefined index: question in path\test.php on line 56
> > So I used if(isset($_GET[''])) but then the default value of the
> > switch does not show up in the first page and I get the first page
> > blank.
> >
> > 1- Is there anyway that I can use the default value of the switch
> > without getting the note?
> >    Or
> > 2- Is there anyway that I can use the default value of the switch with
> > the if(isset()) and show the first page?
> >
> > Here is the content of the script that I hope you can help me with:
> > (please note that it's not finished yet and it's so simple)
> > (The if(isset()) is commented)
> >
> > ================================================
> >
> > <?php
> >        // List of test questions and their answers in variables
> >        $question1 = '1- What is PHP?<br />
> >                                                <input type="radio"
> > name="q1a" value="a" /> A- Software <br />
> >                                                <input type="radio"
> > name="q1a" value="b" /> B- Hypertext
> > Preprocessor, Scripting Server side programming language <br />
> >                                                <input type="radio"
> > name="q1a" value="c" /> C- Client side
> > programming language <br /><br />
> >                                                <input type="submit"
> > name="sumbit" value="Question 2" />';
> >        $answer1 = 'b';
> >
> >        $question2 = '2- What is PHP used for?<br />
> >                                                <input type="radio"
> > name="q2a" value="a" /> A- For web <br />
> >                                                <input type="radio"
> > name="q2a" value="b" /> B- For Computers <br />
> >                                                <input type="radio"
> > name="q2a" value="c" /> C- For Designing <br /><br />
> >                                                <input type="submit"
> > name="sumbit" value="Question 3" />';
> >        $answer2 = 'a';
> >
> >        $question3 = '3- What is good about PHP?<br />
> >                                                <input type="checkbox"
> > name="q3a[a]" value="a" /> A- It\'s
> > Designed and created for the web <br />
> >                                                <input type="checkbox"
> > name="q3a[b]" value="b" /> B- It\'s Free <br />
> >                                                <input type="checkbox"
> > name="q3a[c]" value="c" /> C- It supports
> > different Databases <br />
> >                                                <input type="checkbox"
> > name="q3a[d]" value="d" /> D- It\'s not
> > an Oriented Programming language <br /><br />
> >                                                <input type="submit"
> > name="sumbit" value="Question 4" />';
> >        $answer3_1 = 'a';
> >        $answer3_2 = 'b';
> >        $answer3_3 = 'c';
> >
> >        $question4 = '4- What does PHP stands for?<br />
> >                                                <input type="radio"
> > name="q4a" value="a" /> A- Perl Hypertext
> > Programming <br />
> >                                                <input type="radio"
> > name="q4a" value="b" /> B- Personal Home
> > Page <br /><br />
> >                                                <input type="submit"
> > name="sumbit" value="Question 5" />';
> >        $answer4 = 'b';
> >
> >        $question5 = '5- Who created PHP?<br />
> >                                                <input type="radio"
> > name="q5a" value="a" /> A- Andi Gutmans <br />
> >                                                <input type="radio"
> > name="q5a" value="b" /> B- Zeev Suraski <br />
> >                                                <input type="radio"
> > name="q5a" value="c" /> C- Rasmus Lerdorf <br /><br />
> >                                                <input type="submit"
> > name="sumbit" value="Results" />';
> >        $answer5 = 'c';
> > ?>
> >
> > <html>
> >        <head>
> >                <title>
> >                        <?php
> >                                if(isset($_GET['question'])) {
> >                                        echo "Question " .
> > $_GET['question'];
> >                                } else {
> >                                        echo 'Instroctions';
> >                                }
> >                        ?>
> >                </title>
> >        </head>
> >        <body>
> >                <?php
> >                        //if(isset($_GET['question'])) {
> >                                switch($_GET['question']) {
> >                                        case '1':
> >                                                echo '<form
> > action="test.php?question=2" method="post">' .
> > $question1 .'</form>';
> >                                        break;
> >
> >                                        case '2':
> >                                                echo '<form
> > action="test.php?question=3" method="post">' .
> > $question2 .'</form>';
> >                                        break;
> >
> >                                        case '3':
> >                                                echo '<form
> > action="test.php?question=4" method="post">' .
> > $question3 .'</form>';
> >                                        break;
> >
> >                                        case '4':
> >                                                echo '<form
> > action="test.php?question=5" method="post">' .
> > $question4 .'</form>';
> >                                        break;
> >
> >                                        case '5':
> >                                                echo '<form
> > action="test.php?question=Results" method="post">' .
> >  $question5 .'</form>';
> >                                        break;
> >
> >                                        case 'Results':
> >                                                echo 'You results are: <br
> > /><br /><a href="test.php">Go to the
> > home page and do the test again!</a>';
> >                                        break;
> >
> >                                        default:
> >                                                echo 'Welcome to PHP simple
> > test, Please click on Start button
> > to begin you test. <br />
> >                                                        <a
> > href="test.php?question=1">Click Here to Begin!</a>';
> >                                        break;
> >                                }
> >                        //}
> >                ?>
> >        </body>
> > </html>
> >
> > ================================================
> >
> >
> > Thanks for your help guys! ^^
> >
> > MuFei
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> You could do any one of the following:
> 
> // set the GET
> if(!isset($_GET['question'])) { $_GET['question'] = 'default';}
> 
> 
> 
> 
> 
> 
> 
> // OR, take the default out of the switch and put it in an else statement
> if(isset($_GET['question'])) {
>                                switch($_GET['question']) {
>                                        case '1':
>                                                echo '<form
> action="test.php?question=2" method="post">' .
> $question1 .'</form>';
>                                        break;
> 
>                                        case '2':
>                                                echo '<form
> action="test.php?question=3" method="post">' .
> $question2 .'</form>';
>                                        break;
> 
>                                        case '3':
>                                                echo '<form
> action="test.php?question=4" method="post">' .
> $question3 .'</form>';
>                                        break;
> 
>                                        case '4':
>                                                echo '<form
> action="test.php?question=5" method="post">' .
> $question4 .'</form>';
>                                        break;
> 
>                                        case '5':
>                                                echo '<form
> action="test.php?question=Results" method="post">' .
>  $question5 .'</form>';
>                                        break;
> 
>                                        case 'Results':
>                                                echo 'You results are: <br
> /><br /><a href="test.php">Go to the
> home page and do the test again!</a>';
>                                        break;
> 
>                                        default:
>                                                echo 'We received an invalid
> option';
>                                        break;
>                                }
>                        } else {
> default:
>                                                echo 'Welcome to PHP simple
> test, Please click on Start button
> to begin you test. <br />
>                                                        <a
> href="test.php?question=1">Click Here to Begin!</a>';
>                        }
> 
> 
> 
> OR, disable E_NOTICE level warnings (the corresponding array value for an
> undefined index will be null):
> http://php.net/manual/en/errorfunc.configuration.php
> 
> Adam
> 


I tend to do something like this:

$question = (isset($_GET['question']))?$_GET['question']:'some default
value here';

And then use the value of $question in the switch.

On security: this is perfectly safe as long as you don't use the
default: part of the switch statement to output the value of $question
in an unsafe way. If you do need to use the value in some other way in
default: then consider using some method to secure it first. If it's
going into the database, use mysql_real_escape_string() on it. If it's
being displayed, something like htmlspecialchars() should suffice. You
won't need to do that for the pre-defined cases, as they are
pre-defined, so you are already expecting those values.

Thanks,
Ash
http://www.ashleysheridan.co.uk



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux