MySQL database populated dropdown box and PHP search

怎甘沉沦 提交于 2020-01-06 12:30:10

问题


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


回答1:


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.




回答2:


Create a form with a search input textbox.

Then on the page where the form submits (I like to post back to the same page) you need to execute a select query to get the list of the occupations based on that search.

example: in index.php

<?php

?>
<html>
<body>
<form action="<?php echo htmlentities($PHP_SELF); ?>"
  <?php
    if (isset($_POST['submit'])){
      // Insert code to connect to DB HERE
      $searchText = htmlentities($_POST['search']);
      $query = "SELECT * FROM table_name WHERE Occupation LIKE '%$searchText%'";
      $results = mysql_query($query) or die(mysql_query());
      echo "<select name=\"occupations\">";
      while($row=mysql_fetch_array($results)){
        echo "<option>$row[Occupation];</option>";
      }
      echo "</select>";
    }
  ?>
  <input type="text" name="search"/>
  <input type="submit" name="submit"/>
</form>
</body>
</html>


来源:https://stackoverflow.com/questions/12254915/mysql-database-populated-dropdown-box-and-php-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!