简体   繁体   中英

Get customer info from database after selecting dropdown using php/ajax

i'm trying to make a simple webpage for making invoices. to select/load customer info i'm using a dropdown filled with all the customers in the database. after selecting a customer i want php to get all the values of that customer from the database and echo somewhere else on the page. thought it would be something simple but tried everything and cant seem to get it to work.

any help?

after deleting all the code that didn't work anyway this is what i'm left with:

<select name="selectCustomer">
    <option selected>Klantnaam</option>
    <?
        $result = mysql_query('SELECT * FROM '.$c_tbl_name);

        while($row = mysql_fetch_array($result)) {
            echo    '<option value="'.$row['c_id'].'">';
            echo        $row['c_name'];
            echo    '</option>';
        }
    ?>
</select>

You need some small AJAX function to do that:

jQuery(document).ready(function($){
    $('[name="selectCustomer"]').change(function(){
        $('#result').load('load_data_from_db.php', {
            customer : $(this).val();
        });
    });
});

You than need a script load_data_from_db.php which takes the selected customer, generates the content and returns it to the client where it would be placed into an element with the ID result.

Try

$result = mysql_query('SELECT * FROM '.$c_tbl_name) or die(mysql_error());

and this will give info if the query failed.

Furthermore, you should really consider upgrading from mysql() as it is now deprecated. Try google search for 'php PDO'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM