0

It seems tedious to repeat php blocks for every option of a list of dropdown choices, as in this case:

<select name="religion" id="relaff" onchange="changeList(this)">
  <option <?php if($religion == "Paganism"){echo " selected=\"Paganism\"";} ?> value="Paganism">Paganism</option>
  <option <?php if($religion == "Wicca"){echo " selected=\"Wicca\"";} ?> value="Wicca">Wicca</option>
  <option <?php if($religion == "Witchcraft"){echo " selected=\"Witchcraft\"";} ?> value="Witchcraft">Witchcraft</option>
  <option <?php if($religion == "Islam"){echo " selected=\"Islam\"";} ?> value="Islam">Islam</option>
  <option <?php if($religion == "Buddhism"){echo " selected=\"Buddhism\"";} ?> value="Buddhism">Buddhism</option>
  <option <?php if($religion == "No religion"){echo " selected=\"No religion\"";} ?> value="No religion">No religion</option>
  <option <?php if($religion == "Christianity"){echo " selected=\"Christianity\"";} ?> value="Christianity">Christianity</option>
  <option <?php if($religion == "Other"){echo " selected=\"Other\"";} ?> value="Other">Other</option></select>

Which is the worst case since in the same form, I have another dropdown menu that auto-generates a value depending on what is selected on the dropdown coded above. Here's the second dropdown menu related to the one above:

<select name="denomination" id="reldet">
<option 
<?php echo " selected=" . $denomination . ""; ?>
value="NONE">This will update after selecting a religion</option></select>

Can someone help me fix this? It seems confusing. EDIT: The JS file that has the function changeList is here:

var lists = new Array();

lists['Select religion']    = new Array();
lists['Select religion'][0] = new Array(
'This will update after selecting a religion'
);
lists['Select religion'][1] = new Array(
''
);

// First set of text and values
lists['Paganism']    = new Array();
lists['Paganism'][0] = new Array(
'Hellenic',
'Asatru',
'Roman',
'Celtic'
);
lists['Paganism'][1] = new Array(
'Hellenic',
'Asatru',
'Roman',
'Celtic'
);

// Second set of text and values
lists['Wicca']    = new Array();
lists['Wicca'][0] = new Array(
'Gardnerian',
'Eclectic',
'Inclusive',
'Solitary'
);
lists['Wicca'][1] = new Array(
'Gardnerian',
'Eclectic',
'Inclusive',
'Solitary'
);

lists['Witchcraft']    = new Array();
lists['Witchcraft'][0] = new Array(
'British Traditional',
'Santeria',
'Voodoo'
);
lists['Witchcraft'][1] = new Array(
'British Traditional',
'Santeria',
'Voodoo'
);

lists['Christianity'] = new Array();
lists['Christianity'][0] = new Array(
'Catholic',
'Aglipayan',
'Protestant',
'Pentecostal',
'Baptist',
'Methodist',
'Iglesia Ni Cristo',
'Latter-Day Saints (Mormons)',
'Seventh-Day Adventist',
// string escape method here
'Jehovah\'s Witnesses'
);
lists['Christianity'][1] = new Array(
'Catholic',
'Aglipayan',
'Protestant',
'Pentecostal',
'Baptist',
'Methodist',
'Iglesia Ni Cristo',
'Latter-Day Saints (Mormons)',
'Seventh-Day Adventist',
// string escape method here
'Jehovah\'s Witnesses'
);

lists['Other']    = new Array();
lists['Other'][0] = new Array(
'Judaism',
'Sikhism',
'Shintoism'
);
lists['Other'][1] = new Array(
'Judaism',
'Sikhism',
'Shintoism'
);

lists['No religion']    = new Array();
lists['No religion'][0] = new Array(
'Atheist',
'Agnostic'
);
lists['No religion'][1] = new Array(
'Atheist',
'Agnostic'
);

// This function goes through the options for the given
// drop down box and removes them in preparation for
// a new set of values

function emptyList( box ) {
// Set each option to null thus removing it
while ( box.options.length ) box.options[0] = null;
}

// This function assigns new drop down options to the given
// drop down box from the list of lists specified

function fillList( box, arr ) {
// arr[0] holds the display text
// arr[1] are the values

for ( i = 0; i < arr[0].length; i++ ) {

    // Create a new drop down option with the
    // display text and value from arr

    option = new Option( arr[0][i], arr[1][i] );

    // Add to the end of the existing options

    box.options[box.length] = option;
}

// Preselect option 0

box.selectedIndex=0;
}

// This function performs a drop down list option change by first
// emptying the existing option list and then assigning a new set

function changeList( box ) {
// Isolate the appropriate list by using the value
// of the currently selected option

list = lists[box.options[box.selectedIndex].value];

// Next empty the slave list

emptyList( box.form.denomination );

// Then assign the new list values

fillList( box.form.denomination, list );
}
2
  • First of all have a look at the selected attribute in your code and compare it with the documentation at w3schools.com/tags/tag_option.asp. Hint: the selected attribute takes only a "selected" as value. What does the changeList() javascript function? Can you provide any code? The php code in the second dropdown menu is confusing. What does $denomination before the <?php marks? Commented Oct 11, 2013 at 6:52
  • I just added the javascript code. The $denomination is a typo. I'll fix that. Commented Oct 11, 2013 at 6:55

1 Answer 1

1

There many ways to avoid tedious work. One of them might be:

<?php 
 $options = array('Paganism', 'Wicca', ... );
?>

<select name="religion" id="relaff" onchange="changeList(this)">
   <?php 
     foreach($options as $option){
   ?>
     <option <?php if($religion == $option){echo " selected=\"$option\"";} ?> value="<?php echo $option;?>"><?php echo $option ; ?></option>

   <?php } ?>


</select>

However, to change the options of another dropdown when a selection is made, I think it is a Javascript's job unless you refresh the page upon selection.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.