0

I need to be able to add to my $questions array, but there is no dynamics in the code, so when I add something I have to create a if sentence and a new input field for each question to store the data (the teacher demands storing in hidden type). Does anyone know a good tip of how I can get it more efficient?

<?php 




    date_default_timezone_set('Europe/Oslo');

    $questions = array(
        array(
            'text' => 'Whats your name?',
            'type' => 'text',
            'name' => 'svar1'
        ),
        array(
            'text' => 'Do you live in Oslo?',
            'type' => 'checkbox',
            'name' => 'checkbox'
        ),
        array(
            'text' => 'How old are you?',
            'type' => 'text',
            'name' => 'svar3'
        ),
        array(
            'text' => 'Do you have friends?',   
            'type' => 'text',
            'name' => 'svar4'
        ),
        array(
            'text' => 'Do you like going to the movies?',   
            'type' => 'text',
            'name' => 'svar5'
            )
    );




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

    if (isset($_POST['prev'])) {
        $step --;
    }


    if (isset($_POST['svar1'])) {
        $step++;
    }



    echo "<p><a href='/webutvikling/oblig2.php?reset=true'>Start på nytt</a></p>\n";
?>




<!DOCTYPE html>
<html>
    <head>
        <title>Spørreundersøkelse</title>
        <meta charset="utf-8">
    </head>
    <body>

        <style type="text/css">

        body {

            font-family: Helvetica Neue, Arial;
            text-rendering: optimizeLegibility;
            -webkit-font-smoothing: antialiased;
            background-color: #E0E0EB;
        }

        .wrapper {

            border: 1px solid black;
            text-indent: 10px;
            font-size: 1em;
            background: RGBA(255, 255, 255, 0.8);
        }
        </style>

        <h1>Spørreundersøkelse</h1>

        <form target="oblig2.php" method="post" >

            <input type="hidden" name="step" value="<?php echo $step ?>">

            <?php if (isset($questions[$step])) : ?>

                <p>Gjeldende steg: <?php echo $step +1 ?></p>

                <h3><?php echo $questions[$step]['text'] ?></h3>


                    <input type="<?=$questions[$step]['type']?>" autofocus name="<?= $questions[$step]['name']?>">


                <input type="submit" name="next" value="Send inn">

            <?php endif; ?>

            <br><br>

            <?php if ($step > 0) : ?>

            <input type="submit" name="prev" value="Forrige spørsmål">

            <?php endif; ?>


            <?php if($step > 0):?>
                <input type="hidden" name="svar1" value="<?= isset($_POST['svar1']) ? $_POST['svar1'] : false   ?>" >
            <?php endif;?>

            <?php if($step > 1):?>
                <input type="hidden" name="checkbox" value="<?= isset($_POST['checkbox']) ? $_POST['checkbox'] : false  ?>" >
            <?php endif;?>

            <?php if($step > 2):?>
                <input type="hidden" name="svar3" value="<?= isset($_POST['svar3']) ? $_POST['svar3'] : false   ?>" >
            <?php endif;?>

            <?php if($step > 3):?>
                <input type="hidden" name="svar4" value="<?= isset($_POST['svar4']) ? $_POST['svar4'] : false   ?>" >
            <?php endif;?>

            <?php if($step > 4):?>
                <input type="hidden" name="svar5" value="<?= isset($_POST['svar5']) ? $_POST['svar5'] : false   ?>" >
            <?php endif;?>




        </form>




        <?= isset($_POST['svar1']) ? $_POST['svar1'] : false    ?> <br /> <br />
        <?= (isset($_POST['checkbox']) && $_POST['checkbox'] == 'on') ? 'Ja' : 'Nei'?> <br /> <br />
        <?= isset($_POST['svar3']) ? $_POST['svar3'] : false    ?> <br /> <br />
        <?= isset($_POST['svar4']) ? $_POST['svar4'] : false    ?> <br /> <br />
        <?= isset($_POST['svar5']) ? $_POST['svar5'] : false    ?> <br /> <br />


    </body>
</html>
4
  • 1
    $_SESSION <- The way to go Commented Mar 15, 2015 at 21:48
  • Yeah.. I agree. I was thinking maybe questions array gets stored in $_SESSION and then the form can be generated with for loop and some heredoc syntax for longer html. PErhaps this way stackoverflow.com/questions/4301203/php-session-array and php.net/manual/en/… Commented Mar 16, 2015 at 4:16
  • this code is easily hackable. but i guess you are just new to programming and security is a later issue. Commented Mar 16, 2015 at 10:01
  • Dont hack my code, help me instead ;) Commented Mar 16, 2015 at 10:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.