Reputation: 11
i am trying to return a variable and I really don't understand how to do it. This is the
jquery part $('a[href="if_blog"]').click( function( e ) {
e.preventDefault();
var bblog = $( this ).attr('href');
var dataString = 'bblog=' + bblog;
$.ajax({
type: 'POST',
url: '',
data: dataString,
success: function(data){
$('#php').html(data);
}
});
});
this is the php part and it is part of a big wordpress file:
$byzmo_blog = $_POST['bblog'];
switch ($byzmo_blog) {
case 'if_blog':
$byzmo_postype = 'if_posts';
break;
case 'food_mood_blog':
$byzmo_postype = 'food_mood';
break;
case 'look_good_blog':
$byzmo_postype = 'look_good';
break;
case 'feel_good_blog':
$byzmo_postype = 'feel_good';
break;
}
?>
<div id="php"></div>
<?php var_dump($_POST);
$query = array(
'post_type' => $byzmo_postype,
'posts_per_page' => get_theme_option('blog_posts'),
'orderby' => get_theme_option('blog_orderby'),
'order' => get_theme_option('blog_order'),
'paged' => ( get_query_var('paged') ? get_query_var('paged') : true )
);
?>
So when I click the link (a[href="if_blog"]') I want to load some content according to what $_POST shows, but bcs I am in the same page data loads the whole index.php file, so it is loaded twise in the page(because I use .html()) and then it loads in $_POST too.. how can use only the variable in data with $_POST for this part $byzmo_blog = $_POST['bblog']; ?
I hope you can understand me......
EDIT: Ok, i am afraid I don't get it: so I have the index.file which is sth like this:
<a href='if_blog'>The Link</a>
<?php
$post_type = $_POST['posttype'];
$query = array(
'post_type' => $post_type,
'posts_per_page' => get_theme_option('blog_posts')
);
?>
<?php query_posts( $query ); if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); endwhile; endif; ?>
So, clicking the link is fetched by the jquery file:
jQuery.noConflict();
jQuery( document ).ready( function( $ ) {
$('a[href="if_blog"]').click( function( e ) {
var bblog = $( this ).attr('href');
var url = 'test.php';
$.post(url, { byzmo_blog: bblog },
function(data) {
alert(data);
//console.log(data); //
}); //, "json");
e.preventDefault();
});
} );
returns either a string from text.php, or an object if I apply json_encode.
Now from this point I want to return this data to index.html and use it $post_type = $_POST['posttype']; further.. when I use the console.log nothing happens, do I have to use json_decode here? i searched a lot but just can't get it at this moment... :/
Upvotes: 0
Views: 761
Reputation: 10961
use $.post('url', '{data}', function(answer));
instead. You can gather more info from postJquery.
Basically, you have:
$.post("test.php", { name: "John", time: "2pm" },
function(data) {
alert("Data Loaded: " + data);
});
in the client site. So, in you server side, retrieve the value as
$name = $_POST['name'];
$time = $_POST['time'];
You can use json_decode($_POST['name'])
to the JavaScript literal object to a PHP array, and to send it back to the page use json_encode($array)
; In your example, you did not give POST
to where send the data to. You definitely need the url to where to POST
to.
Upvotes: 1