Solved

javascript show hide in php file

Posted on 2010-09-20
3
266 Views
Last Modified: 2012-05-10
Hi,

I am novice at both JavaScript and PHP so any help would be greatly appreciated. I am building a job posting website using Drupal. For my user application form I would like to show and hide sections of the application form.

What I require is, when a user clicks New Zealand as their country I would like to display a section with the field 'city'. When a user selects any country that is not New Zealand I would like a section to display asking the users about their eligibility to work in New Zealand.

This is the form code I have (Just no idea how to use JavaScript with it). I won't list all the countries or regions as that would be a lot of code.

Thank you very much for any help,
Dan

<?php
  $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' => t('New Zealand'),
        'Australia' => t('Australia'),       
        'Afghanistan' => t('Afghanistan'),
        'Albania' => t('Albania'),
        'Algeria' => t('Algeria'),
         ),
        '#description' => t('Select the country you currently live in'),
);

$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'),
        ), 
        '#description' => t('Select the region you currently live in'),
    );

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

Open in new window

0
Comment
Question by:dangaskin
3 Comments
 
LVL 14

Expert Comment

by:sam2912
ID: 33722801
So where's the HTML code? JavaScript does not work with PHP directly. PHP needs to print out the above as HTML code, and JavaScript can modify the HTML elements.
0
 

Author Comment

by:dangaskin
ID: 33723099
Everything is PHP in Drupal. The entire module that this came from is PHP. To get around this I was going to use print(). And print the JavaScript/HTML that is required. I however, have no understanding of what to 'print'.

Thanks,
Dan
0
 
LVL 4

Accepted Solution

by:
heppa earned 500 total points
ID: 33724404
Hi!

if you are using drupal, you might want to know, that things get a bit more complicated since you would not want to "hardcode" something, when using a CMS.

Have a look at this:
Adding Javascript to Drupal Modules

It means that you would add javascript that will then be added in the html output.

2 Hints:
first: make yourself familiar with simple HTML, then with simple HTML having JavaScript, then with simple PHP creating HTML, and finally with simple PHP creating simple HTML with simple Javascript! ;)

If you know all about that, you might recognize that CMS (like Drupal) do nothing else then creating HTML output. JavaScript can work in that HTML (eg. in the DOM). And if you know all that you might find the link I gave you useful, since you would have to rewrite the module you are using.

Best of luck!
Alex
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question