私は現在、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);
}
});
});
});
そして、ここでは、私はそれが全体を出力している変換ボタンをクリックしたときに
<?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'];
は今問題があるconversion.phpに持っているものであるのjquery +アヤックスであります$ xではなく#results divのWebページを
私は一日中ここに費やしていますので、どんな助けにも大変感謝しています。
FYI - カールがインストールされ、正しく
「ウェブページ全体を出力する」とはどういう意味ですか?それはconversion.phpファイルの内容ですか?ブラウザーやcurlコマンドラインからurl "conversion.php"(paramsとともに)を試して、正しく動作するかどうか確認することもできます。 – rsmoorthy
申し訳ありませんが、私はスクリプトをテストしているウェブページを参照しています。したがって、#resultには、全体のWebページ(ヘッダー、ナビゲーションメニュー、コンテンツなど)が表示されます。 – Danny
成功関数内でconsole.log(data)を試してみてください。 Firebugのようなブラウザデバッガをチェックインして、送信されたPOST要求とサーバから受信した応答を確認します。それはおそらく助けになるでしょう。 – rsmoorthy