Forum Moderators: open

Message Too Old, No Replies

error when trying to paste url into ajax function

         

kpop

9:23 pm on Jul 18, 2007 (gmt 0)

10+ Year Member



Hi guy,
I'm very new to ajax. Please help me fix error below.

This code supposes to take the url from the link, put it into showHint function and output and image.


<script type="text/javascript">
var xmlHttp

function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="img.php";
url=url+"?img="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
<a href="#" onclick="showHint(<[small][url=http://www.WebmasterWorld.com/javascript/3346054.htm]What URL?[/url][/small]>)">here</a>
<span id="txtHint"></span>

And this is the code of img.php


<?php
echo "<img src=\"{$_GET['img']}\">";
?>

the code won't work. when I paste the source code of the page into frontpage, it said I got an error near onclick. When I replace the link of the image by a random number or any other character, it works just fine. why?

thanks.

[edited by: DrDoc at 11:13 pm (utc) on July 18, 2007]

lavazza

10:09 pm on Jul 18, 2007 (gmt 0)

10+ Year Member



Maybe its the &

url=url+"&sid="+Math.random();
url=url+"&amp;sid="+Math.random();

?

kpop

10:22 pm on Jul 18, 2007 (gmt 0)

10+ Year Member



lavazza, there is nothing wrong with that part.
My question is, why is it not working with links, but with simple characters and numbers?

if I was to replace: "<What URL? [WebmasterWorld.com]>" with "2", the code is working fine but of course the output is not as expected (because I didn't paste any image link to img.php

thanks.

[edited by: DrDoc at 11:13 pm (utc) on July 18, 2007]

DrDoc

11:14 pm on Jul 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to put quotes around the URL.

<a href="#" onclick="showHint('http://www.example.com')">here</a>

kpop

11:17 pm on Jul 18, 2007 (gmt 0)

10+ Year Member



it worked!

thank you so much DrDoc
I didn't even notice that.

thanks.