Jump to content

strange result in "echo" (php/ajax)


valanews.com
 Share

Recommended Posts

Hi I wana check value of an input text for validation by ajax. I read a great tutorial in w3schools.com in here:http://w3schools.com...ax_database.asp and I changed it for my business. so I have 3 file:in index.php

<div class="fieldwrapper">  <div class="thefield 1" id="idn1">	<input name="password" type="password" value="" size="30" onchange="showUser(this.value)"/>  </div></div>

and and some of code in second file: load.js

 function showUser(str){if (str=="")  {  document.getElementById("idn1").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("idn1").innerHTML=xmlhttp.responseText;	}  }xmlhttp.open("GET","getuser.php?q="+str,true);xmlhttp.send();}

and finaly, in third file: (external php): idn1.php

<?php$q=$_GET["q"];   if(strlen($q) <6 && strlen($q) <> NULL)	{ echo "<p> it's true </p>";	  }      else	{    echo "<p> it's true </p>";	}

this php/ajax code works great... Now I wana write a single "html tag" in my code in idn1.php (not double html tag)like this:

<?php$q=$_GET["q"];   if(strlen($q) <6 && strlen($q) <> NULL)	{   echo "<p> it's true </p>";  	  }      else	{   echo "<div>";   echo "<p> it's true </p>";	}

for (strlen($q) >=6), when I look in index.php html source code (by browser [google chorme- [inspect element])I expect view these result:

 <div><p>it's true</p>

but i get these strange result:

 <div><p>it's true</p></div>

I hope u got my means.I don't want print </div> tag ....I just wana see <div> tag in my html source code.what can I do to solve this problem?

Edited by valanews.com
  • Like 1
Link to comment
Share on other sites

that's not strange. you are setting the innerHTML of the div to the response you are echoing. I'm not sure what the issue is, if your concern is printing out </div>, because your actual result already shows that happening. (as it should)

Link to comment
Share on other sites

The solution is to apply htmlspecialchars() to the string before returning it from the server.
like this:
$str="<div>";  echo htmlspecialchars($str) ?>

No! I think u don't underestand my means I don't what print a html tag as a normal text or as a stringI wana bring a single tag (start tag or end tag) from idn1.php to index.php...becausefor example when I type a start tag [like: echo "<div>"; ] The server (or debugger) automatically create end tag.

Edited by valanews.com
Link to comment
Share on other sites

It's JavaScript that is doing that. You are messing with DOM stuff and it will try to close any tags you don't close. The only way I know to go around that is to simply load the response into the variable instead of printing it on the screen.So instead of doing this:

document.getElementById("idn1").innerHTML=xmlhttp.responseText;

Do this:

var str = xmlhttp.responseText;

After you do that, append anything you want to that string, THEN use innerHTML:

document.getElementById("idn1").innerHTML=str;

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...