Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6390151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:29:30+00:00 2026-05-25T03:29:30+00:00

I have MySQL tables looking like this: regions table id | region ——————- 1

  • 0

I have MySQL tables looking like this:

regions table

id     |   region
-------------------
1      |   Region1
2      |   Region2

…

and schools table

region_id |   school
-------------------
1         |   schno1
1         |   schno5
1         |   schno6
2         |   scho120 

My page works like this: At first, page populates #regions select menu from db table named "regions". when user selects #region, the JS sends selected region’s value to search.php. Server-side PHP script searches db table named "schools" for #region (previously selected menu) value, finds all matches and echoes them.

Now the question is, how can I hide #class and #school select menus, and show only error message "there is no school found in this region" if no matches are found? How to check if there’s no result from search.php? I’m a beginner to JS.

My JavaScript looks like this: http://pastie.org/2444922 and the piece of code from form: http://pastie.org/2444929 and finally search.php: http://pastie.org/2444933

Update

I changed my JS but no success.

$(document).ready(function(){
    $("#school").hide();
    $("#class").hide();
searchSchool = function(regionSelect){
var selectedRegion = $("select[name*='"+regionSelect.name+"'] option:selected").val();
if (selectedRegion!='0'){
    $.ajax({
    type: "POST",
    url : "core/code/includes/search.php",
    data: "&region_id="+selectedRegion,
    success: function(result, status, xResponse){
        if (result!=null){
            $("#school").show();
            $("#class").show();
            $("#school").html(result);
        }else{
            $("#error").html("There is no school found in this region");
            $("#school").html('');
            $("#school").hide();
        }
    },
    error: function(e){
        alert(e);
    }
    });
}else{
    $("#error").html('Please select a region first');
    $("#school").html('');        
    $("#school").hide();
    $("#class").hide();
}
}
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:29:31+00:00Added an answer on May 25, 2026 at 3:29 am

    You could try this

    index.php :

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <title>Ajax With Jquery</title>
    
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript">
        searchSchool = function(regionSelect){
        var selectedRegion = $("select[name*='"+regionSelect.name+"'] option:selected").val();
        if (selectedRegion!='0'){
            $.ajax({
                type: "POST",
                url : "search.php",
                data: "&region_id="+selectedRegion,
                success: function(result, status, xResponse){
                alert(result);
                if (result!=''){
                        $("#school").show();
                        $("#school").html(result);
                    }else{
                        $("#error").html("There is no school found in this region");
                        $("#school").html('');
                        $("#school").hide();
                    }
                },
                error: function(e){
                    alert(e);
                }
            });
        }else{
            $("#error").html('Please select a region first');
            $("#school").html('');        
            $("#school").hide();
        }
    }
    </script>
    </head>
    
    
    <body>
    
        <?php 
    $username="root";
    $password="";
    $database="test";
    
    mysql_connect('localhost',$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM regions";
    $result=mysql_query($query);
    
    $num=mysql_numrows($result);
    
    mysql_close();
    
    echo "<b><center>Database Output</center></b><br><br>";
    
    ?>
    <select name="region" id="region" onchange="searchSchool(this)">
    <option value="0">Please select a Region</option>
    <?php 
    while($data = mysql_fetch_array( $result )) 
    { 
    ?>
    <option value="<?php echo $data['id']?>"><?php echo $data['name']?></option>
    <?php 
    }
    ?>
    </select>
    
    <select name="school" id="school"></select>
    
    <span id="error"></span>
    
    </body>
    </html>
    

    Search.php:

    <?php 
    
    $username="root";
    $password="";
    $database="test";
    
    mysql_connect('localhost',$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    
    if(isset($_POST['region_id'])) {
    $query = "SELECT * FROM schools WHERE region_id='".$_POST['region_id']."'";
    $result=mysql_query($query);
    
    $num = mysql_numrows($result);
    
    if ($num>0){
        while ($row = mysql_fetch_array($result)) {
            echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
        }
        }
    else{
    return null;
    }
    }
    
    
    mysql_close();
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table looking like this: > describe books; +---------------+--------------+------+-----+---------+----------------+ | Field
I have a MySQL table with a structure like the following: I'm looking for
I have two MySQL tables something like this: tool_owners: tool_owners_id | user_id | ...
I have a mysql table that looks like this: id | uid | title
I have two tables. First table looks like this: Table: Records rid | user
I have two MySQL tables, j and t, with a third normalising table, jt.
I have this mysql tables I want to display with jqgrid. The problem appears
I am looking for some help with a MYSQL query. I have two tables,
I have two tables, mytableA and mytableB. MytableA looks like this ColumnA|Columnb Apple|fruitstand Pear|fruitstand
I've got a problem with my SQL. I have a table (named Regions) looking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.