View Full Version : Phone book problem

09-08-2008, 10:59 AM
I am working on a phone book that uses AJAX and Perl. When the name is searched for, it should display the name, address, phone number, e-mail and an image. Everything works fine except for the image part. HTML and Perl code is below. Could someone please correct my code or let me know why the image does not get displayed while everything else does.:confused:

Also, this is my first time posting here... Please let me know if I have posted under the wrong forum.



HTML code




<script type="text/javascript">

function createRequestObject() {
var ro
var browser = navigator.appName
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP")
ro = new XMLHttpRequest()
return ro

var http = createRequestObject()

function sndReq(action) {

http.open('get', 'cgi/store.pl?searchInput='+action, true)
http.onreadystatechange = handleResponse

function handleResponse() {

if(http.readyState == 4){

var response = http.responseText;
var update = new Array()

if(response.indexOf('|')) {
display = response.split('|')

document.getElementById('name').innerHTML = document.getElementById('input').value
document.getElementById('address').innerHTML = display[0]
document.getElementById('phone').innerHTML = display[1]
document.getElementById('email').innerHTML = display[2]
document.getElementById('image').src= display[3]





<form id="search">
<input type="text" id="input">
<input type="button" value="Search" onClick="sndReq(document.getElementById('input').value)">
<div id="name"></div>
<div id="address"></div>
<div id="phone"></div>
<div id="email"></div>
<img id="image" style="display:none" onload="style.display = 'block'"/>




External Perl Script (store.pl)



print "Content-type: text/html\n\n";

%query = split(/=/, $ENV{QUERY_STRING});

$searchEntry = $query{"searchInput"};

%addressdb = ('abc' => 'Address',
'xyz' => 'Address');

%phonedb = ('abc' => 'Phone Number',
'xyz' => 'Phone Number');

%emaildb = ('abc' => 'E-mail',
'xyz' => 'E-mail');

%imagedb = ('abc' => '<img src="http://domainname.com/image1.jpg" />',
'xyz' => '<img src="http://domainname.com/image2.jpg" />');

$address = $addressdb{"$searchEntry"};
$phone = $phonedb{"$searchEntry"};
$email = $emaildb{"$searchEntry"};
$email = $emaildb{"$searchEntry"};
$image = $imagedb{"$searchEntry"};

print "$address|$phone|$email|$image";

09-08-2008, 04:20 PM
if you do


what does it show? [A properly formmatted URL or html markup?]


09-08-2008, 04:48 PM
I inserted the code alert(display[3]); and when executed it displays the image url that is stored in the perl script for 'abc'<img src="http://domainname.com/image1.jpg" /> I still don't know what I am doing wrong. Would really appreciate it if someone could explain it to me...
I am very new to perl so I am guessing there's something wrong with the script. I really don't know!

09-08-2008, 04:56 PM
when you set the source of the image it just wants the URL. Nothing else. You need ...src="http://www.example.com/images/foo.gif";


09-08-2008, 05:50 PM
Thankyou for helping. Everything works fine now!