需要帮助ajax建议下拉列表

时间:2014-12-09 15:04:55

标签: javascript php jquery ajax

我有一个ajax建议下拉菜单,我想让它显示一些团队名称,但它会显示"。$ team。"

我的HTML

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html;charset=iso-8859-1">



    <style type="text/css">



    #mainContainer{
        width:660px;
        margin:0 auto;
        text-align:left;
        height:100%;

        border-left:3px double #000;
        border-right:3px double #000;
    }
    #formContent{
        padding:5px;
    }



    /* Big box with list of options */
    #ajax_listOfOptions{
        position:absolute;  /* Never change this one */
        width:175px;    /* Width of box */
        height:250px;   /* Height of box */
        overflow:auto;  /* Scrolling features */
        border:1px solid #317082;   /* Dark green border */
        background-color:#FFF;  /* White background color */
    color: black;
        text-align:left;
        font-size:0.9em;
        z-index:100;
    }
    #ajax_listOfOptions div{    /* General rule for both .optionDiv and .optionDivSelected */
        margin:1px;     
        padding:1px;
        cursor:pointer;
        font-size:0.9em;
    }
    #ajax_listOfOptions .optionDiv{ /* Div for each item in list */

    }
    #ajax_listOfOptions .optionDivSelected{ /* Selected item in the list */
        background-color:#317082;
        color:#FFF;
    }
    #ajax_listOfOptions_iframe{
        background-color:#F00;
        position:absolute;
        z-index:5;
    }

    form{
        display:inline;
    }
    </style>
   </head>
   <body>
   <script type="text/javascript" src="javascript/ajax.js"></script>
    <script type="text/javascript" src="javascript/ajax-dynamic-list.js"></script>
   <center>

    <form>
        <fieldset>
                    <h1>Enter your team team name to see starting 11 and Crest</h1>
            <table border="0">
                <tr>
                    <td><label for="team">Team: </label></td>
                    <td><input type="text" id="team" name="team" value=""   onkeyup="ajax_showOptions(this,'getTeamsByLetters',event)">
                    <input type="hidden" id="team_hidden" name="team_ID"><!-- THE ID OF the country     will be inserted into this hidden input --></td>
                </tr>   

            </table>        

        </fieldset> 
        </form>
  </center>


    </body>
    </html>

这取自我的php,其中包含html从

获取的团队列表
<?php
$string = 
"arsenal,
manchester united,
manchester city,
 cheslea,
tottehnam hotspur,
 southampton,
 everton,
 aston villa,
 leicester city,
westham united,
newcastle united,
queens park rangers,
sunderland,
swansea,
hull city,
 west bromwich albion,
crystal palace,
 burnley,
stoke city";

$aTeams = explode(',', $string);

if(isset($_GET['getTeamsByLetters']) && isset($_GET['letters'])){
    $letters = $_GET['letters'];
    $letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
    //$res = mysql_query("select ID,teamName from ajax_countries where TeamName like '".$letters."%'") or die(mysql_error());
  foreach($aTeams as $key => $team) {
    $team = strtolower($team);
    if(strpos($team, $letters)!==false)
      echo $key."###".$team."|";
  }
}
?>

2 个答案:

答案 0 :(得分:0)

你的php函数没有返回任何内容。此外,您正在回应每一个团队,但您并没有将其附加到其他团队。你能提供ajax_showOptions函数吗?我找不到它

如果您尝试使用以下内容会发生什么:

 $res = "";

  foreach($aTeams as $team) {
    $team = strtolower($team);
    if(strpos($team, $letters)!==false)
      $res.= $key."###".$team."|";
  }

return $res;

答案 1 :(得分:0)

你需要将结果收集到某个变量然后回显它:

    if(isset($_GET['getTeamsByLetters']) && isset($_GET['letters'])){
        $letters = $_GET['letters'];
        $letters = preg_replace("/[^a-z0-9 ]/si","",$letters);
        //$res = mysql_query("select ID,teamName from ajax_countries where TeamName like '".$letters."%'") or die(mysql_error());
    $result = "";
      foreach($aTeams as $key => $team) {
        $team = strtolower($team);
        if(strpos($team, $letters)!==false)
         $result .= $key."###".$team."|";
      }
print_r($result) // see in your console what value it gives in response.
    }
    echo $result;