Page 1 of 1

Change the color of the word in the textbox when mispelled.

Posted: Tue Jul 17, 2007 2:27 pm
by icesha
Hi! I need help regarding a code i have been developing, i want to change the color of the generated output text when it is detected as wrong spelling. I can already check the spelling. I want it to look like the MSWORD where wrong spellings are underlines, as red line. Just like in this textbox. please i need help.

all i can do with this code is output it in a different page and change the color of the mispelled word. What i want is to underline or change the color of the mispelled word in the same page and same textbox.
here is my code. please. need help.

Code: Select all

<?php

include("ecsel_connectdb.php");

$var=$_POST['textarea'];

$var= trim($var,"\t\r\n");
$mypar=explode('.',$var);
//sentence


foreach($mypar as $b)
{
$b=trim($b, "\t\r\n "); 
echo "$b - This is a sentence";
echo "<br/>";
echo "<br/>";
}//end first for

$var=trim(str_replace('.','',$var));
$myarray=explode(' ',$var);
$pspell_link = pspell_new("en");

foreach($myarray as $a)
{
 	 $var=trim(str_replace('.','',$var));
	 $a=trim($a, "\t\r\n ");  	 	 	 	   
   		 $pspell_link = pspell_new("en");
		 if (pspell_check($pspell_link, $a))
   			{	
				echo "$a - This is a valid spelling";
				echo "<br/>";  
				echo "<br/>"; 	
     		} //end if
  		 else 
			{		
				/*echo "$a - Sorry, wrong spelling";
				echo "<br/>"; 
				echo "<br/>"; 
  */
              /* echo $var;
			   echo "<br/>"; */
			   
			   
			   echo "<font size=\"3\" face=\"Segoe UI\" color=\"red\"> $a <b><br /><br /><br><br> </font>";	
  				
               $wrong=$wrong +1;
                  if (!pspell_check($pspell_link, $a)) 
					   {
       				 		$suggestions = pspell_suggest($pspell_link,$a);
    						foreach ($suggestions as $suggestion) 
							{
       							
																
								echo "Possible spelling: $suggestion<br />";
								//echo "<br/>";  
							}//end inner for
					  }//end inner if
			}//end  elsehind
	

}//end for		
	
	
	
?>

Posted: Tue Jul 17, 2007 2:46 pm
by vigge89
You can't style specific parts of the contents in a textarea/input(type=text) with CSS/HTML styling.
There are ways to do this, but it will require Javascript and I'm not the one to explain it (don't have much knowledge in the area).

Posted: Tue Jul 17, 2007 3:21 pm
by ianhull
You could do with using AJAX and php, but you may require a MySQL dictionary to query agains misspelled word onchange

<textarea name="textarea" onchange="checkSpelling(this.value)">Microsof i$ blah etc</textarea>

Read up on AJAX at http://www.w3schools.com

Posted: Wed Jul 18, 2007 3:50 am
by Rovas
First I don' t know why are trying to do this the majority on current generation browsers have word spelling checker except IE 7. You' ll need a huge database for this.
Read some tutorials on AJAX, PHP and MySQL then come back with questions. It' s simple the server side part but on the client side (JavaScript) :( (I don' t have a clue how you will check each word that the user writes you' ll need a function that checks that user has wrote a word).

Posted: Thu Jul 19, 2007 5:50 am
by UrButtFullOfArr0ws
To ianhull: onchange doesn't seem to be SO dynamic, and i''d say onkeyup would be a better choice. And so that it doesnt check from the first letter you can add a middle function of the kind:

Code: Select all

function CheckLength(){
if (length >= 3){
checkSpelling(value);
}
anyway you get the idea :)