3

I am a noob to drupal and cannot figure out why my form isn't working correctly. The form itself loads fine and if I go to the redirect page manually it works, so I figure its a problem with my submit or redirect but I cannot figure out what is going wrong.

<?php

function queen_attack_menu(){
    $items['queen_attack']=array(
    'title' => "Queen Attack!",
    'page callback' => 'drupal_get_form',
    'page arguments' => array('queen_attack_form'),
    'access callback' => TRUE,
    'type' => MENU_NORMAL_ITEM,
);
$items['attack_calculated']=array(
    'title' => "Attack Results!",
    'page callback' => 'attack_results',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
);
return $items;
}
//chess board 8x8 so coordinates 1-8 for each side
function queen_attack_form(){
$form["queen_coordinates_x"] = array(
    '#title' => 'Queen Horizontal Coordinate',
    '#type' => 'select',
    "#options" => array(
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8
    ),
    '#description' => t('Enter your starting horizontal coordinate for your Queen'),
);
$form['queen_coordinates_y'] = array(
    '#title' => 'Queen Vertical Coordinate',
    '#type' => 'select',
    '#options' => array(
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8
    ),
    '#description' => t('Enter your starting vertical coordinate for your Queen'),
);
$form['target__coordinates_x'] = array(
    '#title' => 'Target Horizontal Coordinate',
    '#type' => "select",
    '#options' => array(
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8
    ),
    '#description' => t('Enter the horizontal coordinate for the target piece'),
);
$form['target_coordinates_y']= array(
    '#title' => 'Target Vertical Coordinate',
    '#type' => "select",
    '#options' => array(
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8
    ),
    '#description' => t('Enter the vertical coordinate for the target piece'),
);

$form['submit'] = array(
    '#type' => 'submit',
    '#value' => "Queen, Attack!",
);

return $form;
}


function queen_attack_form_submit($form, $form_state){
//$queen_x;
//$queen_y;
//$target_x;
//$target_y;
$queen_coords = $form_state['values']   ['queen_coordinates_x'];//['queen_coordinates_y'];
$target_coords = $form_state['values']   ['target__coordinates_x'];//['target_coordinates_y'];

//session hit or miss

$_SESSION['queen_coords'] = $queen_coords;
$_SESSION['target_coords'] = $target_coords;
$form_state['redirect'] = 'attack_calculated';
}


function attack_results(){
 $result = 'Hit or Missed';
    return '<p> Queen Coords: ' . $_SESSION['queen_coords'] . '</p> <p>     Target Coords: ' . $_SESSION['target_coords'] . '</p>';

//      return '<h3> Your Queen' . $result . 'the target! </h3>';
}
2
  • 3
    Welcome to Drupal Answers :) What you're describing is a common error for people new to Drupal/PHP...you've simply forgotten to receive $form_state by reference in the submit function definition, so the redirect item you're adding to it doesn't persist when that function completes. To fix, accept the parameter by reference: function queen_attack_form_submit($form, &$form_state){
    – Clive
    Commented Dec 8, 2016 at 18:18
  • from user @vijay that don't have access to comments: try with $form['submit'] = array( '#type' => 'submit', '#value' => 'Queen, Attack!', '#submit' => array(queen_attack_form_submit) ); return $form; Commented Dec 9, 2016 at 18:26

0

Browse other questions tagged or ask your own question.