2012-01-19 9 views
0

テキストエリアからデータを送信する簡単なjQuery関数を記述しました。スクリプトは計算結果を返します。 recievedDataが定義されていないというエラーが表示されます。 firebugでデバッグすると、私は応答を見ることができます。jQueryを使用したPHPスクリプトとの通信

ここに私のjQuery関数があります。

$('#submitButton').click(
function(evt){ 
userCode = $('#answer').val(); 
$.ajax({ 
type : 'POST', 
url : 'scripts/php/check_answer.php', 
data : 'code=' + userCode, 
dataType : 'text', 
success : alert(recievedData) 
}); 
evt.preventDefault; 
}); 

そして、これがrecievedDataが定義されていないだってそれだPHPスクリプト

<?php 

//sample string, to be taken as input from the user program. 
$str = $_POST['code']; 
//the array with the weights 
$patterns = array (
    '/[:space]/' => 0, //whitespaces are free; need to add spaces too 
    '/[a-zA-Z]/' => 10, //characters 
    '/[0-9]/' => 500, //digits 
    '/[\+\-\/\*\%]/' => 10000, //arithmetic operators 
    '/[\<\>]/' => 5000, //relational operators 
    '/[\^\~\|\&]/' => 1000 , //bitwise operators 
    '/[\=]/' => 250, //assignment operator 
    '[\#]' => 100000 //No Macros 
); 

//default weight for characters not found is set here 
$default_weight = 55; 
$weight_total = 0; 
foreach ($patterns as $pattern => $weight) 
{ 
    $match_found = preg_match_all ($pattern, $str, $match); 
    if($match_found) 
    { 
    $weight_total += $weight * $match_found; 
    } 
    else 
    { 
    //this part needs to be fixed 
    $weight_total += $default_weight; 
    } 
} 

echo $weight_total; 

?> 
+1

に投稿するもう一つの方法です。 ;-)サイドノート:アプリケーションでその名前を使用する場合は、「receivedData」というスペルミールをおすすめします。私たちのアプリケーションで間違って「受け取った」と綴られていて、何年も私たちを悩ませました!冗談抜き! (下位互換性を持っていなければならなかった) –

答えて

2

書き込み

success:function(data){alert(data);} 
+0

それはうまくいきました。匿名関数の代わりに名前付き関数を呼びたい場合、何をする必要がありますか? – nikhil

+0

ただ警告の代わりにそれを呼び出してください。 –

+1

逆のカンマなしで関数名を指定するだけです: 'success:successFunction、...' – craigmj

1

jqueryの

$('#submitButton').click(
function(evt){ 
userCode = $('#answer').val(); 

$.post("scripts/php/check_answer.php", {code: userCode}, 
    function(data) { 
    alert("Data Loaded: " + data); 
    }); 

evt.preventDefault; 
}); 
+0

これはよりエレガントです。 – nikhil

関連する問題