If statement to display different sections of form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dangaskin
Forum Newbie
Posts: 6
Joined: Thu Sep 16, 2010 6:26 pm

If statement to display different sections of form

Post 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
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: If statement to display different sections of form

Post 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
Post Reply