2012-01-19 8 views
0

텍스트 영역에서 데이터를 제출하는 간단한 jQuery 함수를 작성했습니다. 스크립트는 계산 결과를 리턴합니다. recievedData가 정의되지 않았다는 오류가 발생합니다. 방화 광에서 디버깅 할 때 응답을 볼 수 있습니다.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; 
}); 

는 그리고 이것은 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

그냥 alert 대신에 그것을 호출하십시오 .. –

+1

역주의 쉼표없이 함수 이름을 제공하십시오 :'success : successFunction, ... ' – craigmj

1

JQuery와

그것은 recievedData이 정의되지 않은 사촌의
$('#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

관련 문제