MySQL数据库填充下拉框和PHP搜索 [英] MySQL database populated dropdown box and PHP search

查看:186
本文介绍了MySQL数据库填充下拉框和PHP搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的表格中包含字段:

pre>
ID
名字
姓氏
公司名称
占有
说明

现在我需要从数据库(字段占领)和文本框填充搜索表单,我可以放置任何我想要的内容,然后从基于网页的数据库获得结果。



我真的很抱歉,但我完全是begginer,只需要一些这样的代码的例子,很多帮助:)



谢谢

解决方案

使用AJAX从您的页面调用php脚本,然后使用php脚本查询数据库并将结果回显到页面。



我要去在这个例子中使用jQuery是因为它可以节省很多行,如果你还没有的话,你应该检查它。

 < HTML> 
< head>
< script src =// ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js\"> ;</script>
< script type =text / javascript>
函数searchOccupation(){
$ .ajax({
url:searchOccupation.php?search =+ $('#searchTxt')。attr('value'),
success:function(data){
alert(data);
}
});
}
< / script>
< / head>
< body>
< input type =textid =searchTxt>
< input type =buttonvalue =Searchid =searchBtnonclick =searchOccupation()>
< / body>



然后你的php脚本名称应该与ajax调用的url字段中的名称匹配(在这种情况下,它应该被命名为searchOccupation.php)将如下所示:

 <?php 
$ searchTxt = $ _GET ['search'];
$ con = new mysqli('server','user','password','database ');
if(!$ con){die(failed to connect:。$ con> connect_error;)}
$ sql =SELECT * FROM tableName WHERE occupy ='。 $ searchTxt。';
$ result = $ con> query($ sql);
if(!$ result){die(No result set);}
while($ row = $ result-> fetch_assoc()){
echo $ row ['firstName']; //这将数据发送回页面
}
?>

php脚本的echo部分是将数据发送回success:function(data)的部分的JavaScript,所以回声无论你想在页面上的领域如上。



编辑:稍微误解了你的意思,ajon的上面可能是你需要的。


I have question regarding search on webpage with textbox and dropdown box.

I have table with fields:

ID
First name
Last name
Company name
Occupation
Description

Now i need to make search form which will be populated from database (field Occupation) and textbox where I can put whatever I want, and then get results from database based on those on web page.

I am really sorry but i am totally begginer and only need some examples of such kind of code and much help :)

Thank you

解决方案

You're going to want to use AJAX to call a php script from your page and then use the php script to query your database and to echo the results back to the page.

I'm going to use jQuery for this example because it saves a lot of lines, you should check it out if you haven't already.

<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
    <script type="text/javascript">
        function searchOccupation () {
            $.ajax({
                url: "searchOccupation.php?search=" + $('#searchTxt').attr('value'),
                success: function (data) {
                    alert(data);
                }
            });
        }
</script>
</head>
<body>
    <input type="text" id="searchTxt">
    <input type="button" value="Search" id="searchBtn" onclick="searchOccupation()">
</body>

Then your php script (whose name should match that in the "url" field of the ajax call (in this case it should be named "searchOccupation.php") will look like this:

<?php
    $searchTxt = $_GET['search'];
    $con = new mysqli('server', 'user', 'password', 'database');
    if (!$con) {die("failed to connect: " . $con->connect_error;)}
    $sql = "SELECT * FROM tableName WHERE occupation = '" . $searchTxt . "'";
    $result = $con->query($sql);
    if (!$result) {die("No result set");}
    while($row = $result->fetch_assoc()) {
        echo $row['firstName'];  //This sends data back to the page 
    } 
?>

The echo part of the php script is what sends data back into the "success: function (data)" of the javascript, so echo whichever field you want on the page as above.

Edit: Slightly misunderstood what you meant, ajon's above is probably what you need.

这篇关于MySQL数据库填充下拉框和PHP搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆