I am writing a module to calculate the factorial of a number. As of right now when I enter any number but 2 I get a blank page, and when I enter 2 I get a response message "The requested page '/factorial_success_page" could not be found."

Here is all the code in my factorial.module file:

<?php
function factorial_menu(){
    $items['factorial'] = array(
        'title' => 'Factorial Calculator',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('factorial_form'),
        'access callback' => TRUE,
        'type' => MENU_NORMAL_ITEM,
    );
    $items['factorial_success_page'] = array(
        'title' => 'Factorial Results',
        'page callback' => 'factorial_success',
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
    );
    return $items;
}

function factorial_form(){
    $form['factorial_number'] = array(
        '#title' => 'Number',
        '#type' => 'textfield',
        '#description' => 'Enter a number to calculate its factorial',
        '#element_validate' => array('element_validate_integer_positive'),
        '#required' => TRUE,
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Calculate!',
    );
    return $form;
}

function factorial_form_submit($form, &$form_state){
    $number = $form_state['values']['factorial_number'];
    $i = ($number - 1); // sets the first number to multiply by i.e.if $number = 8 then $i = 7;
    $output = 0;
    do{
        $number = ($number * ($i));
        $i--;
    } while($i > 0);

    $_SESSION['result'] = $number;
    $form_state['redirect'] = 'factorial_success_page';
}

function factorial_success(){
    $result = $_SESSION['result'];
    return $result;
}

Comments

jaypan’s picture

Change this:

    return $result;

To this:

    return '<p>' . $result . '</p>';

Returning integer values from a page callback has a different meaning in Drupal, which is where your problem is coming from. Wrapping the integer in HTML will display the HTML.

Contact me to contract me for D7 -> D10/11 migrations.

bronda’s picture

Ahh that would do it! I haven't returned an integer before but I have done it with a string, I just thought it was the same principle.

Thank you!