Search code examples
javascriptphpmysqlsearchdata-transfer

What is the right way for Searching functions in a Website with Javascript?


Its known that interactions between Javascript and SQL-Databases are not very secure. But most Websites use it cause the Webside doesent reload to show matches in a search.

With PHP it isn't possible to change Page-Contents without a completely Page-Refreshing.

Witch is the right way to get Data from SQL with Javascript without security-neglects. Aspeccialy for a Searching function with directly matches in a list.


Solution

  • You can use 2 way to get data from db by using js;

    1. Ajax:

    function refresh() {
        $.ajax({
            url:"your url",
            method: "GET",
            data: your_params,
            success: function(response) {
                $("#specific_div_id").html(response);
            }
        });
    }
    

    You can do this within an interval like;

    setInterval(refresh, 5000); 
    

    in order to get content in every 5 sec.

    2. Websockets

    In AJAX, you are requesting in every 5 secs to get updated content from server. Think that, you are not getting content server pushes updated content to you. In other words, server notifies you on any updated data. You can have a look at Socket.io for an example implementation of websockets. When server notifies you, you can take data and put it related html area