Reputation: 3174
I am trying to make a dynamic form when some field will automatic depends on user input. for example.
mySql Data row
+-------+----------------+
|br_code| br_name |
+-------+----------------+
| 1057 | BANANI |
+-------+----------------+
| 1065 | BANANI BAZAR |
+-------+----------------+
| 1073 | PRIME OFFICE |
+-------+----------------+
I have two input field like this
<input type="number" name="br_code" id="br_code" placeholder="Branch Code"/>
<input type="text" name="br_name" id="br_name" disabled/>
so if I input the br_code
how can I populate br_name
on other field.
to achieve this What I try
jQuery
jQuery('#br_code').change(function($) {
$('#br_name').attr('disabled', true);
$('#br_name').val("<?php echo($br_name);?>")
});
PHP
<?php
global $wpdb;
$tablename = $wpdb->prefix."sbl_employee";
//getting branch name from database
$br_name = $wpdb->get_row( "SELECT br_name FROM $tablename WHERE br_code = '$br_code'" );
====Need help to call Wordpress Ajax call===
Upvotes: 0
Views: 1756
Reputation: 3572
Add this to your theme's functions.php
add_action( 'wp_ajax_brnamereturner', 'brnamereturner' );
add_action( 'wp_ajax_nopriv_brnamereturner', 'brnamereturner' );
function brnamereturner() {
global $wpdb;
if(empty($_GET["br_code"])) return;
$br_code=intval( $_GET['br_code'] );
$tablename = $wpdb->prefix."sbl_br_name";
$rows = $wpdb->get_results("SELECT br_name from $tablename WHERE br_code = $br_code");
foreach ($rows as $row) {
echo $row->br_name;
}
wp_die();
}
In your jQuery, use this:
jQuery('#br_code').change(function($) {
$('#br_name').attr('disabled', true);
var data = {
'action': 'brnamereturner',
'br_code': jQuery('#br_code').val()
};
jQuery.get("<?php echo admin_url( 'admin-ajax.php' );?>", data, function(response) {
$('#br_name').val(response);
});
});
Upvotes: 1