如何进行传递id和显示名称的jQuery自动完成

时间:2017-02-09 11:57:48

标签: php jquery autocomplete get

我是php的新手。我有一个工作正常的脚本。但问题是我想传递已完成名称的id。

我的输入文字:

<tr><td>Marital Status</td><td><input type="text" id="m_status"    
name="m_status" value="<?php echo $ddlMarital >0 ?    
$MartialArray[$ddlMarital] : '' ;?>"></td></tr>

脚本:

$(function() {

     $( "#m_status" ).autocomplete({
        source: 'Getmstaus.php'
    });

Getmstaus.php:

<?php
include("include/connection.php");

$searchTerm = $_GET['term'];

    //get matched data from skills table
    $query = $db->query("SELECT * FROM m_status WHERE m_status LIKE   
   '%".$searchTerm."%' ORDER BY m_status ASC");
    while ($row = $query->fetch_assoc()) {
        $data['name'] = $row['m_status'];
         $data['id'] = $row['m_id'];

    }


    echo json_encode($data);

现在我收到M_status name,如果我提交m_status将提交的表单。但我想显示m_status,而在表单提交时我想提交m_id。我怎么能这样做?请帮助我。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

试试这希望这会对你有所帮助

    tr><td>Marital Status</td><td>
      <input type="text" id="m_status"    
        name="m_status" value="<?php echo $ddlMarital >0 ?    
        $MartialArray[$ddlMarital] : '' ;?>">
    </td>
   <input type="hidden" name="hidden" id="project-id">

    </tr>

<强>脚本

<script>



  $( function() {


    $( "#m_status" ).autocomplete({
      minLength: 0,
      source: 'Getmstaus.php',

      select: function( event, ui ) {
     $('#project-id').val(ui.item.id);

        //return false;
      }
    })

  } );
  </script>

<强> Getmstaus.php:

    <?php
    include("include/connection.php");

    $searchTerm = $_GET['term'];

        //get matched data from skills table
        $query = $db->query("SELECT * FROM m_status WHERE m_status LIKE   
       '%".$searchTerm."%' ORDER BY m_status ASC");
        while ($row = $query->fetch_assoc()) {

 $data[] = array(
    "value" => $row['m_status',
    "id" => $row['m_id'],

);

        }


        echo json_encode($data);