简体   繁体   English

获取选择框以在其他选择框中显示选择的值

[英]getting select box to display selected value in different select box

I have two select boxes in which I want to select a value for one and the second select box should get same value. 我有两个选择框,我要在其中选择一个值,第二个选择框应获得相同的值。 Currently I am passing id and want my designation also to pass to ajax. 目前,我正在传递ID,并希望我的指定也传递给Ajax。 Can I know how this can be implemented via ajax. 我能知道如何通过ajax实现吗。 Any help will be highly appreciated. 任何帮助将不胜感激。

      <select name="designation" class="form-control"  id="desig" >
        <option value="">Select a Designation/Role</option>
       <?php 

            $sql = mysql_query("SELECT id, designation FROM tbl where status =1 and designationtype_id = 1 ");
                  while ($rows = mysql_fetch_assoc($sql)){   
                echo "<option value=" . $rows['id'] . ">" . $rows['designation'] . "</option>";
         }

?>      <select name="dd" id="dd" class="form-control" disabled>
        <option value=""></option>
      </select> 

My AJAX, 我的AJAX

  <script type="text/javascript">

  $(document).ready(function() {

    $("#desig").change(function() {
      var id = $(this).val();
      var dataString1 = 'id=' + id;
      var des = $(this).val();
      var dataString2 = 'designationname=' + des;
      $.ajax({
        type: "POST",
        url: "escalation_ajax.php",
        data: dataString,
        cache: false,
        success: function(html) {
          var data = html.split(",");

          $('#rephead').val(data[0]);

        }
      });
    });
  });
</script>

escalation_ajax.php escalation_ajax.php

<?php

if ($_POST['id'])
  {
  if ($_POST['des'])
    {
    $des_id = $_POST['id'];
    $designation = $_POST['des'];
    $sql = mysql_query("SELECT designation_id, reporting_head FROM aafmindia_in_sbi.tbl_reporting_head WHERE status=1 and  reporting_head_for='$des_id'");
    if ($sql === FALSE)
      {
      trigger_error('Query failed returning error: ' . mysql_error() , E_USER_ERROR);
      }
      else
      {
      while ($row = mysql_fetch_array($sql))
        {
        $id = $row['designation_id'];
        $reporting_head = $row['reporting_head'];
        echo '<option value="' . $id . '">' . $reporting_head . '</option>' . ',' . '<option value="' . $des_id . '">' . $designation . '</option>';
        }
      }
    }
  }

?>

What you could do, is have the second select (the one that needs the same value as the first) in a seperate file that you load via AJAX. 您可以做的是,在通过AJAX加载的单独文件中,选择第二个(与第一个需要相同的值)。

AJAX function: AJAX功能:

function selection()
{
  var selectValue=$("select#dd option:selected").val();

  $.ajax({
    type : "POST",
    url : "escalation_ajax.php",
    data : { id : selectValue },
    success: function (html) {
      $("#secondSelectorDiv").html(html);                   
    }
  })
}

What this does, is that when the selection() function is called, it will post the selected value of the first select to "escalation_ajax.php". 它的作用是,当调用selection()函数时,它将把第一个选择的选定值发布到“ escalation_ajax.php”。 It will then load that page into an element (div element in my example) with the id "secondSelectorDiv". 然后,它将将该页面加载到ID为“ secondSelectorDiv”的元素(在我的示例中为div元素)中。

The html for the select with the function (which I will call onchange in this example), can look like this: 用于带有功能的select的html(在此示例中,我将称为onchange)如下所示:

<select id="dd" onchange="selection();">
  <option value=""></option>
</select>

<div id="secondSelectorDiv"></div>

Now in escalation_ajax.php you can retrieve the post variable and use it to look for the id in question for the second select. 现在,在escalation_ajax.php中,您可以检索post变量,并使用它查找第二个选择项所涉及的ID。

<?php
$id=$_POST['id'];

/*
If you're using the id to fetch something in your database,
which it looks like you're doing, then use the post variable
to fetch your rows and build the select from that.
*/

$sql="SELECT * FROM table_name WHERE id='$id'";
$result_set=mysql_query($sql);                                   
$row=mysql_fetch_array($result_set);

$count=mysql_num_rows(result_set);
$counter=0;

//this is the id you will check for in order to see what's to be selected
$idToCheck=$row['id'];
?>
<select id="dd2">
  while($count > $counter)
  {
    counter++;

    echo '<option value=""'; if($idToCheck == $id){ echo 'selected="selected"'; } echo '></option>';
  }
?>

If you want the second select to be displayed before the first select has a value, you can simply just call an AJAX function that loads in the second select on page load. 如果要在第二选择项显示一个值之前显示第二选择项,则只需调用一个AJAX函数即可在页面加载时在第二选择项中加载。

IMPORTANT!: You should really switch to mysqli_* or PDO instead of using the deprecated mysql_*. 重要!:您应该真正切换到mysqli_ *或PDO,而不要使用不推荐使用的mysql_ *。 You should at the very least look into sanitizing your inputs. 您至少应该考虑对输入内容进行清理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM