开发者

ajax how to read out $_post variable

开发者 https://www.devze.com 2022-12-22 02:02 出处:网络
I am trying to filter/search a database with ajax $.ajax({ type: \"POST\", url: \"filterSearch.php\", queryString: qry,

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??


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.


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.


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


I rather like the serialize method.

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

  }

});


  $.ajax(
  {
  type: "POST",
  url: "test.php",
  data: {'test': test, 'name': 0, 'asdf': 'asdf'},

    success: function(html)
    {
    alert(html);
    }
  });

and

<?php  
echo "come here";
echo $_POST['runquery'];
?>

dir

$ tree
.
├── a.php
└── test.php
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号