Welcome to WebmasterWorld Guest from 54.147.10.12

Forum Moderators: open

Message Too Old, No Replies

error when trying to paste url into ajax function

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

New User

5+ Year Member

joined:June 10, 2006
posts:34
votes: 0


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]

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

Preferred Member

5+ Year Member

joined:May 29, 2007
posts:578
votes: 0


Maybe its the &

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

?

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

New User

5+ Year Member

joined:June 10, 2006
posts:34
votes: 0


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]

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

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 0


You need to put quotes around the URL.

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

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

New User

5+ Year Member

joined:June 10, 2006
posts:34
votes: 0


it worked!

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

thanks.