0

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===
1

1 Answer 1

1

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);
    });

});
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help I found this error on console Uncaught SyntaxError: missing ) after argument list
i have updated the answer with error fix. Check it again. if still fail, then you need some debugging in browser console and let me know which line of the code gives this error. Or type here URL where i can see the error.
response return null value . Also I try post instead of get
okay, then it is possible that your SQL query is wrong. (don't try post, use get, because my code is designed for "get"). Try to print raw sql query with this code: echo $wpdb->prepare("SELECT br_name FROM $tablename WHERE br_code = %s",$br_code); and make sure that the query is correct.
I have updated your answer and change the $wpdb query and its working now thanks for the help man.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.