weixin_33716941 2017-08-08 16:42 采纳率: 0%
浏览 16

将数据传递到两个字段

Hi I am trying to make a autocomplete function where I use a search field and write the first letters of the name of a company. the code vill search an online database for the company and there will be a list of suggestions. for example company1 street1 zipcode etc. company2 astreet2 zipcode etc. company3 street1 zipcode etc.

I will click on one of the listed companies and it vill populate field1 with company1 field2 street field3 zipcode etc. but I cant make it. I dont reallt know how to do this and Im trying to learn and understand but this is too complex for me so I hope any of you guys can hekp me - thanks in advance.

This is how my code looks like so far. main page:

<!doctype html>
<html>
   <head>
      <meta charset="utf-8">
      <title></title>
      <link href="layout.css" rel="stylesheet" type="text/css">
      <link href="menu.css" rel="stylesheet" type="text/css">
   </head>
   <style>
        .kundsok {
            border: 1px solid #a8d4b1;
            background-color: #c6f7d0;
            margin: 2px 0px;
            padding: 40px;
            border-radius: 4px;
        }
        #ftg-list {
            float: center;
            list-style: none;
            margin- top: -3px;
            padding: 0;
            width: 190px;
        }
        #ftg-list li {
            padding: 10px;
            background: #f0f0f0;
            border-bottom: #bbb9b9 1px solid;
        }
        #ftg-list li:hover {
            background: #ece3d2;
            cursor: pointer;
        }
        #search-box {
            padding: 10px;
            border: #a8d4b1 1px solid;
            border-radius: 4px;
        }
   </style>
   <script src="jquery-3.2.1.min.js" type="text/javascript"></script>
   <script>
        $(document).ready(function() {
            $("#search-box").keyup(function() {
                $.ajax({
                    type: "POST",
                    url: "readkund_eniro.php",
                    data: 'keyword=' + $(this).val(),
                    beforeSend: function() {
                        $("#search-box").css("background", "#FFF url(LoaderIcon.gif) no-repeat 165 px ");
                    },
                    success: function(data) {
                        $("#suggesstion-box").show();
                        $("#suggesstion-box").html(data);
                        $("#search-box").css("background", "#FFF");
                    }
                });
            });
        });
        function selkund(val, val2) {
            $("#search-box").val(val);
            $("#foretag_name").val(val);
            $("#stad").val(val2);
            $("#suggesstion-box").hide();
        }
    </script>
   <body>
      <div id="sidlayout">
      <div id="content">
         <?php include "menukunder.php"; ?>
         <div class="kundsok">
            <center>
            <input type="text" id="search-box" name ="search-box" placeholder="kund" >
            <div id="suggesstion-box"></div>
            </form>
            <h1></h1>
            <H3></H3>
            <center>
            <form action="sida3ka.php" method="post">
               <fieldset>
                  <label title="Skriv in företagets namn"  for="foretag_name">Företag:</label>
                  <input type="text" name="foretag_name"  id="foretag_name">
                  <label title="Skriv in stad" for="stad">Stad:</label>
                  <input type="text" name="stad" id="stad">    
               </fieldset>
            </form>
         </div>
      </div>
   </body>
</html>

on the other page called readkund_eniro.php the code looks like this

$url = 'https://api.eniro.com/cs/search/basic?profile=Martens&key=3653823918839001013&country=se&version=1.1.3&search_word
  =' . $_POST["keyword"] . '&geo_area=stockholm';
$response = file_get_contents($url);
$json = json_decode($response, true);
if(!empty($json)) {
    ?>
        <ul id="ftg-list">
    <?php
        foreach($json['adverts'] as $ftg) {
    ?>
            <li onClick="selkund('<?php
                echo $ftg["companyInfo"]['companyName'];
                echo $ftg["address"]['streetName'];
        ?>');"><?php
                    echo $ftg["companyInfo"]['companyName'];
        ?>
            </li>
        <?php
        }
    ?>
        </ul>
    <?php
}

I the database respons will be

$ftg["companyInfo"]['companyName']
$ftg["address"]['streetName']
$ftg["address"]['postCode']

and so on. thanks again Best regards Johan

  • 写回答

1条回答 默认 最新

  • weixin_33690367 2017-08-08 17:54
    关注

    i think there is problem in API url at readkund_eniro.php file. can you please try with as below.

    $url = "https://api.eniro.com/cs/search/basic?profile=Martens&key=3653823918839001013&country=se&version=1.1.3&search_word=".$_POST['keyword']."&geo_area=stockholm";
    

    another thing i found with your response text inside

    <li onClick="selkund('<?php echo $ftg["companyInfo"]['companyName'];?>', '<?php echo  $ftg["address"]['streetName'];?>');"><?php
                    echo $ftg["companyInfo"]['companyName']. ', '. $ftg["address"]['streetName'].', '. $ftg["address"]['postCode'] ;
        ?>
            </li>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛