skip to Main Content

We are on progress creating web page for live search and update file in bulk using JQUERY AJAX method. The files consist of index.php (for display to user and Javascript), and multiple_update.php (for fetch and update file in bulk). Initial reference we got is from here from webslesson website, but it does not have any reference for searching the record, hence we search for help for our journey.

Below is our index.php file

<div class="content-wrapper">
    <div class="content-heading">
        <div>STO Monitoring<small>Tables, one step forward.</small></div>
        <div class="ml-auto"><input type="text" id="search" placeholder="Search" /></div>
        <div id="display"></div>
        <div class="ml-auto">
            <button class="btn btn-primary btn-lg" type="button" data-toggle="modal" data-target="#myModalSmall"><i class="fas fa-plus-square"></i> Add Record</button>
        </div>
    </div>
    <form method="post" id="update_form">
        <div class="card">
            <div class="card-body">
                <table class="display" id="example" style="width:100%">
                    <thead>
                       <tr>
                          <th width="3%"></th>
                          <th width="5%">No</th>
                          <th width="15%">STO</th>
                          <th width="20%">PN</th>
                          <th width="8%">Qty</th>
                          <th width="10%">From</th>
                          <th width="10%">Dest</th>
                          <th width="15%">Status</th>
                          <th width="14%">Remark</th>
                       </tr>
                    </thead>
                    <tbody></tbody>
                 </table>
                 <div align="left">
                    <input type="submit" name="multiple_update" id="multiple_update" class="btn btn-info" value="Multiple Update" />
                </div>
              </div>
           </div>
        </form>
    </div>
 .....

Below is script inside our index.php, the one we suspect need troubleshoot at the moment.

<script>
function fill(Value) {
    $('#search').val(Value);
    if (name == "") {
        $("#display").html("");
    }
}

$(document).ready(function(){  

    function fetch_data()
    {
        $("#search").keyup(function() {
            var name = $('#search').val();
            if (name == "") {
                //Assigning empty value to "display" div in "search.php" file.
                $("#display").html("empty");
            } else {
                $.ajax({
                    url:"multiple_select.php",
                    method:"POST",
                    dataType:"json",
                    data: {
                       //Assigning value of "name" into "search" variable.
                       search: name
                    },
                    success:function(data)
                    {
                        var html = '';
                        for(var count = 0; count < data.length; count++) {
                            html += '<tr>';
                            html += '<td><input type="checkbox" id="'+data[count].num+'" data-num="'+data[count].num+'" data-sto="'+data[count].sto+'" data-pn="'+data[count].pn+'" data-qty="'+data[count].qty+'" data-supplant="'+data[count].supplant+'" data-dest="'+data[count].dest+'" data-stat="'+data[count].stat+'" data-remark="'+data[count].remark+'" class="check_box"  /></td>';
                            html += '<td>'+(count+1)+'</td>';
                            html += '<td>'+data[count].sto+'</td>';
                            html += '<td>'+data[count].pn+'</td>';
                            html += '<td>'+data[count].qty+'</td>';
                            html += '<td><span class="btn btn-oval btn-primary">'+data[count].supplant+'</span></td>';
                            html += '<td><span class="btn btn-oval btn-warning">'+data[count].dest+'</span></td>';
                            html += '<td>'+data[count].stat+'</td>';
                            html += '<td>'+data[count].remark+'</td></tr>';
                        }
                        $('tbody').html(html);
                    }
                });
            }
        });
    }

fetch_data();

$(document).on('click', '.check_box', function(){
 .....
</script>

We modify the AJAX to see if the input can be catched by the network, below is code for multiple_update.php

<?php

include('multiple_connection.php');
$name = $_POST['search'];
echo $name;

$query = "SELECT * FROM matreq_list, sto_list WHERE matreq_list.sto = sto_list.sto AND sto_list.sto LIKE '%$name%' LIMIT 5;

$statement = $connect->prepare($query);

if($statement->execute()) {
    while($row = $statement->fetch(PDO::FETCH_ASSOC)) {
        $data[] = $row;
    }
    echo json_encode($data);
}
?>

We want to make every search is captured via AJAX, and respond from network will be live-reflected in our index file. Below is our expected final result (this result is without "LIKE" in mysql statement to show the result only) :

Expected Result

And we confirm AJAX can handle our input, below is the image :

AJAX capture and respond

–UPDATE– Below is error messages :
Error messages

enter image description here

However, after we fired the input, nothing cames up in our index.php file. Network shows good respond, but the HTML is not responding the way we expected it to do. Please kindly advise us sir, what is wrong with our method and what should we fix?

Thank you and appreciate your kind help in our case

=====UPDATE=====

2020-07-02 : As mentioned by mr Nadir Baoun, tried to change the order of jquery.js and put it above the bootstrap.js, and somehow my table now able to search some part or whole part of the data.

Before :

.....
 <script src="vendor/datatables.net/js/jquery.dataTables.js"></script>
 <script src="vendor/datatables.net-bs4/js/dataTables.bootstrap4.js"></script>
 <script src="vendor/datatables.net-responsive-bs/js/responsive.bootstrap.js"></script>
 <script src="vendor/jquery/dist/jquery3.5.1.js"></script>
 <script src="vendor/datatables.net/dist/js/jquery.dataTables.min.js"></script>

After ordered : I move the jquery to the top of all codes.

and below is network screenshot :

After change order of javascript

Surprisingly, this work well 😀

2

Answers


  1. Chosen as BEST ANSWER

    After thoroughly reading on various articles with a guide from Mr Nadir Baoun, my problem is now fixed by changing the order of the script, putting the jquery script before the bootstrap script.

    Similar answers also posted in stackoverflow website : script order for jquery with bootstrap

    Thank you :)


  2. The HTML isnt responding the way you want is because you have JavaScript errors thus your response code wont function accordingly.
    First thing include your jquery file before bootstrap.
    This should solve the " cannot read property fn of undefined " error.
    Please update your post with debug messages in the success param of your ajax request after doing what i have mentionned above

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search