-2

How can i get the selected value from nationality_id to check query in book_id query. So I can find where is the country.

My selection got Nationality, Book and country. I want user to select nationality display book. And user select Book display for country. But my country will base on Nationality and book for display the data.

Appreciate anyone if can help me. My SQL got the result from what i need, just need help in correct the script

PHP script : nationality id

if(!empty($_POST['nationality_id'])) {


    $query = "SELECT nationality.nationalities, book_type.book ";
    $query .= "FROM info ";
    $query .= "JOIN nationality ON nationality.id = info.nationality_id ";
    $query .= "JOIN book_type ON book_type.id = info.book_type_id ";
    $query .= "WHERE nationality.nationalities LIKE '{$_POST['nationality_id']}' ";
    $query .= "GROUP BY book_type.book ";
    $query .= "ORDER BY book_type.book DESC ";
    $check_nationality = mysqli_query($db_connection, $query); 

    // Generate HTML of nationality options list 
    if($check_nationality->num_rows > 0){ 
        echo '<option value="">Please Select Your Book</option>'; 
        while($row = $check_nationality->fetch_assoc()){  
            echo '<option value="'.$row['book'].'">'.$row['book'].'</option>'; 
        } 
    }


}

For display book without any problem : When i try to key in:

AND nationality.natioalities LIKE '{$_POST['nationality_id']}

My selection function did not work any more.

PHP script : book id

 if(!empty($_POST['book_id'])) {

    $query = "SELECT nationality.nationalities, book_type.book, region.region, country.country ";
    $query .= "FROM info ";
    $query .= "JOIN nationality ON nationality.id = info.nationality_id ";
    $query .= "JOIN book_type ON book_type.id = book.book_type_id ";
    $query .= "JOIN region ON region.id = info.region_id ";
    $query .= "JOIN country ON country.region_id = region.id ";
    $query .= "WHERE book_type.book LIKE '{$_POST['book_id']} AND nationality.natioalities LIKE '{$_POST['nationality_id']}' ";
    $query .= "GROUP BY country.country ";
    $query .= "ORDER BY country.country ASC ";

    $check_country = mysqli_query($db_connection, $query); 

    // Generate HTML of country options list 
    if($check_country->num_rows > 0){ 

        echo '<option value="">Please Select Your Country</option>'; 
        while($row = $check_country->fetch_assoc()){  
            $country = ucwords(strtolower($row['country']));
            echo '<option value="'.$country.'">'.$country.'</option>'; 
        } 
    } 
}

JQuery script :

$(document).ready(function(){
    $('#nationality').on('change', function(){
        var nationalityID = $(this).val();
        if(nationalityID){
            $.ajax({
                type:'POST',
                url:'ajaxData.php',
                data:'nationality_id='+nationalityID,
                success:function(html){
                    $('#book').html(html);
                    $('#Country').html('<option value="">Please Select Nationality first</option>'); 
                }
            }); 
        }else{
            $('#book').html('<option value="">Please Select Nationality first</option>');
            $('#country').html('<option value="">Please Select Book first</option>'); 
        }
    });

    $('#book').on('change', function(){
        var bookID = $(this).val();
        if(bookID){
            $.ajax({
                type:'POST',
                url:'ajaxData.php',
                data:'book_id='+bookID ,
                success:function(html){
                    $('#country').html(html);
                }
            }); 
        }else{
            $('#country').html('<option value="">Please Select book first</option>'); 
        }
    });
});

1 Answers1

0

i think the problem with your question has too much details ;

i am guessing that this generalized form is what you need :

How to access a php variable in external php file using JavaScript?

if that is right I can edit my answer to better answer your question.

Winston L
  • 53
  • 6
  • Thank for your answer, so mean i may need to use JS for passing data ? – jimmy chin Oct 18 '19 at 08:40
  • i assume you use javascript for passing data because you use jQuery which is useful javascript library with ajax ; also note that the dollar sign in php script is not same as dollar sign in jQuery script ; – Winston L Oct 18 '19 at 09:02
  • Because from my JQuery, nationality_id it working when passing data to $_POST['nationality_id']. When i want to pass the nationality_id inside the $_POST['book_id'] it give me an error in my sql statement. – jimmy chin Oct 18 '19 at 09:50
  • 1
    I found the answer after i add in var dataID = $("#nationality option:selected").attr('data-id'); data:'book_id='+bookID +'&data_id='+dataID, Thank for your help too. – jimmy chin Oct 18 '19 at 17:01