mercredi 6 mai 2015

PDO query with multiple condition to be passed as JSON via ajax [duplicate]

This question already has an answer here:

This is how my JSON string is like:

$return='{"sub":{"4":"","5":""},"postcode":"89098","slider1":"230","action":"test"}';

This is how I get the values from JSON string :

$a=$data['sub'];

foreach($a as $v=>$k) 
  {

    $key[]=$v;

  }
 $key2=implode(',',$key);

 $postcode=$data['postcode'];
 $rate=$data['slider1'];

This is my query:

 $statement = $pdo->prepare("SELECT * FROM posts WHERE subid IN (:key2) AND postcode=:postcode2 AND rate=:rate2");
$statement->execute(array(':key2' => $key2,':postcode2'=>$postcode,':rate2'=>$rate));



$row = $statement->fetchAll(PDO::FETCH_ASSOC);

I'm unable to pass anything to the HTML page from the php script via AJAX as json right now.

Question 1) How do I form the query with 3 condition:

condition 1= subjects are filtered by $key

condition 2=postcode is filtered by $postcode

condition 3=rate is filtered by $rate

Question 2) I wish to pass this as JSON to the HTML page to be displayed there.SO how do I pass from this php script?

Currently I'm passing like echo json_encode($row);

My AJAX

$("form").on("submit",function() {

  var data = {
  "action": "test"
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
  type: "POST",
  dataType: "json",
  url: "ajax2.php", //Relative or absolute path to response.php file
  data: data,
  success: function(data) {
       var id = data[0];              //get id
    var vname = data[1]; 
    $(".the-return").html(
      "Subject: " + id
    );

    alert("Form submitted successfully.\nReturned json: " + data+"vname= "+vname);
  }
});
return false;  
});

Aucun commentaire:

Enregistrer un commentaire