, поэтому я сейчас пытаюсь реализовать скрипт конвертации валют с использованием Jquery, curl, ajax и Google api, однако у меня возникли некоторые проблемы.
Итак, вот jquery + ajax
$(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",
url: "conversion.php",
data: dataString,
success: function(data){
$('#result').show();
//Put received response into result div
$('#result').html(data);
}
});
});
});
А вот что у меня есть в Conversion.php
<?php
// sanitizing input using built in filter_input available from PHP 5.2
$amount = filter_input(INPUT_POST, 'amount', FILTER_VALIDATE_INT);
$from = filter_input(INPUT_POST, 'from', FILTER_SANITIZE_SPECIAL_CHARS);
$to = filter_input(INPUT_POST, 'to', FILTER_SANITIZE_SPECIAL_CHARS);
// building a parameter string for the query
$encoded_string = urlencode($amount) . urlencode($from) . '%3D%3F' . urlencode($to);
$url = 'http://www.google.com/ig/calculator?hl=en&amp;q=' . $encoded_string;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
$results = curl_exec($ch);
// this is json_decode function if you are having PHP < 5.2.0
// taken from php.net
$comment = false;
$out = '$x=';
for ($i=0; $i<strlen($results); $i++)
{
if (!$comment)
{
if ($results[$i] == '{') $out .= ' array(';
else if ($results[$i] == '}') $out .= ')';
else if ($results[$i] == ':') $out .= '=>';
else $out .= $results[$i];
}
else $out .= $results[$i];
if ($results[$i] == '"') $comment = !$comment;
}
// building an $x variable which contains decoded array
echo eval($out . ';');
echo $x['lhs'] . ' = ' . $x['rhs'];
Теперь проблема в том, что, когда я нажимаю кнопку конвертирования, она выводит всю веб-страницу в div #results, а не $ x из translation.php
Я провел весь день на этом сейчас, поэтому любая помощь очень ценится.
FYI - Curl установлен и работает правильно