在PHP中发布选项文本而不是值

时间:2017-09-25 15:04:53

标签: javascript php jquery html ajax

我想发布美国和佛罗里达等数据而不是数字(1和1)。我尝试将.value更改为文本,但代码无效。

这是基于国家的过滤城市和州,但我必须用国家和州名更新数据库,目前我只获得价值,是否还有解决这个问题?

数据库结构

INSERT INTO `case_category` (`id`, `category`) VALUES
(1, 'USA'),
(2, 'China'):

INSERT INTO `case_sub_category` (`id`, `sub_category`, `category_id`) VALUES
(1, 'Florida', 1),
(2, 'Guangdong', 2),
(3, 'Sichuan', 2);

loadData.php     

$loadType=$_POST['loadType'];
$loadId=$_POST['loadId'];

if($loadType=="subCat"){
    $sql="select id,sub_category from case_sub_category where category_id='".$loadId."' order by sub_category asc";
}else{
    $sql="select id,sub_sub_category from case_sub_sub_category where sub_category_id='".$loadId."' order by sub_sub_category asc";
}
$res=mysql_query($sql);
$check=mysql_num_rows($res);
if($check > 0){
    $HTML="";
    while($row=mysql_fetch_array($res)){
        $HTML.="<option value='".$row['id']."'>".$row['1']."</option>";
    }
    echo $HTML;
}
?> 

选择

<table>
    <tr>
        <td>
            <select name="request_type_c"  onchange="selectsubSubCat(this.options[this.selectedIndex].value)">
                <option value="-1">Select Category</option>
                <?php
                while($rowCategory=mysql_fetch_array($resCategory)){
                    ?>
                    <option value="<?php echo $rowCategory['id']?>"><?php echo $rowCategory['category']?></option>
                    <?php
                }
                ?>
            </select>
        </td>
    </tr>
    <tr>
        <td>
            <select name="request_type_c_sub"  id="subCat_dropdown" onchange="selectsubCat(this.options[this.selectedIndex].value)">
                <option value="-1">Select Sub Category</option>
            </select>
            <span id="subCat_loader"></span>
        </td>
    </tr>

    <tr>
        <td>
            <select name="request_type_c_sub_sub" id="subSubCat_dropdown">
                <option value="-1">Select Sub Sub Category</option>
            </select>
            <span id="subSubCat_loader"></span>
        </td>
    </tr>
</table>

AJAX

 <script type="text/javascript">
    function selectsubSubCat(Category_id){
        if(Category_id!="-1"){
            loadData('subCat',Category_id);
            $("#subSubCat_dropdown").html("<option value='-1'>Select Category</option>");   
        }else{
            $("#subCat_dropdown").html("<option value='-1'>Select Sub Category</option>");
            $("#subSubCat_dropdown").html("<option value='-1'>Select Sub Sub Category</option>");       
        }
    }

    function selectsubCat(subCat_id){
        if(subCat_id!="-1"){
            loadData('subSubCat',subCat_id);
        }else{
            $("#subSubCat_dropdown").html("<option value='-1'>Select Sub Sub Category</option>");       
        }
    }

    function loadData(loadType,loadId){
        var dataString = 'loadType='+ loadType +'&loadId='+ loadId;
        $("#"+loadType+"_loader").show();
        $("#"+loadType+"_loader").fadeIn(400).html('Please wait... <img src="image/loading.gif" />');
        $.ajax({
            type: "POST",
            url: "loadData.php",
            data: dataString,
            cache: false,
            success: function(result){
                $("#"+loadType+"_loader").hide();
                $("#"+loadType+"_dropdown").html("<option value='-1'>Select "+loadType+"</option>");  
                $("#"+loadType+"_dropdown").append(result);  
            }
        });
    }
    </script>

1 个答案:

答案 0 :(得分:0)

在DB上,您必须将字段类型更改为varchar,因此您可以将值设置为字符串(美国或其他国家/地区)而不是数字。将字段“category”设置为子类别表中的foreing键...

在每个表格中设置AI,这样您就可以查询有关国家和城市的名称......