Help needed with jQuery remote validation

This is new territory for me. I have a form where I need to check the selected value of a dropdown against a MySQL table. I'm looking to see if a room is available, if it's not, I will need to display a message. My problem is, the alerts show me that this only returns 0.

Any help would be greatly appreciated.

Form:
<select id="el_31"> 
<option selected="selected" value=""></option>
<option value="1">Standard</option>
<option  value="2">Executive</option>
<option  value="3">Suite</option>
</select>

Open in new window


PHP function/file (validate-form.php):
    
function check_availability($roomNeeded) {
        $result = mysqli_query($db_connection, "SELECT * FROM table
            WHERE option_value='{$roomNeeded}' LIMIT 1");

        $roomCheck = mysqli_fetch_assoc($result);
    
        if( $roomCheck['total'] > $roomCheck['booked'] ) {
             return true;
        } else { return false; }
    }
    if(isset($_POST['selected'])):
    
        $selectedRoom = intval( $_POST['selected'] );
    
        if(check_availability($selectedRoom)) {
          echo json_encode(array("msg" => "Available" , "result"=>1));
        
        } else {
        
          echo json_encode(array("msg" => "Not Available" , "result"=>0));
        } 
    
    endif;    

Open in new window


My thought is the script would need to be something like this, but seems to only return 0:
        $('#el_31').change(function () {
            $.ajax({
                url: 'validate-room.php',
                type: 'POST',
                dataType: 'json',
                data: {
                    'selected' : $(this).val()
                },
                success: function(data){
                     if (data.result == 1) {
                         alert(data.msg);
                     }
                     else{
                         alert(data.msg);
                         $('#li_31').addClass('error');
                     }
                }
            });
            
        });
    }); 

Open in new window

LVL 1
jej07Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jej07Author Commented:
I wasn't able to get this method to work, so I ended up checking the room availability via php, after the form is posted.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ioannis ParaskevopoulosCommented:
Hi,

I know that you say you did it another way, but i would like to give a little help. I think the best way for you to search what is wrong would be in the success callback to log what is returned:

success: function(data){
    console.log(data);
    if (data.result == 1) {
        alert(data.msg);
    } else {
        alert(data.msg);
        $('#li_31').addClass('error');
    }
}

Open in new window


On your browser hit F12 while on your page and then go to the console section. Try posting your request and see what is written in the log, so you have an idea of what is returned. You will also be able to check what was the value that you posted in the console.

I would also check what thes sql query returns for those values. I do not know your case, but it seems that you are getting just one of the rooms of lets say 'Standard' type. You are not trying to filter the rooms that the total is greater than the booked. You just get the first and check it. What if the first is full, then each time you check it it will still be booked.

What if your sql was something like :
SELECT * FROM table WHERE option_value='{$roomNeeded}' AND total > booked LIMIT 1

Open in new window


Once again, i am not aware of your scenario, so this query might not make ant sense.

Giannis
0
jej07Author Commented:
Thank you very much for your help and pointing me in the right direction.
This was a time sensitive project, which is why I went another direction. If I could, I would gladly award the points to you. I followed your advice, and it turns out that the problem was within the PHP.
Thanks again.
0
Ioannis ParaskevopoulosCommented:
Mo problem at all. I just saw this as neglected question and thought i could help a bit.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.