-1

I am writing a difficulty script for a simple math game I made. I created an html form where the user can enter the difficulty from 1 to 5 which goes through an if function to assign the difficulty accordingly. This works correctly however on the start of a session the difficulty variable is not set yet, this is no problem for the if function because it ends with an else option that assigns difficult to one, making the default difficulty of the game 1. However I am left with undefined variable errors even though the variable doeskin need to be called yet. Here is what I have written and I can not seem to find the solution to this issue.

   session_start();
 echo ('<h2>Change Difficulty (1 to 5)</h2>' . '<br/>');
 echo "<form method = 'post'><input type='text' id='random' name='random' onChange='preventDefault();'></form>";
                if(isset($_POST['random'])) {
 if ($_POST['random'] == 1 or $_SESSION['diffculty'] == 1) {
    $randvalue = 10;
    echo 'Current Difficulty Is 1';
    $diffculty = 1;
} elseif($_POST['random'] == 2 or $_SESSION['diffculty'] == 2) {
    $randvalue = 100;
    echo 'Current Difficulty Is 2';
    $diffculty = 2;
} elseif($_POST['random'] == 3 or $_SESSION['diffculty'] == 3) {
    $randvalue = 1000;
    echo 'Current Difficulty Is 3';
    $diffculty = 3;
} elseif($_POST['random'] == 4 or $_SESSION['diffculty'] == 4) {
    $randvalue = 10000;
    echo 'Current Difficulty Is 4';
    $diffculty = 4;
} elseif($_POST['random'] == 5 or  $_SESSION['diffculty'] == 5) {
    $randvalue = 100000;
    echo 'Current Difficulty Is 5';
    $diffculty = 5;
} else {
    $randvalue = 10;
    echo 'current Difficulty is 1';
    $diffculty = 1;
}

 $_SESSION['randvalues'] = $randvalue;
 $_SESSION['difficulty'] = $diffculty;
 }

Also even if the user sets the difficulty variable you still get undefined index errors for every line of code that it is called in the if function even though it is assigned.

2 Answers2

0

Apply isset for session and add else case for first if.

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();
echo ('<h2>Change Difficulty (1 to 5)</h2>' . '<br/>');


 echo "
 <form method = 'post'>
 <input type='text' id='random' name='random' onChange='preventDefault();'>
 </form>";

if(isset($_POST['random']) && isset($_SESSION['diffculty'])) {
    if ($_POST['random'] == 1 or $_SESSION['difficulty'] == 1) {
        $randvalue = 10;
        echo 'Current Difficulty Is 1';
        $difficulty = 1;
    } elseif($_POST['random'] == 2 or $_SESSION['difficulty'] == 2) {
        $randvalue = 100;
        echo 'Current Difficulty Is 2';
        $difficulty = 2;
    } elseif($_POST['random'] == 3 or $_SESSION['difficulty'] == 3) {
        $randvalue = 1000;
        echo 'Current Difficulty Is 3';
        $difficulty = 3;
    } elseif($_POST['random'] == 4 or $_SESSION['difficulty'] == 4) {
        $randvalue = 10000;
        echo 'Current Difficulty Is 4';
        $difficulty = 4;
    } elseif($_POST['random'] == 5 or  $_SESSION['difficulty'] == 5) {
        $randvalue = 100000;
        echo 'Current Difficulty Is 5';
        $difficulty = 5;
    } else {
        $randvalue = 10;
        echo 'current Difficulty is 1';
        $difficulty = 1;
    }

    $_SESSION['randvalues'] = $randvalue;
    $_SESSION['difficulty'] = $difficulty;
} else {
    $_SESSION['randvalues'] = 10;
    $_SESSION['difficulty'] = 1;
}
Saravanan Sampathkumar
  • 3,201
  • 1
  • 20
  • 46
Nishant Nair
  • 1,999
  • 1
  • 13
  • 18
0

Define your form action:to the same page using global variable: $_SERVER['PHP_SELF']

echo "<form method = 'post'><input action=".$_SERVER['PHP_SELF']."type='text' id='random' name='random' onChange='preventDefault();'></form>";
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19