Q:

How to show message or data only one time while select multiple checkbox in jquery?

code:
<script>
    $(document).ready(function(){
        $(".choose").click(function(){
            job_type = $(':checked').map(function() { 
                            return this.value; 
                        }).get().join(',');
            category = $("#cat_gory").val();
            $.ajax({
                type:"POST",
                data:{"job_type":job_type,"category":category},
                url:"type-jobs.php",
                success:function(data){
                    $(".success").html(data);
                }
            });
        });
    });
</script>
    <input type="hidden" name="cat_gory" id="cat_gory" value="<?php echo $job_category; ?>" />  
<p class="flchek">
    <input type="checkbox" name="choosetype" value="freelance" class="choose" id="33r">
    <label for="33r">Freelance</label>
</p>
<p class="ftchek">
    <input type="checkbox" name="choosetype" value="full time" class="choose" id="dsf">
    <label for="dsf">Full Time</label>
</p>
<p class="ischek">
    <input type="checkbox" name="choosetype" value="internship" class="choose" id="sdd">
    <label for="sdd">Internship</label>
</p>
<div class="success"></div>

type-jobs.php
<?php
    include("config.php");
    $job_category = mysqli_real_escape_string($con,$_POST['category']);
    $job_type = mysqli_real_escape_string($con,$_POST['job_type']);
    $tag = explode(",",$job_type);
    foreach($tag as $type)
    {
        $sql_job = mysqli_query($con,"select p.job_title,c.state,c.city from company c inner join jobs p on p.company_name = c.company_name where p.category = '".$job_category."' and job_type='".$type."'");

        $result_job = mysqli_num_rows($sql_job);
        if($result_job > 0)
        {
            while($row_jobs = mysqli_fetch_array($sql_job))
            {
                echo '<h3>'.$row_jobs['job_title'].'</h3>';
            }
        }
        else
        {
            echo '<p id="c_mm">record not found</p>';
        }
    }
?>

In this code I have multiple checkbox. Now, When I click on checkbox to get data through jquery/ajax it return data to me but the problem is if I click on freelance checkbox it show me record not found again when I click on full time then it showing record not found as well as jobs. Now, I want if data was not found in my database it will show single message to me and if data found then hide message and show database record. So, How can I do this? Please help me.
Thank You   

A:

If you want to show record not found message only if you don't find any record then you can use a flag in php something like this:
<?php
include("config.php");
$job_category = mysqli_real_escape_string($con,$_POST['category']);
$job_type = mysqli_real_escape_string($con,$_POST['job_type']);
$found = 0;
$message = '';
$tag = explode(",",$job_type);
foreach($tag as $type)
{
    $sql_job = mysqli_query($con,"select p.job_title,c.state,c.city from company c inner join jobs p on p.company_name = c.company_name where p.category = '".$job_category."' and job_type='".$type."'");

    $result_job = mysqli_num_rows($sql_job);
    if($result_job > 0)
    {
        $found = 1;
        while($row_jobs = mysqli_fetch_array($sql_job))
        {
            $message .= '<h3>'.$row_jobs['job_title'].'</h3>';
        }
    }
}

if(found == 0) {
    $message = '<p id="c_mm">record not found</p>';
}
echo message;
?>

Notice $found flag here in code. HTH.

