Gold Pearl Gold Pearl - 3 years ago 238
Ajax Question

Post option text instead of value in PHP

I want to post the data like USA and Florida instead of numbers (1 and 1). I tried to change

.value
to text but the code was not working.

This for filter city and state based on country but I have to update database with country and state name, currently I am getting values only, is there anyway to fix this?

DB structure

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;
}
?>


Selector

<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>

Answer Source

on DB, you must change the type of fields to varchar, so you can set the values as string (usa or other country) instead of numbers. set the field "category" as foreing key in sub category table...

set AI in each table, so you can make queries about countries and cities name...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download