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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line