0

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

4
  • Your question is not clear. What are you searching and what do you want to display on the website ? Commented Sep 3, 2012 at 23:23
  • 1
    There are plenty of good tutorials, videos, ebooks, books etc. that can guide you through this, use google. SO is not the best place to be asking for code examples of this kind. Commented Sep 3, 2012 at 23:24
  • I have a people list with their occupations and other info stored in database. Now i need to have dropdown box populated from database with people occupations implemented in search for, so when i choose from dropdown box for example doctor and click SEARCH that will show all doctors from database wit data about them. Is it clearer now? Commented Sep 3, 2012 at 23:26
  • 1
    David, last 2 days i search on google and found NOTHING, i found multiform, css dropdown, php dropdown with ajax but nothing similar to my needs. You dont help with your attitude, if you dont want answer on my question or dont know answer on my question, other people will help me Commented Sep 3, 2012 at 23:29

1 Answer 1

3

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'];
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $con = new mysqli('server', 'user', 'password', 'database');
    $sql = "SELECT * FROM tableName WHERE occupation = ?";
    $stmt = $con->prepare($sql);
    $stmt->bind_param('s', $searchTxt);
    $stmt->execute();
    $result = $stmt->get_result();
    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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.