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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kittuk
Forum Newbie
Posts: 5
Joined: Sat Jan 23, 2010 5:08 am
Location: India

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

Post 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!!! :(
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

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

Post 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>
kittuk
Forum Newbie
Posts: 5
Joined: Sat Jan 23, 2010 5:08 am
Location: India

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

Post 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:
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

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

Post by maxx99 »

http://www.akchauhan.com/word-count-in-textarea/
Example of JS word count.

Don use AJAX for this kind of tasks :)
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

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

Post 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
kittuk
Forum Newbie
Posts: 5
Joined: Sat Jan 23, 2010 5:08 am
Location: India

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

Post by kittuk »

Thanks guys.. will check it out & reply back!!

btw.. why not use Ajax?? :) m a beginner.. please explain!
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

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

Post 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?
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

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

Post 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.
kittuk
Forum Newbie
Posts: 5
Joined: Sat Jan 23, 2010 5:08 am
Location: India

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

Post by kittuk »

Ah.. geddit!! :D

Thank you guys!!
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

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

Post 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
Post Reply