-1

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 :D

  • Just to add context, do you have errors enabled ? are you sure your file has no errors in it ? ( Database wise ). – Thenad Jun 29 '20 at 09:53
  • Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Jun 29 '20 at 09:54
  • 1
    your code is **vulnerable** to **sql injection** use always **prepared statements with parameters** see https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – nbk Jun 29 '20 at 09:55
  • Hi @NadirBaoun, thank you for your respond. We forgot to attach the errors, we will edit this post to ensure clearer understanding of our problem. In short, yes there is error but I still not have any clue where to go next. – Oka Mahendra Jun 29 '20 at 10:00
  • Thanks @RiggsFolly, i will check your suggestions, and sorry that this code is not a perfect example on the standard since I learn programming as self-taught. I will try my best to learn and comply one step at a time – Oka Mahendra Jun 29 '20 at 10:07
  • hi @nbk, thank you so much on your suggestion, i will look at it and apply to the code shortly :D – Oka Mahendra Jun 29 '20 at 10:07
  • Those errors while serious, dont appear to be related to the code you show. – RiggsFolly Jun 29 '20 at 10:08
  • @NadirBaoun, as I check with phpmyadmin, the code is good to go and yield good results too. – Oka Mahendra Jun 29 '20 at 10:09
  • yes @RiggsFolly, i also try to find the relation with our suspect. Do you have anything in mind that can help to find the possible errors? Also, please kindly review my additional image about error, hope this can enhance give clearer situation. – Oka Mahendra Jun 29 '20 at 10:12
  • _Small Point_ There is no point preparing a query that you have already concatenated a SQLInjection vector into. Use parameterised and bound queries – RiggsFolly Jun 29 '20 at 10:18
  • Add [error reporting](http://stackoverflow.com/questions/845021/) to the top of your file(s) _while testing_ right after your opening PHP tag for example. Even if you are developing on a server configured as LIVE you will now see any errors. ` – RiggsFolly Jun 29 '20 at 10:18
  • we already include error reporting and display errors, but nothing shows up sir. – Oka Mahendra Jun 29 '20 at 10:31

2 Answers2

0

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

Thenad
  • 155
  • 2
  • 11
  • Dear mr Nadir, thank you for your suggestion. After change the position of jquery, i found that the AJAX now able to do the update to my index.php. Is there anything that i might have missed about the ordering of the link sir? – Oka Mahendra Jul 01 '20 at 21:43
0

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 :)