0

I've been writing a currency converter and need to use jQuery and AJAX to send the to and from currencies, and the value to convert to a PHP files, which will return the converted value.

My solution is based off of: How can I call PHP functions by JavaScript? however it doesn't seem to work.

jQuery code:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

<script>
function exchange(from, to, amount){
    alert("got to here"); //this alert shows
    jQuery.ajax({
        type: "POST",
        url: 'exchange_caller.php',
        dataType: 'json',
        data: {functionname: 'exchange_rate_convert', arguments:["USD", "EUR", 1]},
        //dummy pass values

        success: function (obj, textstatus){
            if( !('error' in obj)){
                answer = obj.result;
                alert(answer);      //neither of these alerts show
                alert('anything');
            }
            else{
                console.log(obj.error);
                alert("got an error"); //this alert doesn't show
            }
        }
    });
    alert("passed the block"); //this alert shows
    return false;               //return false so the page doesn't refresh
}</script>

The code in the php file 'exchange_caller.php' (set up as a dummy).

<?php header('Content-Type: application/json');

$aResult = array();

if(!isset($_POST['functionname'])){
    $aResult['error'] = 'No function name!';
}
if(!isset($_POST['arguments'])){
    $aResult['error'] = 'No function arguments';
}
if(!isset($aResult['error'])){
    switch($_POST['functionname']){
        case 'exchange_rate_convert':
            if(!is_array($_POST['arguments']) || (count($_POST['arguments']) < 3)){
                $aResult['error'] = 'Error in arguments!';
            }
            else{
                $aResult['result'] = exchange_rate_convert($_POST['arguments'][0], $_POST['arguments'][1], $_POST['arguments'][2]);
            }
            break;

        default:
            $aResult['error'] = 'Not found function '.$_POST['functionname'].'!';
            break;
    }
}

echo json_encode($aResult);

function exchange_rate_convert($from, $to, $amount){    //function to run
    //dummy code, just return 2 for now
    $value = 2;
    return $value;
}?>

When this runs I receive the 'got to here' and 'passed the block' error messages, but neither of the messages when I should be getting a result back.

Any help would be greatly appreciated.

Thank you.

Community
  • 1
  • 1
Jordan
  • 11
  • 3
  • 1
    what about an `alert` with just `obj` itself, and don't forget to add an `error` catch block, might help you. and also check the network tab, its very useful in debugging ajax requests – Kevin Mar 21 '15 at 13:28
  • if you put a var_dump("arrived") in your php fucntion. this print? – Jacinto Mar 21 '15 at 13:28
  • An alert with just obj does not show. var_dump("arrived") also does not print, but I'm not sure where it's supposed to print to, as the purpose of using ajax is to not have to refresh the page. – Jordan Mar 21 '15 at 13:34
  • put the var_dump("arrived"), before the if(!isset($_POST['functionname']) and put die(); after the var_dump. He will print in the alert thaht you have – Jacinto Mar 21 '15 at 13:51
  • Check the console, any requests and responses will be visible there. – jeroen Mar 21 '15 at 14:03
  • I setup files with the posted code and they functioned as intended. It looks like there is another issue unrelated to the code provided. – KauriNZ Mar 21 '15 at 14:40

1 Answers1

0

I setup files with the posted code and they functioned as intended.

It looks like there is another issue unrelated to the code provided.

I suggest ensuring that there is not an issue with your hosting environment. Consult your server logs to see if another issue is preventing your request from being fulfilled and also test it in another browser to ensure that no addons or extensions are interfering with it.

KauriNZ
  • 142
  • 5
  • This does 'solve' the issue. By adding: to the jQuery/ajax file. However I now have the problem of the code running immediately, without being called. – Jordan Mar 21 '15 at 23:44
  • are you making a cross domain request? When both files were hosted on the same domain they functioned properly. Including/inlining the PHP will break your implementation. – KauriNZ Mar 22 '15 at 12:32
  • Both files are in the same folder – Jordan Mar 22 '15 at 15:18