Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    eri
    eri is offline
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Phone book problem

    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.

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


    Thanks.

    -------------------------------------------------------------------------

    HTML code

    -------------------------------------------------------------------------

    <html>

    <head>

    <script type="text/javascript">

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

    var http = createRequestObject()

    function sndReq(action) {

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

    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]

    }

    }

    }

    </script>
    </head>

    <body>
    <form id="search">
    <input type="text" id="input">
    <input type="button" value="Search" onClick="sndReq(document.getElementById('input').value)">
    </form>
    <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'"/>

    </body>

    </html>

    -------------------------------------------------------------------------

    External Perl Script (store.pl)

    -------------------------------------------------------------------------

    #!/usr/bin/perl

    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";

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    if you do

    Code:
    alert(display[3]);
    what does it show? [A properly formmatted URL or html markup?]

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    eri
    eri is offline
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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";

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • Users who have thanked A1ien51 for this post:

    eri (09-10-2008)

  • #5
    eri
    eri is offline
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thankyou for helping. Everything works fine now!
    Last edited by eri; 09-10-2008 at 09:26 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •