Page 1 of 1

If statement to display different sections of form

Posted: Fri Sep 17, 2010 9:11 pm
by dangaskin
Hi,

I have created a form. When the user selects New Zealand from the country menu I would like the form to display a new field called 'city' and when a user selects a field outside of New Zealand I would like to display a new field called eligibility to work in New Zealand.

The code for the country section of the form (Note, I won't list all countries as it will be a very long piece of code.

Code: Select all

  $form['job_posting_country'] = array(
    '#type' => 'select',
    '#title' => t('Country'),
    '#default_value' => variable_get('feed_item_length','New Zealand'), 
    '#required' => True,
    '#options' => array(
        'New Zealand' => ('New Zealand'),
        'Australia' => ('Australia'),        
        'Afghanistan' => ('Afghanistan'),
        'Albania' => ('Albania'),
        'Algeria' => ('Algeria'),
        'American Samoa' => ('American Samoa'),
        'Andorra' => ('Andorra'),
        ),
        '#description' => t('Select the country you currently live in'),
);
The if statement and 'city' code

Code: Select all

if ($form['job_posting_country'] == 'New Zealand'){
$form['job_posting_region'] = array(
    '#type' => 'select',
    '#title' => t('Region'),
    '#default_value' => variable_get('feed_item_length','Northland'), 
    '#required' => TRUE,
    '#options' => array (
        'Northland' => t('Northland'),
        'Auckland' => t('Auckland'),
        'Coromandel' => t('Coromandel'),
        'Waikato' => t('Waikato'),
        'Bay of Plenty' => t('Bay of Plenty'),
        'East Coast' => t('East Coast'),
        'Central Plateau' => t('Central Plateau'),
        'Hawkes Bay' => t('Hawkes Bay'),
        'Taranaki' => t('Taranaki'),
        'Manawatu-Wanganui'=> t('Manawatu-Wanganui'),
        'Wairarpa' => t('Wairarpa'),
        'Wellington' => t('Wellington'),
        'Nelson' => t('Nelson'),
        'Marlborough' => t('Marlborough'),
        'West Coast' => t('West Coast'),
        'Canterbury' => t('Canterbury'),
        'Otago' => t('Otago'),
        'Southland' => t('Southland'),
        ), 
        '#description' => t('Select the region you currently live in'),
    ); 
}
else statement and eligibility to work in NZ code

Code: Select all

else { 
$form['job_posting_workinnz'] = array(
    '#type' => 'radios',
    '#title' => t('Entitled to work in New Zealand'),
    '#requited' => TRUE,
    '#default_value' => variable_get('comment_preview', 1),
    '#options' => array(t('I have a work permit'), t('I require a work permit')),
  );
}
Thanks,
Dan

Re: If statement to display different sections of form

Posted: Sat Sep 18, 2010 11:00 am
by shawngoldw
You need to use javascript rather than php. When the page is requested the php runs and returns html to the user, at this point there is not way for you to know which field they are going to select. Javascript will run with the user's browser and you can change the form as the user interacts with it.

You need to
a) display a default form, probably with the city using php
then
b) using javascript change the field to the eligibility one when appropriate


Shawn