0

I'm trying to learn using AJAX but I've completely stuck somewhere.

So I have the following code:

<label>View as:</label> <a href ="#" onClick="return false" onmousedown="javascript:swapContent('con1');">Test 1</a> / <a href ="#" onClick="return false" onmousedown="javascript:swapContent('con2');">Test 2</a><br/>
<div id = "myDiv">asdasd</div>

The following .js script:

function swapContent(cv) {
$("#myDiv").html("Put animated .gif here").show();
var url = templateDir;
$.post(url.concat("/profileajax.php"), {contentVar: cv}, function(data) {
    $("#myDiv").html(data).show();
})
}

And the following profileajax.php page:

<?php
$contentVar = $_POST['contentVar'];
if($contentVar == "con1") {
    echo get_bloginfo('template_directory');
}
else {
    echo "Test";
}
?>

However when I'm clicking on the first link, I get the following error: Fatal error: Call to undefined function get_bloginfo() in D:\Webdesign\XAMPP\htdocs\wordpress\wp-content\themes\globestate\profileajax.php on line 4.

How can I make it work? (By the way I'm using Wordpress)

1
  • You need to define that function in your PHP file. Otherwise the AJAX is working perfectly. Commented May 29, 2015 at 13:14

2 Answers 2

1

Add a definition for the get_bloginfo() function to your PHP -

<?php
function get_bloginfo($dir) {
    // just for test we'll return the argument passed in
    return $dir;
}

$contentVar = $_POST['contentVar'];

if($contentVar == "con1") {
    echo get_bloginfo('template_directory');
} else {
    echo "Test";
}

?>

Since it seems your AJAX is working you should see "template_directory" echo'd to the page.

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

5 Comments

What I am trying to do is make the profileajax.php page recognize all the wordpress functions, not only the specific get_bloginfo function
You're not including any of the wordpress functions in this PHP file.
Well that's what I want to do but I don't know how. (Sorry! I'm a beginner)
Then you need to work your way through some of the WP tutorials.
This article is a good place to begin: codex.wordpress.org/AJAX_in_Plugins
0

I fixed the problem! What I did was include this line on top of the php page: require_once("../../../wp-load.php");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.