0

I have a select field which has ID name as 'region_code' well as its value. And I want pass ID in ajax. As you can see below, the input field is not included in any form. It has a value and the ID

Is it possible to get value in ajax as shown below?

  echo '<select id="region_code" onchange="show_region_code();">';

  $result = mysql_query("SELECT region_code, region_name FROM list_region");
  while($rows = mysql_fetch_array($result)) {                      
             echo "<option value=\"$rows[0]\">".$rows["1"].'</option>';
  }
  echo '</select>';

My ajax function as below

function show_region_code() {

     var region_code = $("#region_code").val();

     $.ajax ({
            type: "POST",
            url: "show_region_code.php",
            data: { region_code1: region_code },
              success: function(data) {
                     $("#region_code").html(data);
            }
     });
 }
user3766078
  • 81
  • 1
  • 1
  • 11

3 Answers3

1

Use this

<select id="region_code" onchange="show_region_code(this);">
    <option value="1">te</option>
    <option value="2">te2</option>
</select>

function show_region_code(Obj)
{
    alert(Obj.id);
}
Mayank Sharma
  • 844
  • 7
  • 21
1

see this it's your code only and working:

<?php 
echo '<select id="region_code" onchange="show_region_code();">';
$rows=0;
  //$result = mysql_query("SELECT region_code, region_name FROM list_region");
  while($rows !=2) { 
    $rows++;
             echo "<option value=$rows>".$rows.'</option>';

  }
  echo '</select>';
?>
<script>
function show_region_code() {

     var region_code = $("#region_code").val();
alert(region_code);
     $.ajax ({
            type: "POST",
            url: "show_region_code.php",
            data: { region_code1: region_code },
              success: function(data) {
                     $("#region_code").html(data);
            }
     });
 }

</script>
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
0
<?php         
   echo '<select id="region_code" onchange="show_region_code();">';

   $result = mysql_query("SELECT region_code, region_name FROM list_region");
   while($rows = mysql_fetch_array($result)) {                      
      echo "<option value=".$rows[0].">".$rows[1]."</option>";
   }
   echo '</select>';
?>

function show_region_code() {

     var region_code = $("#region_code").val();

     $.ajax ({
            type: "POST",
            url: "show_region_code.php",
            data: "region_code1="+region_code,
            success: function(data) {
               $("#region_code").html(data);
            }
     });
 }
Edrich
  • 232
  • 2
  • 8