dsdtumf776629385 2013-01-10 23:22
浏览 81
已采纳

单击按钮填充列表框

I am trying to fill a listbox on a webpage and I want the listbox to start blank. Once the button is clicked the listbox should populate. My code below automatically fills the listbox but I would rather have the button do it.

<?php 
$dbc = mysql_connect('','','') 
     or die('Error connecting to MySQL server.'); 

mysql_select_db('MyDB'); 

$result = mysql_query("select * from tblRestaurants order by RestName ASC"); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 

<head> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

<title>SEARCH</title> 


 </head> 

<body> 
<form method="post" action="1004mcout.php">



<p><center>SEARCH</CENTER></P> 
<select name="RestName"> 
<?php 
 while ($nt= mysql_fetch_assoc($result))  
{ 
     echo '<option value="' . $nt['RestID'] . '">' . $nt['RestName'] . '</option>'; 
}  
?> 
</select>

<p> SPACE</p> 

<p>Click "SUBMIT" to display the calculation results</p> 

<input type="submit" name="Submit" value="Submit" /> 

<br /> 

</form> 

</body> 

</html> 
  • 写回答

3条回答 默认 最新

  • doutuo2829 2013-01-11 00:21
    关注

    I would either: Preload the data into the page as some ready but invisible html list (maybe a bit n00b), or save the data as a javascript array and a function will load it into the page (better), or do an ajax call to the same page (for simplicity) (probably best, leaves you the option open for updated data after page initiation).

    The Ajax route will have to use jQuery (change this_page.php to whichever page this is called):

    <?php
    
    
    while ($nt= mysql_fetch_assoc($result))
        $arrData[] = $nt;
    
    //If you want to test without DB, uncomment this, and comment previous
    /*$arrData = array(
            array('RestID' => "1", 'RestName' => "Mike"),
            array('RestID' => "2", 'RestName' => "Sebastian"),
            array('RestID' => "3", 'RestName' => "Shitter")
            );*/
    
    if(isset($_GET["ajax"]))
    {
        echo json_encode($arrData);
        die();
    }
    ?>
    <html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
    function displayItems()
    {
        $.getJSON("this_page.php?ajax=true", function(data) {
            $.each(data, function(index, objRecord) {
                var option=document.createElement("option");
                option.value=objRecord.RestID;
                option.text=objRecord.RestName;
                $("#RestName").append('<option value="' + objRecord.RestID + '">' + objRecord.RestName + '</option>');
            });
        });
    
    }
        </script>
        <title>SEARCH</title>
    </head>
    <body>
        <form method="post" action="1004mcout.php">
            <p><center>SEARCH</CENTER></P>
            <select id="RestName"></select>
            <p> SPACE</p> 
            <p>Click "SUBMIT" to display the calculation results</p> 
            <button type="button" onclick="javascript:displayItems();">Insert options</button>
            <br /> 
        </form> 
    </body> 
    </html> 
    

    Essentially, what it does, it collects the data, checks if there is a request for the ajax data in the url, if not, it prints the rest of the page (with an empty select). If there is an ajax flag in the url, then the php will encode the data into json, print that and stop. When the User receives the page with an empty select, it clicks the button which will trigger the displayItems() function. Inside that function, it does a jQuery-based ajax call to the same page with the ajax flag set in the url, and the result (which is json), is evaluated to a valid javascript array. That array is then created into options and loaded into the RestName SELECT element.

    A final cookie? You could just print the data as options, into the select anyway, just like the previous answers described. Then, inside the displayItems() function, you clear the select before loading it from the jQuery/ajax call. That way, the user will see data right from the beginning, and will only update this with the most recent data from the DB. Clean and all in one page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题