1

I am trying to create dynamic drop down lists in php using javascript. I am able to get the value from the dropdown list and display the second list values. However, that list gets displayed on the next page. How do i get it to display on the same page?

This is on the form page:

$(document).ready(course_selectbox_change);
function course_selectbox_change() {
    $('#course').change(update_section_list);
}

function update_section_list() {
    var course=$('#course').attr('value');
    $.get('hashtag.php?course='+course, generateSelect);
}              

function show_sections(sections) {
    $('#section').html(sections);
}



<?php
    include "database.php";
    $course= ($_REQUEST['course']);
    $semester = ($_REQUEST['semester']);

    if (isset($course)) {
        $sections = retrieveSection($course);
    }
    if (!$sections) {
        echo 'Select a course first';
    } else {
        echo '<select name="section"><option>' 
            . join('</option><option>', $sections) 
            . '</select>';
    }
?>
1
  • 1
    could you also post the generateSelect function you are calling in your $.get Commented Dec 6, 2011 at 10:11

2 Answers 2

1

You can't use join in this case

it will print out something likes value1<option></option>value2<option></option>value3

try

<select name="section">
<?php
foreach($sections as $key => $value)
{
   echo "<option value='$key'>$value</option>";
}
?>
</select>
Sign up to request clarification or add additional context in comments.

5 Comments

Are you sure? I think it will output one empty option first and an unclosed one at the end. The other ones should work just fine
I'm sure, it will out put exactly value1<option></option>value2<option></option>value3. Because join is alias of implode, it use <option></option> as delimeter
You misread <option></option>; It is actually </option><option> and an opening <option> before the join()
thats right, the closing </option> is missing after the join(); And your solution is even better to read and modify, would do it the exact same way
i tried this but i still don't get it. it still doesn't work. The right dropdown list gets displayed but on the wrong page, and it is only after i submit the value of the first one..
0
  1. Why don't to use native jquery ui autocomplete?
  2. What do you mean by "list gets displayed on the next page"? New content appears below visible window? If "yes", just move #content block higher

1 Comment

when i select a course, i have to click submit before anything changes and the dropdown list which is based on this course input, gets displayed on the form action page rather than the main form page. How do i overcome that?

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.