-1

I have a form that consists of 4 radio buttons, each radio button represents a different shape either cuboid, cone, cylinder or sphere, I also have a session variable for each of those shapes ( each one is set to 0), when a user selects a radio button and submits the form I want it to add 1 to whatever shape was selected here is my code

HTML

<form action="question2.php" method="post">
    <input type="radio" name="ans" value="cuboid">
    <input type="radio" name="ans" value="cone">
    <input type="radio" name="ans" value="cylinder">
    <input type="radio" name="ans" value="sphere">
    <input type="submit" value="Submit">
</form>

PHP

<?php
    if(isset($_POST['submit'])) {
        if(isset( $_POST['ans'])) {
             $selected_answer = $_POST['ans'];
             if($selected_answer == "cuboid") {
                 $_SESSION["cuboid"] = ((int)Session["cuboid"]) + 1;
             }
        }
    }
?>

However this is not working. $_SESSION["cuboid"] just stays 0. Can anyone tell me where I am going wrong?

EDIT - I am defining the Session variable in a previous page like this

$_SESSION["cuboid"] = 0;

And also I have the following at the top of all my pages

<?php
    session_start();
?>
Dfarrelly
  • 695
  • 2
  • 7
  • 24
  • did you check if all conditions where met? FYI `isset()` can take more than one parameter. – Ivan Aug 16 '16 at 22:27
  • Do you mean have an if statement for each radio button? Im not sure what you mean by it being able to take more than one parameter? – Dfarrelly Aug 16 '16 at 22:28
  • `((int)Session["cuboid"])` wrong syntax. Should be `(int)$_SESSION["cuboid"]` - you also need `session_start();` and to define the variable it when it's never set. – Qirel Aug 16 '16 at 22:29
  • The `$_SESSION` array is called `$_SESSION` and not `Session` which MUST be generating an error of some sort somewhere. Find your PHP ERROR LOG and review it often, or while testing add ` – RiggsFolly Aug 16 '16 at 22:36
  • 2
    Don't expect this `if(isset($_POST['submit'])) {...}` to ever fire. – Funk Forty Niner Aug 16 '16 at 22:38
  • 1
    @Fred-ii- Excellent catch! – Script47 Aug 16 '16 at 22:39
  • 1
    @Script47 It's always the first thing I check, conditionals. – Funk Forty Niner Aug 16 '16 at 22:39
  • actually your if is not being called if(isset($_POST['submit'])) { as submit input dont have name="submit" and it is Submit it should be submit – owais Aug 16 '16 at 22:41

1 Answers1

4

You need to use use session_start, if you haven't done so already.

You also have a syntax error which should be showing if you have a error reporting enabled.

Change,

$_SESSION["cuboid"] = ((int)Session["cuboid"]) + 1;

To,

$_SESSION["cuboid"] = ((int)$_SESSION["cuboid"]) + 1;

Another thing...

Your if (isset($_POST['submit'])) conditional check will never run, as you have not actually set the name property for it.

Error Reporting

To enable error reporting to see any errors that you make,

error_reporting(-1);
ini_set('display_errors', 'On');
Script47
  • 14,230
  • 4
  • 45
  • 66