views:

61

answers:

2

Hey guys!

I'm looking to make a page wherein a user will make a selection for eg: "How old is your computer?" - "One year", "Two years" etc etc and the page will remove and add 'options' (which at the moment only need to be informative sections of text)

Is there any way to do something like that?

The technologies I'm using are PHP and of course, HTML and CSS to style the pages.

Thanks in advance!

+1  A: 

I think you're mixing some stuff together. From what I understand is you want to show or hide portions of the page depending on what the user selected in a dropdown.

PHP makes "dynamic webpages" that's true. But what's ment by this is that the page can serve different content on each request. But once the content is served it's rendered on the client side and not in PHP hands anymore.

If you want to change the content without reloading the whole page you should use javascript.
You could also server new content using ajax, but I think you just want to put some div's display propertie to none or something like that.

Nicky De Maeyer
Haha, sorry guys, i forgot that i wrote PHP in the title. What i really meant is that i'm using PHP primarily for the website as my main technology, i know PHP is more used as a way to grab various sections then display them as a static page.
SevenT2
+1  A: 

PHP cannot detect if changes has been made to the page, you will have to use Javascript for that.

If you are using a HTML select element and you want to create the content dynamically it could look something like this:
HTML

<select id="computerAge">
    <option>2 years</option>
    <option>3 years</option>
</select>

<div id="dynamicContent">
</div>

Javascript

var computerAge = document.getElementById('computerAge');
var dynamicContent = document.getElementById('dynamicContent');

computerAge.onchange = function()
{
    // Get a list of the current dynamic content
    var contents = document.getElementById('content').document.getElementsByTagname('p');

    // Remove current dynamic content
    for(i = 0; i < contents.length; i++)
    {
        dynamicContent.removeChild(contents[i]);
    }

    // Create new dynamic content
    var dynamicP = document.createElement('p');

    if(computerAge.value == '2 years')
    {
        dynamicP.innerHTML = 'Content for 2 year old computer';
        dynamicContent.appendChild(dynamicP);
    }
    elseif(computerAge.value == '3 years')
    {
        dynamicP.innerHTML = 'Content for 3 year old computer';
        dyanmicContent.appendChild(dynamicP);
    }
}

Or if you're not creating elements dynamically:

HTML

<select id="computerAge">
    <option>2_years</option>
    <option>3_years</option>
</select>

<div id="content">
    <div id="2_years" style="display: none">
        <p>Content for 2 year old computer</p>
    </div>  

    <div id="3_years" style="display: none">
        <p>Content for 3 year old computer</p>
    </div>
</div>

Javascript

function init()
{
    // Get the select element
    var select = document.getElementById('computerAge');

    select.onchange = function()
    {
        // Get the elements which could be the dynamic content
        var content = document.getElementById('content')
     var contents = content.getElementsByTagName('div');

        // Iterate over contents; make selected visible and hide others
        for(i = 0; i < contents.length; i++)
        {
            if(contents[i].id == select.value)
            {
                contents[i].style.display = 'block';
            }
            else
            {
                contents[i].style.display = 'none';
            }
        }
    }
}

window.onload = init;
richard
I was attempting to avoid java-script if possible, its not going to happen though is it? HahaThanks for the amazingly quick response and code!
SevenT2
With the non dynamic option you have put there, is it possible to double up on the usage, and have two or more selectors removing/showing different things?
SevenT2
Sure thing. You could copy the code and just change the ID of the select element (which is the same as the first Javascript variable. And the contents var as well.
richard
Awesome, thanks so much for your help, its been invaluable!
SevenT2
I tried the non-dynamic code and its not doing anything..? I just placed them in the same file (obviously putting the script first and in the appropriate tags) Any suggestions?
SevenT2
I'm sorry, there was an error in there. I also wrapped the code inside an init function that will be fired on page load. The edited code is above, should work now!
richard
thanks for all your help! :D
SevenT2