I use
if(isset($_POST['category_drop'])){
var_dump($_POST );
echo $_POST['category_drop'];
} {...} to check for the post condition in PHP.
Kindly provide me a solution.
$('.category_filter .dropdown-menu li a ').on('click', function(e) {
e.preventDefault();
var category = $(this).text();
$.ajax({
type: "POST",
url: 'page_author.php',
data: {
category_drop: category
},
success: function(data) {
// do something;
alert(category);
}
});
});
I am getting this below value in an alert box , but my page is not getting loaded with the respective value,
array(1) { ["category_drop"]=> string(12) "Accomodation" }
Accomodation
The commented Code below shows how you may accomplish your goal:
e.preventDefault();
var category = $(this).text();
$.ajax({
type: "POST",
url: 'page_author.php',
data: {
category_drop: category
},
success: function(data) {
// PERHAPS IT'S NOT category BUT data THAT YOU SHOULD BE INTERESTED IN.
// AND; YOU BETTER console.log()
// TO AVOID THOSE ANNOYING JAVASCRIPT ALERTS...
// ALTHOUGH, YOU MAY STILL ALERT THE category IF YOU WISHED...
console.log(data);
//alert(category);
}
});
ON THE PHP SIDE OF THINGS:
<?php
if(isset($_POST['category_drop'])){
// ASSIGN THE $_POST['category_drop'] TO A VARIABLE $categoryDrop
// TO BE USED IN YOUR MySQL DATABASE QUERIES...
$categoryDrop = $_POST['category_drop'];
// YOU CAN NOW USE $categoryDrop IN YOUR MySQL QUERIES.
// AFTERWARDS, IF YOU WANT TO ONLY SEND BACK THE $categoryDrop
// YOU CAN SIMPLY DO:
//EITHER:
die($categoryDrop);
}
PHP:
$result = array(
'success' => false,
);
if(isset($_POST['category_drop'])){
$result['category'] = $_POST['category_drop'];
$result['success'] = true;
// you can pass in result array whatever you need
}
echo json_encode($result);
exit;
Js:
$('.category_filter .dropdown-menu li a ').on('click', function(e) {
e.preventDefault();
var category = $(this).text();
$.ajax({
type: "POST",
url: 'page_author.php',
dataType: 'application/json',
data: {
category_drop: category
},
success: function(data) {
if(data.success){
//do something here
console.log(data.category);
console.log(data);
}
}
});
});
Always use good practices :)
As per my understanding of your question you may follow this.
$result = array(
'category' => null,
'success' => false
);
if (isset($_POST['category_drop'])) {
$result['category'] = $_POST['category_drop'];
$result['success'] = true;
}
echo json_encode($result);
exit;
$('.category_filter .dropdown-menu li a ').on('click', function (e) {
e.preventDefault();
var category = $(this).text();
$.ajax({
type: "POST",
url: 'page_author.php',
dataType: 'application/json',
data: {
category_drop: category
},
success: function (data) {
if (data.success) {
// append here the category value
// to your respective TAG
$("YOUR-RESPECTIVE TAG").text(data.category);
//OR
$("YOUR-RESPECTIVE TAG").text(data['category']);
}
}
});
});