我写了一个简单的jQuery函数来从textarea提交数据。该脚本返回计算结果。 我收到一条错误,说没有定义recievedData。 使用firebug进行调试时,我可以看到响应。
这是我的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;
?>
答案 0 :(得分:2)
写
success:function(data){alert(data);}
答案 1 :(得分: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;
});