0

I created a simple searching function in php. Its working when im not using ajax to that script. But when i use ajax, my implode function disappear.

Here how my codes looks like.

$searchtag = filter_var($_GET['search'], FILTER_SANITIZE_STRING);
$searchtagrlce = str_replace("+", " ", $searchtag);
$searchTermsis = explode(' ', $searchtagrlce);

    $searchTermBitsis = array();
    foreach ($searchTermsis as $terms) {
        $terms = trim($terms);
        if (!empty($terms)) {
            $searchTermBitsis[] = "tbale_row1 LIKE '%$terms%' OR tbale_row12 LIKE '%$terms%' OR tbale_row13 LIKE '%$terms%'";

        }
    }




     $getdataquerystores = "SELECT `tbale_row1`, `tbale_row2`, `tbale_row3`, `tbale_row4`, `tbale_row5`, `tbale_row6` FROM `tablename` WHERE ".implode(' AND ', $searchTermBitsis). " ORDER BY tbale_row1 DESC LIMIT $limit, 10";
      $getdataquerystoress = mysqli_query($connection, $getdataquerystores);

when i echo or print the above codes without ajax, im getting this

SELECT `tbale_row1`, `tbale_row2`, `tbale_row3`, `tbale_row4`, `tbale_row5`, `tbale_row5` FROM `tablename` WHERE tbale_row1 LIKE '%a%' OR tbale_row2 LIKE '%a%' OR tbale_row3 LIKE '%a%' ORDER BY tbale_row1 DESC LIMIT 0, 10

but when i use ajax and print the above same code, im getting this (AFTER WHERE FUNCTION DATA HAS BEEN DISAPPEAR)

SELECT `tbale_row1`, `tbale_row2`, `tbale_row3`, `Logo_croped_554`, `tbale_row5, `tbale_row6` FROM `tablename` WHERE ORDER BY tbale_row1 DESC LIMIT 10, 10

here it is my ajax code

$(window).scroll(function ()
    {
   if($(document).height() <= $(window).scrollTop() + $(window).height())
   {
  loadmore();
   }
    });

    function loadmore()
    {
      var val = $("#row_no").val();
      $.ajax({
      type: 'post',
      url: '/myproject/data/alldata.php',
      data: {
       getresult:val
      },
   beforeSend:function(){
     $(".loading-data").fadeIn("slow");
   },
   
   uploadProgress:function(){
    
    $(".loading-data").fadeIn("slow");
   },
      success: function (response) {
   var content = document.getElementById("dsdplasdy_sdtres");
      content.innerHTML = content.innerHTML+response;
 
      
      document.getElementById("row_no").value = Number(val)+10;
   },
   complete:function() {
     $(".loading-data").fadeOut("slow");
    
   }
   
      });
    }

here it is the value im getting from tne hidden field

 <input type="hidden" id="row_no" value="10" />

Please any ex[ert may help me. Any help much appreciated. Thanks.

alex
  • 35
  • 8

2 Answers2

0

To pass arrays over the internet, or any other data for that matter, the data needs to be properly serialized and encoded on the senders end and un-serialized and decoded on the receivers end. There are many ways to serilize the data but the defaults that are supported by PHP directly are application/x-www-form-urlencoded or multipart/form-data which will populate the $_POST superglobal variable if the POST request method is used. The data can also be passed as URL parameters where the query portion of the URL is "application/x-www-form-urlencoded" encoded and populates the $_GET superglobal variable in PHP.

Note: Use $_POST or $_GET superglobals to get data. If you have register globals on you should turn it off, it is no longer supported as of version 5.4 http://php.net/manual/en/security.globals.php

For your specific case you are not passing the search parameter from your AJAX request.

url: '/myproject/data/alldata.php',

should be

url: '/myproject/data/alldata.php?search='+encodeURI(searchTerms),

You will need to populate the searchTerms variable with your search terms or replace searchTerms with your actual search terms. If you don't know how to get the search terms see How to retrieve GET parameters from javascript?

Community
  • 1
  • 1
Ralph Ritoch
  • 3,260
  • 27
  • 37
  • Hi @Ralpha, Im not filling any form or submitting any data. Im just retrieving data when the user scroll to the bottom of the page from where the last id to next few data. Im having this trouble only with that ~implode~ function. I have used another scripts with my ajax code and they are working . Any suggestions ? – alex Nov 30 '16 at 02:42
  • alex, I have reviewed your code and it is a javascript equivalent of a form with method post which means the data will show up in the superglobal $_POST['getresult'] , regardless your question is a duplicate. – Ralph Ritoch Nov 30 '16 at 02:45
0

Thank you everyone who answered above. I been trying to figure this. It wan't something with the ajax as i mentioned.I was about how php forget variables data when it is passing through ajax. There for i used hidden field to store the data and then i sent it to the sript later using ajax.

<input type = "hidden" value= "<?php search parameter ?>"
alex
  • 35
  • 8
  • This solution is horrible and will break if the user has different searches open in different tabs since cookies/sessions are the same between tabs. It would have been better to pass the search parameter as we suggested. – Ralph Ritoch Nov 30 '16 at 04:10
  • we can destroy it one the function success – alex Nov 30 '16 at 04:11
  • Sadly I've seen professional applications abuse the session in this way. You won't have the only web site that doesn't support multiple tabs BUT it is better to properly support multiple tabs/windows by not using the session for page/request specific data. The session is better for USER specific data so you can share data between windows/tabs. – Ralph Ritoch Nov 30 '16 at 04:15
  • @RalphRitoch i just updated my answer. What you think now ? – alex Nov 30 '16 at 04:28
  • alex, passing data from PHP to Javascript via hidden tags is reasonable BUT I believe W3C compliance will also require that these tags are in a form which doesn't make any fundamental sense but that form can be hidden. The most elegant way to pass data from PHP to Javascript is via the script attribute. A good example using JSON is here > http://stackoverflow.com/questions/7581133/how-can-i-read-a-json-in-the-script-tag-from-javascript – Ralph Ritoch Nov 30 '16 at 04:42
  • @alex You really should just use GET rather than POST for your AJAX request (as you are getting data) and then give it 2 pieces of data (2 properties of your data object): `getresult` and `search`. This way you don't need any hidden fields and you're not mixing GET and POST! – Nerdwood Nov 30 '16 at 06:04
  • I want to add certain parameters to my search script like only `tble_row78` = '0' thing , But all the time that statement becomming false when im searching using the script above – alex Nov 30 '16 at 09:10