Solved

javascript show hide in php file

Posted on 2010-09-20
3
267 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
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…

789 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