Database 从数据库中检索数字并将其作为搜索参数传递

Database 从数据库中检索数字并将其作为搜索参数传递,database,Database,当有人在我的房地产网站中搜索特定城市时,我需要从.csv数据库中检索匹配的cityID,并将检索到的cityID作为搜索参数之一传递给我的供应商网站,如http//www.vendor.com?cityID=9999 cityID和cityName的列表位于名为myproperty.csv的文件中 下面是我现在拥有的当前html代码 <form action="vendor.com" method="get" target="_blank"> <label>Please

当有人在我的房地产网站中搜索特定城市时,我需要从.csv数据库中检索匹配的cityID,并将检索到的cityID作为搜索参数之一传递给我的供应商网站,如http//www.vendor.com?cityID=9999

cityID和cityName的列表位于名为myproperty.csv的文件中

下面是我现在拥有的当前html代码

<form action="vendor.com" method="get" target="_blank">
<label>Please enter city name:-</label>
<input type="text" id="cityName" name="cityName">

<!-- Please help me to add some codes here -->

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

请输入城市名称:-
有人能帮我完成上述目标吗? 我到处寻找答案,但仍然一无所获。请帮忙。提前感谢你的帮助


Hafiz

我假设您在支持PHP的web服务器上运行您的站点,不是吗?在这种情况下,这里有一个解决方案,它使用PHP逐行读取csv文件,并创建一个html下拉列表,供用户按名称选择城市

另外,如果您的服务器上没有运行PHP,也可以使用JavaScript执行相同的操作,如果您需要JavaScript而不是PHP,请告诉我。

这可能是最简单的解决方案,如果您想使用文本输入而不是下拉列表中的城市列表,可以通过实现javascript自动完成功能来获得更多信息。


请选择城市:

谢谢你的回答,佐尔坦。问题是,我的网站访问者会将城市名称输入文本框,而不是下拉菜单。你能帮我修改代码吗?谢谢你,伙计,我希望这会有帮助。
<form action="vendor.com" method="get" target="_blank">
Please select city: 

<?php
// Set filename to read from
$csvFile = 'dummy.csv';

// Open file handle
$fp = fopen($csvFile, "r");

// In case the file don't exist, exit
if (!is_resource($fp)){
    die("Cannot open $csvFile");
}
?>
<select name="city">
    <?php //Let's read the csvfile line by line: ?>
    <?php while($line = fgetcsv($fp)): ?>
        <?php // Assuming that the csvfile's structure is the following: city_id,city_name, we create the dropdown options: ?>
        <option value="<?php echo $line[0];?>"><?php echo $line[1]?></option>
    <?php endwhile; ?>
</select>

<input type="submit" value="Submit">
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<form action="vendor.com" method="get" target="_blank" id="myForm">
    Please enter city name:-
    <input type="hidden" id="cityId" name="cityId">
    <input type="text" id="cityName" name="cityName">

    <input type="submit" value="Submit">
</form>
<script>
        $(function(){
            //Ajax call to get csv file contents
            $.ajax({
            type: "GET",
            url: "dummy.csv",
            dataType: "text",
            success: function(data) {process_csv(data);}
        });
        
        //Event handler for form submission
        $('#myForm').submit(function(){
            //Let's get the value of the text input (city name)
            var city_name = $('#cityName').val().toLowerCase();
            
            //If the city name exists in our map, let's set the matched city id to the hidden input field called cityId
            if (city_ids[city_name] > 0)
            {
                $('#cityId').val(city_ids[city_name]);
                return true;
            }
            //Otherwise we can't find the city id in our database, so we can't post the form either...
            alert('No such city name in database!');
            return false;
        });
     
    });
    
    //global variable to contain map of city names and id's
    var city_ids = {};
    
    //Function to parse csv file and set city map
    function process_csv(text) {
        var lines = text.split(/\r\n|\n/);

        for (var i=0; i<lines.length; i++) {
            var data = lines[i].split(',');
            city_ids[data[1].toLowerCase()] = data[0];
        }
    }
</script>