Solved

javascript show hide in php file

Posted on 2010-09-20
3
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

695 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