0

私は、ユーザーがプルダウン メニューを使用してフォームに入力できるように、Web サイトのフォームを最適化することに取り組んでいます。PHP クエリに基づいて単一のプルダウン メニューを設定する方法を理解しましたが、その選択に基づいて別のプルダウン メニューを設定する必要があります。select タグに onchange 属性を使用しようとしています。JavaScript でのクエリがまだ動的ではないことはわかっていますが、次のプルダウンを最初に表示するようにしようとしていますが、そうではありません。最初のプルダウンから何かを選択しても何も起こりません。助けてくれてありがとう。

$query2 = "select distinct Provider_Name from CE_ACTIVITY_LIST_T where IS_ACTIVE = 'YES'";
$result2 = mysql_query($query2, $link) or die(mysql_error());
//$row2 = mysql_fetch_array($result2) or die(mysql_error());
echo "<table border='0'><tr><th>";
echo "Course Provider:</th><td><select name='providerName' id='provider'onchange='getResult()'>";
echo "<option SELECTED>Pick Provider</option>";
$i=0;
while($row2 = mysql_fetch_array($result2))
{
    echo "<option value=\"" . $row2['Provider_Name'] . "\">" . $row2['Provider_Name'] . "</option>";
    $i++;
}
echo "</select></td></tr><tr></tr>";

<script type="text/javascript">
function getResult(field)
{
    var field=field;
    var isEqualTo=document.getElementById('provider');
    document.getElementById('selectCourse').innerHTML = '<?php
    include ('connectionOpen.php');
    $queryCourse="select Course_Title from CE_ACTIVITY_LIST_T where Provider_Name = 'Test 1' and isActive = 'YES'";
    echo "<tr><th>";
    echo "Course Title:</th><td><select name='courseTitle'>";
    echo "<option SELECTED>Which course?</option>";
    $i=0;
    while($row2 = mysql_fetch_array($result2))
    {
        echo "<option value=\"" . $row2['Course_Title'] . "\">" . $row2['Course_Title'] . "</option>";
        $i++;
    }
    echo "</select>";
    echo "</td></tr></table>";
    ?>';

}
</script>
4

1 に答える 1

0

If you want to populate another list then you need to load it with ajax, you can use jQuery to make it easier. First create php script that will generate html for second option list, eg. myscript.php, this script should receive parameter name.

Be sure that jQuery is included. Then modify your javascript as follows:

function onChange()
{
    var name = providerName.value; 
    var url = "path-to-script";
    $.get(url, {name:name}  function(html){
        $("#result").html(html) 
    });
}

Instead of second option use <div id='result'></div> as placeholder for new option list.

于 2012-11-02T22:41:46.590 に答える