Passing a search parameter through jquery

I have a form in which, if a user enters a search query, his parameter must be passed through jquery, and after receiving the results, he must load the results in a div container. since I am not very good at jquery, how would I do this?

HTML:

    //currently the data is being displayed on pageload:
    $(document).ready(function() {
    $("#bquote").load("quotes_in.php")
   });  

   $(".bsearch")
    .keydown(function() {
    //pass the parameter to the php page

       //display in div #bquote

    });


 <!-- Begin Search -->
        <div id="search" style="display:none;">
                <input type="text" class="bsearch" name="search" value="Search" />
        </div>
<!-- End Search -->

php [using OOP]:

if (isset($_POST["search"])) 
{
    $quotes->searchQuotes();
}

php class:

  $search = $_POST['search'];

  $sql = "SELECT * FROM thquotes WHERE cQuotes LIKE '%"
  .  mysql_real_escape_string($search) ."%' ORDER BY idQuotes DESC";


  try {

  $query = $this->_db->prepare($sql);
  $query->execute();

  if(!$query->rowCount()==0)
  {
   while($row = $query->fetch())
    {
    echo $this->formatSearch($row);
}
+3
source share
3 answers

I would do this:

$(".bsearch").keydown(function() {
  //create post data
  var postData = { 
    "bsearch" : $(this).val(), 
    "anotherParam" : "anotherValue" 
  };

  //make the call
  $.ajax({
    type: "POST",
    url: "yourAjaxUrl.php",
    data: postData, //send it along with your call
    success: function(response){
      alert(response); //see what comes out
      //fill your div with the response
      $("#bquote").html(response);
    }
  });
});

EDIT:

To host the bootloader you need to check this here:

http://api.jquery.com/category/ajax/global-ajax-event-handlers/

And to show the image of the bootloader, for example:

$("#loading").ajaxStart(function(){
   $(this).show();
});

And to hide it when the ajax call is completed:

$("#loading").ajaxComplete(function(){
   $(this).hide();
 });
+3
source

ajax...

$(".bsearch").keydown(function() {

    // Get the current input value
    var value = $(this).val(); 

    //pass the parameter to the php page
    $.ajax({
       type: "POST",
       url: "some.php", // replace this with the right URL
       data: "bsearch=" + value,
       success: function(msg){
          $("#search").html(msg);
       }
    });         
});

jQuery.ajax(), , jQuery.serialize()

+1

Instead of using $ _POST, you can use $ _GET or $ _REQUEST, as shown below:

var searchString = $(".bsearch").val();
$("#bquote").load("path_to_php_file.php?search="+searchString);

Then in PHP replace

$_POST

... with

$_GET
0
source

Source: https://habr.com/ru/post/1756654/


All Articles