Jump to content

Archived

This topic is now archived and is closed to further replies.

adrianqx

array redirect

Recommended Posts

Hi need some help here i made a drop downform ,and put some arrays to represent each of the dropdown values, what i need it to do is when one selects a value it redirects the user to a specified page on the site this is what i have so far but the redirection has me stamped help me  ??? ??? ???please


function ff_ktown_action(element, action)
{
    area = ff_getElementByName('karea');
    lab  = ff_getElementByName('klab');

    var nairobiArray = new Array(
        'langata', 'weslands', 'umoja', 'kasarani'
    );

    var kisumuArray = new Array(
        'city', 'kodiaga', 'rachwonyo', 'buda'
    );

    var mombasaArray = new Array(
        'chuda', 'nyali', 'malindi', 'kwale'
    );

 
    if (element.value == 'none') {
        // drop countries
        while (area.options.length > 0)
            area.options[area.options.length-1] = null;
    } else {
        // get the matching array
        var selectedArray = eval(element.value+'Array');

        // drop supernumerous option
        while (selectedArray.length < area.options.length-1)
            area.options[area.options.length-1] = null;

        // add the new options
        area.options[0] = new Option('Select area', 'none', true, true);
        for (var i=0; i < selectedArray.length; i++)
            area.options[i+1] =
                new Option(selectedArray[i], selectedArray[i]);
    } // if

    // drop states
    while (lab.options.length > 0)
        lab.options[lab.options.length-1] = null;
} // ff_ktown_action

Share this post


Link to post
Share on other sites
Hi

I'm struggling to see where you are trying to do the redirection here? And where too?

Is it a case of "langata" should be going to "langata.php"?

The redirect code (if thats what you are asking) is just
    location.href = "langata.php"      (or parent.location etc.)

I'm sure this is completely useless to you... :(

Share this post


Link to post
Share on other sites
Hi

I think Uve got the idea,
basically what it is ,there 2 drop down areas, the fisrt one shows towns eg, nairobi ,Mombasa,kampala,. When a user selects a town eg Nairobi ,it should list streets in the next drop down ,and in this drop down when a user selects a street it should take him to a particular page in the site.
So far i have been able to make the town drop down display the streets using the following arrays ,my problem is making the street values eg "langata" take the user to a page when selected


function ff_ktown_action(element, action)
{
    area = ff_getElementByName('karea');
    lab  = ff_getElementByName('klab');

    var nairobiArray = new Array(      // displays "nairobi" in the first drop down& when selected shows the values in the next drop down
        'langata', 'weslands', 'umoja', 'kasarani'
    );

    var kisumuArray = new Array(
        'city', 'kodiaga', 'rachwonyo', 'buda'
    );

    var mombasaArray = new Array(
        'chuda', 'nyali', 'malindi', 'kwale'
    );


i hope i have made it as clear as possible ,please help urgent ??? ??? ???

Share this post


Link to post
Share on other sites
Hi

Okay - im going to try explain what i'm thinking here.

I've had missions with js like this before (although i tend to avoid it like the plague!)

So basically you have to drop downs so something like

[code]<select name='karea' onchange='JavaScript: changearea();'></select>
<select name='klab' onchange='JavaScript: labredirect();'></select>
[/code]
correct?

I would create the arrays when the page loads (but not in a function). These arrays will have the lab as the key, and the page as the value.

So within the html <head> tag: (note ive just script to scrip in this code - it wasn't posting for some reason!)

[code]
<scrip language="JavaScript" type="text/javascript">
  var nairobiArray = new Array();
      nairobiArray["langata"] = "langata.php";
      nairobiArray["weslands"] = "weslands.php";
  var kisumuArray = new Array(); // etc etc
</scrip>
[/code]


If the area's are constant, so the first list box never changes values then i would fill in the values in html...
[code]<select name='karea' onchange='JavaScript: changearea();'>
  <option value=''>Select Area</option>
  <option value='nairobi'>Nairobi</option>
  <option value='kisumi'>Kisumi</option>
</select>
[/code]

Then the function changearea();
[code]
function changeaera()
{
    area = ff_getElementByName('karea');
    lab  = ff_getElementByName('klab');

    if (area.value == 'nairobi') { var temparr = nairobiArray; } // get the values of the array to use and load it into a temp array
    if (area.value == 'kisumu') { var temparr = kisumuArray; }

  lab.options.length = 0; // reset the dropdown
  lab.options[0] = new Option("Select Lab", ""); // add a default option with no value
  lab.value = 0;

  var op = 1;  //set in incremental options count
  for (var key in temparr) // loop through the array
  {
      lab.options[op++] = new Option(key, temparr[key])
  }
}
[/code]
the the function labredirect which is onchange for ther dropdow

[code]function labredirect()
{
    lab  = ff_getElementByName('klab');
    var page = lab.value;
    location.href = page;
}[/code]

i have't actually tested this code (just typing as i go) so theres probably a few mistakes etc... but what do you think of the idea?

cheers,
Michael

Share this post


Link to post
Share on other sites
Hi i will try it , can i do it in php instead fo java eg

var nairobiArray = new Array(
            'langata' = "langata.php"
            'weslands'= "weslands.php"

);

will this work ,so that when a user selects a nairobi in the firstdrop down it will display langata & westlands in the second drop down and when he selects langata it take him to such a url http://mysite.com/ index.php?option=com_content&task=view&id=18&Itemid=27

please

Share this post


Link to post
Share on other sites
Hi

Yes, you can do it in php - but the disadvantage is that you will need to submit and reload the page to repopulate the second drop down, and the submit and reload the page when the second dropdown is changed in order to use the post variable for the redirect.

Using javascript here is more of a mission - but its saves the reloading all the time. here is an example of how to do it in php

ie:

[code]
<?php

// if the lab drop down was selected then redirect
if ($_POST["klab"] != "")
{ header ("location: " . $_POST["klab"]); }

// otherwise build the selects: the first one (area) being static
$areaselect=
          "<select name='karea' onchange='Javascript: document.submit();'>
            <option value=''>Select Area</option>
            <option value='nairobi'>Nairobi'</option>
            <option.....>
          </select><br>";

// set the value of the select to the posted value (if there is one, otherwise it will set to default
$areaselect = str_replace("option value='".$_POST["karea"]."'>", "option value='".$_POST["karea"]."' selected>", $areaselect);


// the second select (lab) is dynamic according to the $_POST variable of karea
$labselect
        "<select name='klab' onchange='Javascript: document.submit();'>
            <option value=''>Select Lab</option>";
langata.php
// you can either user arrays to do this, or just add the options according to the value of karea
// if there aren't many, then its probably easier to just do it manually
if ($_POST["karea"] == "nairobi")
{
$labselect.=
        "  <option value='langata.php'>Langata</option>
            <option.....>";
}
else if ($_POST["karea"] == "kisumi")
{
$labselect.=
        "  <option value='city.php'>City</option>
            <option.....>";
}

// then end the select
$labselect.= "</select>";
         
// then echo out the html:

echo "<html>\n<br>Area:$areaselect<br>Lab:$labselect<br></html>";

?>
[/code]

is that better for you?

cheers,
tdw

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.