텍스트 영역에서 데이터를 제출하는 간단한 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;
?>
에 게시하는 또 다른 방법입니다. ;-) 참고 : 응용 프로그램에서 해당 이름을 사용하기로 결정한 경우 "receivedData"라는 철자를 사용하는 것이 좋습니다. 우리는 우리의 응용 프로그램에서 "받아 들여진"철자가 틀렸고 몇 년 동안 우리를 귀찮게했습니다! 농담 아냐! (이전 버전과의 호환성과 관련이 있음) –