0

我正在尝试编写一个代码,其中我动态生成选择框选项。我需要从这个 onchange 事件中发送 2 个值。1 是 sub_header_id,其他是 header_id。我需要将 $row["header_id"] 发送到 javascript 函数。以下是我的 PHP 代码。我怎么做?我不想发送连接的字符串,然后将其拆分为 javascript 函数。

<?php
    include('config.php');
    $sql = sprintf("SELECT * FROM retail_sub_headers");
    $result = mysql_query($sql);
    $num = mysql_num_rows($result);
    if ($num > 0)
    {
        echo "<form>";
        echo "<select id = \"my_select_box\" onChange = \"javascript:load_brand_option_box(this, 'populate_brands', '');\">";
        echo "<option selected value = \"-------\">-------</option>";
        while($row = mysql_fetch_array($result))
        {
            echo "<option value = ".$row["sub_header_id"].">".$row["sub_header_name"]."</option>";
        }
        echo "</select>";   
        echo "</form>";
    }
?>

这是我的 javascript 函数 javascript:load_brand_option_box。

function load_brand_option_box (sub_header_id, action, brand_id)
{
    var sub_header_value = sub_header_id.options[sub_header_id.selectedIndex].value
     if (action == "populate_brands")
     {
        if (window.XMLHttpRequest)
        {  // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }   
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("brand_names").innerHTML=xmlhttp.responseText;
                //document.getElementById("livesearch").style.border="1px solid #A5ACB2";
            }
        }   
        xmlhttp.open("GET","populate.php?action="+action+"&sub_header_id="+sub_header_value+"&brand_id="+"",true);
        xmlhttp.send();
    }
}
4

1 回答 1

1

试试这个......newval在选项中添加了一个属性,header id 您现在可以grab header idfunction

<?php
        include('config.php');
        $sql = sprintf("SELECT * FROM retail_sub_headers");
        $result = mysql_query($sql);
        $num = mysql_num_rows($result);
        if ($num > 0)
        {
            echo "<form>";
            echo "<select id = \"my_select_box\" onChange = \"load_brand_option_box(this, 'populate_brands', '');\">";
            echo "<option selected value = \"-------\">-------</option>";
            while($row = mysql_fetch_array($result))
            {
                echo "<option value = ".$row["sub_header_id"]."  newval=".$row["header_id"].">".$row["sub_header_name"]."</option>";
            }
            echo "</select>";   
            echo "</form>";
        }
    ?>

    function load_brand_option_box (sub_header_id, action, brand_id)
    {
        var sub_header_value = sub_header_id.options[sub_header_id.selectedIndex].value
        var sel = document.getElementById('my_select_box');
        var header_id=sel.options[sel.selectedIndex].getAttribute('newval');
        //you will get the header id here 
         if (action == "populate_brands")
         {
            if (window.XMLHttpRequest)
            {  // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }   
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("brand_names").innerHTML=xmlhttp.responseText;
                    //document.getElementById("livesearch").style.border="1px solid #A5ACB2";
                }
            }   
            xmlhttp.open("GET","populate.php?action="+action+"&sub_header_id="+sub_header_value+"&brand_id="+"",true);
            xmlhttp.send();
        }
    }
于 2013-05-26T12:14:29.290 回答