AJAX: textarea this.value???

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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

AJAX: textarea this.value???

Post by simonmlewis »

I am brand new to using Ajax in PHP.
I have gone thru a few tutorials to try and grasp it.

I have come up with a real need for it on our of our web sites.
The premise is to stop people entering certain HTML code, like FONT into a box. Or even Pasting it from formatted text in MS Word.

Below is what I have tried (the important part of the code), but as a clear novice, I am obviously missing something.

The HTML content is pasted in. OnChange, it should be going to htmlcheck.php. That page checks thru preg_match for illegal code. If it finds some, it echos the warning. That warning should be in the DIV ID box.

What am I doing wrong?

Code: Select all

<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","htmlcheck.php?q="+str,true);
xmlhttp.send();
}
</script>
<textarea id='elm1' name='description' rows='6' cols='40' onchange=\"showUser(this.value)\"></textarea>
			<br/>
			<div id='txtHint'>&nbsp;</div>

Code: Select all

<?php
$q=$_GET["q"];

if (preg_match("/font/i", "$q")) { echo "<div class='warningbox'><b>ERROR</B><BR/>
The content you have entered contains invalid HTML for your web site.<br/>
We strongly suggest you paste your text into Notepad and then copy/paste that text into the box above and format using the buttons beneath.<br/></div>";}
?>
[text]Update: it works if I use it on the input text part, that would have a "value" in it.
ie. <input type ='text' value=''>
But won't on the <textarea> as there is no 'value' to it. Is that right ?? Is there a workaround?
[/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: AJAX: textarea this.value???

Post by simonmlewis »

I've just made it work on an <input type='text'> field again. Entering FONT... it just propped up the error.
But if I do it in TEXTAREA, where it doesn't display the html as it's one of those HTML type screens (with the B, I, U etc... in it).

Technically it is being entered. But even if I manually type FONT in there, it still doesn't work. And I am convinced it is because a TEXTAREA doesn't have the VALUE attribute like an INPUT field does.

HELP PLEEAASSEE.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply