0

I wrote a simple jQuery function to submit data from a textarea. The script returns the result of the computation. I get an error saying that recievedData isn't defined. When debugging with firebug I can see a response.

Here's my jQuery function.

$('#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;
});

And this is the php script

<?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
  • 1
    It's coz recievedData isn't defined. ;-) Side note: if you decide to use that name in your application, I recommend spelling it "receivedData" -- and I'm not being snotty or pedantic; we spelled "received" incorrectly in our application and it haunted us for years! No joke! (Had to do with backwards compatibility) Commented Jan 19, 2012 at 5:41

2 Answers 2

2

Write

success:function(data){alert(data);}
Sign up to request clarification or add additional context in comments.

2 Comments

That worked, one more doubt. If I wanted to call a named function instead of an anonymous function, what would I need to do?
Just provide the function name, without inverted commas: success: successFunction, ...
1

Another way to post with jquery

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

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

evt.preventDefault;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.