dtdfj08626 2017-02-09 03:57 采纳率: 0%
浏览 151
已采纳

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

I am new in php.I have a script that work fine.but the problem is that i want to pass the id of the name completed.

My input text:

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

Script:

$(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);

Now I get M_status name and if i submit the form that m_status will submit.But i want to display m_status and while form submit i want to submit m_id.How can i do this?Please help me.Any help would be appreciated.

展开全部

  • 写回答

1条回答 默认 最新

  • duandao3265 2017-02-09 22:35
    关注

    try this Hope this will help you

        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

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

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部