Link to home
Start Free TrialLog in
Avatar of jej07
jej07

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of jej07
jej07

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ioannis Paraskevopoulos
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
Avatar of jej07
jej07

ASKER

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.
Mo problem at all. I just saw this as neglected question and thought i could help a bit.