I think i've almost got my head around this ajax is Wordpress business, thoroughly ejoying learning it but i'm now totally stumped.
First up is the nasty bits! I have these in my main plugin file:
wp_enqueue_script( 'function', plugin_dir_url( __FILE__ ) . 'function.js', array( 'jquery', 'json2' ) );
wp_localize_script( 'function', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
add_action('wp_ajax_nopriv_pfxconversion', 'pfxconversion');
Here is what's is function.js
$(document).ready(function() {
$("#convert").click(function () {
var from = $("#from").val();
var to = $("#to").val();
var amount = $("#amount").val();
//Make data string
var dataString = "amount=" + amount + "&from=" + from + "&to=" + to;
$.ajax({
type: "POST",
action: pfxconversion,
url: MyAjax.ajaxurl,
data: dataString,
success: function(data){
$('#result').show();
//Put received response into result div
$('#result').html(data);
}
});
});
});
and here is the function that I know works perfectly outside of Wordpress.
function pfxconversion () {
//Get Posted data
$amount = $_POST['amount'];
$from = $_POST['from'];
$to = $_POST['to'];
//make string to be put in API
$string = "1".$from."=?".$to;
//Call Google API
$google_url = "http://www.google.com/ig/calculator?hl=en&q=".$string;
//Get and Store API results into a variable
$result = file_get_contents($google_url);
//Explode result to convert into an array
$result = explode('"', $result);
################################
# Right Hand Side
################################
$converted_amount = explode(' ', $result[3]);
$conversion = $converted_amount[0];
$conversion = $conversion * $amount;
$conversion = round($conversion, 2);
//Get text for converted currency
$rhs_text = ucwords(str_replace($converted_amount[0],"",$result[3]));
//Make right hand side string
$rhs = $conversion.$rhs_text;
################################
# Left Hand Side
################################
$google_lhs = explode(' ', $result[1]);
$from_amount = $google_lhs[0];
//Get text for converted from currency
$from_text = ucwords(str_replace($from_amount,"",$result[1]));
//Make left hand side string
$lhs = $amount." ".$from_text;
################################
# Make the result
################################
echo $lhs." = ".$rhs;exit;
}
The problem is I don't get the result from my function / ajax request printed out in the #result div
Any help is greatly appreciated.
Edit I'm now getting a -1 value in #result, after changed my js from $ to jQuery - Firebug was reporting an error ($ is not a function).
Edit 2 I'm pretty certain I have the action right now but i'm still getting an output in #result of -1:
jQuery(document).ready(function() {
jQuery("#convert").click(function () {
var from = jQuery("#from").val();
var to = jQuery("#to").val();
var amount = jQuery("#amount").val();
//Make data string
var dataString = "amount=" + amount + "&from=" + from + "&to=" + to;
jQuery.ajax({
type: "POST",
url: MyAjax.ajaxurl,
data: "action=pfxconversion"&dataString,
success: function(data){
jQuery('#result').show();
//Put received response into result div
jQuery('#result').html(data);
}
});
});
});
add_action('wp_ajax_pfxconversion', 'pfxconversion');
right after theno_priv
action? What if you change all the$
in your js tojQuery
? – mor7ifer Jan 29 '12 at 20:09