ProjectCleverWeb / Semantic-UI-WordPress

This project incorporates Semantic UI into a starter (aka developer) theme for WordPress.
Other
275 stars 59 forks source link

Can't get $_POST/$_GET by AJAX #6

Closed plugza closed 10 years ago

plugza commented 10 years ago

I'm not sure if this involved with your wordpress project or not. But I wasted 14 hours with this problem.

Use $_POST/$_GET by AJAX to send data back to index.php I always get empty no matter what.

Here's a example

Jquery part: (This is in head.js ready function)

$('#side_menu ul li a').click(function(e) {
                    e.preventDefault()

                    var pageId = $(this).first().attr('id');
                    var pageurl = $(this).attr('href');

                    if (pageId != null) {
                        // send the pageId
                        $.ajax({  
                            type: 'POST',
                            url: pageurl,
                            cache: false,
                            data: "page_get="+pageId,
                            success: function() {
                                console.log( 'page_id: ' + pageId + " sent!");
                            },
                            error:function() {
                                console.log("AJAX request was a failure");
                            }
                        });
                });

As you can see I try to get a href id by clicking it and the success function is working.

Next is in php part:

$page_name = (!empty($_POST['page_get'])) ? $_POST['page_get'] : 0;

The result of this $page_name is 0 no matter what. If I check is it empty. it's empty.

BUT the interesting part is Chrome Debugging Console is really show the correct result.

If this is not an issue. So I need some help with this problem. I'd appreciate it if you can help.

ProjectCleverWeb commented 10 years ago

This may be a namespace/scope issue. You are probably either setting or getting $page_name in the wrong namespace/scope. Try putting echo __NAMESPACE__; both where you are getting and where you are setting $page_name. If it's scope related, you can put global $page_name; before your getter/setter and see if that changes your results.

Let me know how it goes.