Solved

javascript show hide in php file

Posted on 2010-09-20
3
262 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now