Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Drupal Basics Writing Modules Process Forms in a Module

Access Denied for Sum Answer

Hello!

As I wrote my code for the sum.module file, I kept making sure that my Cameron's Coffee website was still displaying. All of the code seems to be in good working order - but when I submit my form by clicking 'Calculate Sum', the /success URL returns a page with the title 'Access Denied', and the message 'You are not authorized to access this page.' Before I / we added the $sum and $_SESSION key/values, submitting the form took me to the /success URL with the message 'You calculated a sum!'. So it must be something in the last few lines of code from the video, but I must be overlooking something when I compare my code.

Here is my sum.module code:

<?php
function sum_menu() {
    $items = [];
    $items['sum'] = [
        'title' => 'Calculate Sum',
        'page callback' => 'drupal_get_form',
        'page arguments' => ['sum_form'],
        'access callback' => TRUE,
        'type' => MENU_NORMAL_ITEM,
    ];
    $items['success'] = 
    [
        'title' => 'Success',
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
    ];

    return $items;
};
function sum_form() {
    $form = [];
    $form['left_number'] = 
    [
        '#title' => 'Left Number',
        '#type' => 'textfield',
        '#description' => t('Please enter your starting number.'),
    ];

    $form['right_number'] = 
    [
        '#title' => 'Right Number',
        '#type' => 'textfield',
        '#description' => t('Please enter a number to add your starting number.'),
    ];

    $form['submit'] = 
    [
        '#type' => 'submit',
        '#value' => 'Calculate Sum',
    ];

    return $form;
};
    $first_number = $form_state['values']['left_number'];
    $second_number = $form_state['values']['right_number'];
    $sum = $first_number + $second_number;
    $_SESSION['sum_output'] = $sum;
    $form_state['redirect'] = 'success';
};
function sum_success() {
    $answer = $_SESSION['sum_output'];
    return "You calculated a sum! The answer is $answer";
};

Thanks in advance! :)

Andrew Rady
Andrew Rady
20,880 Points

Hey Angela,

You have to go back in the array for success and add the page callback. In the video she left this out and then came back to it near the end. the $items array for success should look like this

$items['success'] = array(
                        'title' => 'success',
            'type' => MENU_CALLBACK,
            'access callback' => TRUE,
            'page callback' => 'sum_success',

1 Answer