Page 1 of 1

PHP-Ajax code working slowly ??!!???

Posted: Wed Nov 23, 2011 2:54 am
by kittuk
Aim: to calculate number of words entered in textarea
whats happening: its counting words & special chars (except +) from textarea as words. which is perfectly fine.
Whats going wrong: If I copy paste a large chunk of words - e.g. 900 , i dont get a output.. if i add one character at a time, it calculates..but a large no of words & it just stops!!!

textarea code:
<textarea rows="10" cols="40" name="noofwords" onKeyUp="calculate_words(this.value);"></textarea>

PHP code where calculation is happening:

if(!empty($totalwords))
{
//$wordlength = strlen($totalwords);
$string = trim(preg_replace(array('/\s{2,}/', '/[\t\n]/')," ",$totalwords)); // \t \n n " " is replaced by " "
$word_array = explode(" ", $string);
$num = count($word_array);

echo "3"."|".$num;
}

dont think variables are wrong.. there is a js file in between where $totalwords = noofwords entered in textarea. Only problem: its SLOW!!! :(

Re: PHP-Ajax code working slowly ??!!???

Posted: Wed Nov 23, 2011 2:59 am
by mikeashfield

Code: Select all

<html lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>JavaScript Textarea Example</title>
<script type="text/javascript"><!--
var wordLen = 255; // Maximum word length
function checkWordLen(obj){
var len = obj.value.split(/[\s]+/);
if(len.length > wordLen){
alert("You cannot put more than "+wordLen+" words in this text area.");
obj.oldValue = obj.value!=obj.oldValue?obj.value:obj.oldValue;
obj.value = obj.oldValue?obj.oldValue:"";
return false;
}
return true;
}
//--></script>
</head>
<body>
<h1>JavaScript Example</h1>
<p>This JavaScript sets a maximum word count to a textarea.</p>
<form action="" method="get"><fieldset>
<legend>Example Form</legend>
<label>Text input: <br><textarea rows="15" cols="30" name="t1" onchange="checkWordLen(this);"></textarea></label>
</fieldset></form>
</body>
</html>

Re: PHP-Ajax code working slowly ??!!???

Posted: Wed Nov 23, 2011 3:03 am
by kittuk
Hey thanks, but I dont want to put a limit to the number of words entered!

Is that what u meant? if not, i dont understand :banghead:

Re: PHP-Ajax code working slowly ??!!???

Posted: Wed Nov 23, 2011 3:17 am
by maxx99
http://www.akchauhan.com/word-count-in-textarea/
Example of JS word count.

Don use AJAX for this kind of tasks :)

Re: PHP-Ajax code working slowly ??!!???

Posted: Wed Nov 23, 2011 3:18 am
by Gopesh
Hi,I think it is because of cache.Use Math.random() with the URL.This ensures a unique URL everytime and hence the Cache problem will get solved. check this linkhttp://viralpatel.net/blogs/2008/12/aja ... in-ie.html.Hope it helps

Re: PHP-Ajax code working slowly ??!!???

Posted: Wed Nov 23, 2011 3:21 am
by kittuk
Thanks guys.. will check it out & reply back!!

btw.. why not use Ajax?? :) m a beginner.. please explain!

Re: PHP-Ajax code working slowly ??!!???

Posted: Wed Nov 23, 2011 3:32 am
by mikeashfield
AJAX is used primarily to communicate back to the server for precessing without a reload. Seeing as JS can do the task, why have the server do it?

Re: PHP-Ajax code working slowly ??!!???

Posted: Wed Nov 23, 2011 3:36 am
by maxx99
I agree with mikeashfield :)
If you can do it on client side (without any performance/compatibility drawbacks - like this case) - you should do it there :)

AJAX requests are relatively slow. You could argue that 5, 10, 20 or even 50ms = super fast. But it isn't :)
Another thing is that every unnecessary request puts additional load on your web server and thats also something you should learn to avoid from the very beginning.

Re: PHP-Ajax code working slowly ??!!???

Posted: Wed Nov 23, 2011 3:38 am
by kittuk
Ah.. geddit!! :D

Thank you guys!!

Re: PHP-Ajax code working slowly ??!!???

Posted: Wed Nov 23, 2011 3:45 am
by mikeashfield
I once saw an example of code where the developer had no idea how to script simple JS (or use Google it would have seemed) where they had a form submit action="" on each keyup and echo the counter=counter++ haha