tags:

views:

91

answers:

4

Hi,

I am trying to filter/search a database with ajax

$.ajax({
   type: "POST",
   url: "filterSearch.php",
   queryString: qry,
   success: function(data){
     alert( "Data Saved: " + data );
     $('#searchResult').html(data); // Fill the search results box

   }
 });

Now in filterSearch.php i have the following test codes

if(isset($_POST['queryString'])) {
    echo "TEST";
}
if($_POST['runquery']==1) {

$sql = "SELECT * FROM fs_vacatures WHERE here-the-like-query?";
$msg =  $sql;
echo $msg;
die();
}

die();

But nor TEST or the $sql is return in the alert??

A: 

Your $.ajax call should look like this:

$.ajax({
   type: "POST",
   url: "filterSearch.php",
   data: {queryString: qry},
   success: function(data){
     alert( "Data Saved: " + data );
     $('#searchResult').html(data); // Fill the search results box

   }
 });

That is, you will need to pass the parameter names and values using the data option. In your example, you have invented a new option called queryString which just won't fly with jQuery.

karim79
A: 

Read the documentation about .ajax(). There is no parameter queryString, you have to use data.

It should look something like this then:

data: {'queryString': qry, 'runquery': 1}

Update:

Ok, either you use POST this way:

$.ajax({
   type: "POST",
   url: "filterSearch.php",
   data: {'runquery': 1, 'name': 'sdaf', 'asdf': 'asdf'}
   //...
 });

and then you have access to the parameters with $_POST['runquery'], $_POST['name'], etc.

Or you use GET:

$.ajax({
   type: "GET",
   url: "filterSearch.php" + qry, // which results in 'filterSearch.php?runquery=1&name=sdaf&asfd=asd'
   // ...
 });

and access the parameters via $_GET['runquery'], $_GET['name'], etc.

Felix Kling
tx, to all i mixed up two things. $.post vs $.ajax and data: vs queryString: going to test it all
alex
alex
@alex: See my updated answer.
Felix Kling
@felix, awesome thxhmm i have another working solution, I removed the ? and now qry="runquery=1type: "POST", url: "filterSearch.php", data: qry, success: function(msg){and it seems to work nicely
alex
@alex: Yes this should also work according to the documentation: *data: Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests.* It is worth reading ;) Btw you should accept those answers that helped you the most for your questions.
Felix Kling
A: 

Hello,

if you are using jQuery try to use "post" where you can add the post parameters as key-value-pairs:

$.post("filterSearch.php", { "queryString": "theValueOfQueryString", "runquery" : 1 },
   function(data){
     alert( "Data Saved: " + data );
     $('#searchResult').html(data); // Fill the search results box
   });

see jquery for more Information

Bernhard Kircher
A: 

I rather like the serialize method.

$.ajax({url: "filterSearch.php", type: "POST", data: $("form").serialize(), 
  success: function(data){

  }

});
aland