0

i have an ajax load request working in wordpress, but i would like to load the content from another page into the container div. at the moment it just passes the url in $dataToSend as a string?

jQuery(document).ready(function(){

    var $dataToSend = "my-page.php";
    var $testBtn = jQuery('#text-ajax-btn');
    var $holdingCtn = jQuery('#my-holding-ctn');

    $testBtn.click(function(){
        jQuery.ajax({
            type:'POST',
            url: myAjax.ajaxurl,
            data:{
                action:'myAjax',
                dataToSend:$dataToSend,
            },
            success: function(data,textStatus,XMLHttpRequest){
                $holdingCtn.html("");
                $holdingCtn.append(data);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                alert(errorThrown);
            }
        });
    });
});

how can i pass an entire .php page through as the $dataTosend?

2
  • url: myAjax.ajaxurl shouldnt this contain my-page.php? I think you are doing opposite of what you want to do :) why °sending° when obviously want to load something in the current page? Commented Sep 25, 2013 at 22:56
  • thanks, have tried this and now i get an error: '500 (Internal Server Error)' in the console. the url looks to be correct. im pretty new to ajax in wordpress, is this because im on a local server 'mamp'? Commented Sep 25, 2013 at 23:06

3 Answers 3

1

I do this all the time for wordpress, give me a sec to access my repository and I will show you example code.

I think problem is your my-page.php! I imagine you custom coded it. So it doesn't have necessary functions loaded.

put following code at the top of your my-page.php (this will help with 500 error you are getting)

require('../../../wp-load.php');

ajax part should look something like this:

//start ajax
            $.ajax({
                url: "http://localhost/wp-content/themes/theme/my-page.php",
                type: "POST",
                data: data,
                cache: false,
                success: function (data) {

                    console.dir(data);

                }
            })
Sign up to request clarification or add additional context in comments.

1 Comment

this worked for me, just adding "require('../../../wp-load');" to the top of the page to be loaded actually made it possible to use the shorthand jQuery('#my-ctn').load('my-page.php'); thanks
0

If you want to load content from my-page.php file then you can load from the server side using

$data = file_get_contents('path/to/file/my-page.php'); // pass right path/url

Then, just echo the content from your function (registered ajax handler in WordPress using add_action) and in this case it should be

echo $data;
die(); // terminate the further execution

So, it should look something like

add_action( 'wp_ajax_myAjax', 'yourAjaxHandler' );
add_action( 'wp_ajax_nopriv_myAjax', 'yourAjaxHandler' );
function yourAjaxHandler(){
    $data = file_get_contents('path/to/file/my-page.php');
    die($data); // echo out the string and terminates execution
}

In your success callback, you can use

success: function(data){
    jQuery('#my-holding-ctn').html(data);
}

1 Comment

Here is a good read about ajax in WordPress.
0

Not sure if this is fully applicable, but the super easy way is just

$("#myDiv").load("myFile.php?foo=1&bar=2...");

4 Comments

yeah this was my first approach, but couldnt get this to work either. I actually ended up at the same point when hardcoding the url for the page in i got the same '500 (Internal Server Error)' which makes me think its to do with being on a localhost?
Your approach is nice but not appropriate for wordPress, In WordPress you need to do it via admin-ajax.php/myAjax.ajaxurl file and using ajax hooks.
Here is a good read about ajax in WordPress
@user2805371 if you were seeing 500s, then that means the request was being made (e.g., your ajax logic is fine) and something was wrong with the request or the file you were calling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.