Hi,

I am working with the 'Jobs Posting Module'. I am relatively new to Drupal and php.

I have customized the application form. What I want to now do is: 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 country outside of New Zealand I would like to display a new field called 'Eligibility to work in New Zealand'.

This is the code I have at the moment. The problem is neither the 'city' or 'Entitled to work in NZ' fields display. So I am guessing my if statement is wrong. (Note, I won't list all countries as it will be a very long piece of code.)

$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'),
);
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 { 
$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')),
  );
}

Thank you for your help,
Dan

Comments

mjlF95’s picture

I'll try to help out here.

I'm not 100% sure why neither of these are showing up for you. If the first statement is not true the second one should be processed. Try taking both of these elements out of the if and see if they appear normally.

As for the conditional, I think the trouble is that you are saying "if that form element == New Zealand" which it doesn't (and won't) as it is equal to an array which you have declared just before the conditional. What you want is more like if $form_values['job_posting_country'] (which is where the values you've submitted the form with go in D5, in 6 it's something similar I just forget what it's called offhand) equals New Zealand. However, you will not get that until you submit the form. You want something that happens on this page, without a page load, at the client level, so you will need to use JavaScript/jQuery to handle that behavior. Handling it on the server side with PHP will only work if the server is given a chance to rebuild the page on a submit. I hope that helps a little.

dgaskin’s picture

Thanks for your reply. I suspected that might be the case but was seeing if it was possible with php.

Thanks,
Dan

jaypan’s picture

javascript is run on the browser, and can change stuff realtime. PHP is run on the server, and can only change stuff during page loads.

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

mjlF95’s picture

My pleasure. jQuery has some very simple and effective selectors and methods for showing/hiding form elements. If you're familiar with CSS you'll find the selectors very intuitive (id, class, etc.). If you bind a function to the selection of the value "New Zealand" from that select element that shows the other element you should be all set. If that sounds complicated don't worry, just do a little reading on jQuery. It's pretty easy.