Hello!

I'm trying to make a small photoalbum, and it's going fairly well.

The picture-handling works as I want it, but I can't get the comment-handling to work. The following code displays the image just fine, and I can change the image by using javascript to update the .src of the image, but I can't get the same thing to work with innerHTML.

Below is index.php

<html>
<head>
<script type="text/javascript">
var i = 1;
var maxSize=<?php include 'maximg.php'; ?>;
var newWidth=100;

function onLoad(){
  loadImg();
}

function loadImg(){
    document.imgSrc.src = "image.php?id="+i;
    document.getElementById('comment').innerHTML= "com_txt.php?id="+i;       
} function prev(){ i=i-1; if (i<1){ i=1; } loadImg(); } function next(){ i=i+1; if (i>maxSize){ i=maxSize; } loadImg(); } window.onload=onLoad; </script> </head> <body> <table border="1"> <tr> <td> <table border="1"> <tr> <button type="button" onClick="prev();">Prev</button> <button type="button" onClick="next();">Next</button> </tr> </table> </td> </tr> </table> <img name="imgSrc" id="imgSrc" onError="error();" alt="Picture not loaded"></img> <hr> <div id="comment"></div> </body> </html>

Below is image.php

<?php

include 'connect.php';

$id = $_GET['id'];

if(!isset($id) || empty($id)){
die("Please select your image!");
}else{

$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];
$size = $row['size'];
$type = $row['type'];
$name = $row['name'];

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;
mysql_close();
}
?> 

Below is com_text.php

<?php

include 'connect.php';

$id = $_GET['id'];

if(!isset($id) || empty($id)){
die("Please select your comment!");
}else{

$query = mysql_query("SELECT * FROM tbl_comments WHERE imageId=".$id);
mysql_close();

while($result = mysql_fetch_array($query)) {
echo " <b>Name</b> = "    .$result["name"]    . " <br>";
echo " <b>Text</b> = "    .$result["text"]    . " <br>";
echo " <b>Date</b> = "    .$result["date"]    . " <br>";
echo "<hr>";
}
}
?> 

The attachment shows how it looks in the browser (even if the colors are distorted)

When I run index.php the image displays just fine, and I can change it with the next/prev buttons, but the text doesn't display properly at all!
If I run com_text.php?id=2 manually with the browser, I get the desired results

What am I doing wrong?
Why does the image change, when the text refuses to change?

    This line
    document.getElementById('comment').innerHTML= "com_txt.php?id="+i;

    will just load the text "com_txt.php?id="+i; into the div.
    You will need to do a http request for the data and put the result in the div.

    Search for AJAX to get the suitable JS for this.

      Might be a stupid question but, why doesn't it work? I mean, it works for changing a variable, src, in an <img>, shouldn't it work to change the value variable in, for example, a text field? (I tested it, and it didn't work, but I don't understand why it doesn't work).

      Isn't "ajax" an entire system of commands and so on, that is complicated to use?

        innerHTML is what it says: inner html. if you use somediv.innerHTML = 'hey', you'll get:

        <div>hey</div>

        you can't just put a URL in it and hope for it to become AJAX. 🙂

        you'll need an Xmlhttprequest for that.

        Basically, if you want the functionality you're looking for, learn about ajax, OR load up all comments in a javascript array by combining PHP in the script and loop through it from there.

          Hmm, I guess I have to give Ajax a try.

          Desdinova;10892036 wrote:

          Basically, if you want the functionality you're looking for, learn about ajax, OR load up all comments in a javascript array by combining PHP in the script and loop through it from there.

          You mean, read all entries from the DB at once, and then simply do the searches etc on the fetched data?

            Yes you could store them in a Javascript Array when loading the page, and think of something funky to load the right one into the div innerHTML, depending on which image is shown. So say you're showing image number 3, that you call the 3rd comment from your array.

              Desdinova;10892128 wrote:

              Yes you could store them in a Javascript Array when loading the page, and think of something funky to load the right one into the div innerHTML, depending on which image is shown. So say you're showing image number 3, that you call the 3rd comment from your array.

              While it's a good idea, I don't like a solution that downloads a lot of data that isn't used.

              I found an Ajax-script that worked:

              function ajaxFunction(){
              	var ajaxRequest;  // The variable that makes Ajax possible!
              
              try{
              	// Opera 8.0+, Firefox, Safari
              	ajaxRequest = new XMLHttpRequest();
              } catch (e){
              	// Internet Explorer Browsers
              	try{
              		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
              	} catch (e) {
              		try{
              			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
              		} catch (e){
              			// Something went wrong
              			alert("Your browser broke!");
              			return false;
              		}
              	}
              }
              // Create a function that will receive data sent from the server
              ajaxRequest.onreadystatechange = function(){
              	if(ajaxRequest.readyState == 4){
              		var ajaxDisplay = document.getElementById('comment');
              		ajaxDisplay.innerHTML = ajaxRequest.responseText;
              	}
              }
              ajaxRequest.open("GET", "comment.php?id="+i, true);
              ajaxRequest.send(null); 
              }

                When I load a new image, I use this function:

                function loadImg(){
                    newImg=new Image();
                    newImg.src="image.php?id="+i;
                    document.imgSrc.src = newImg.src
                    document.getElementById('pnum').innerHTML = i+' av '+maxSize;  
                newWidth= newImg.width; if (newWidth==0){ alert("Picture Error"); } document.imgSrc.style.width=newWidth*(1+sizeVar/4); ajaxFunction(); }

                SizeVar is a variable that is meant to change the zoom of the picture. My initial intention was to use percentages, but that only works for the first picture, the rest of the pictures becomes strange. The problem I've experienced is that this function doesn't seem to load the image properly before continuing, meaning the new picture will have its width saved as 0, which creates an error.
                Is there any way around this problem? I want the image to be fully loaded before the width is set.

                When I return to an image (e.g. when the image is already loaded in the webbrowser cache) the function works great, it's just when I load a new image it sets the size to 0.

                When I try:

                <img name="imgSrc" width="50%" id="imgSrc" alt="Picture not loaded"></img>

                The first file works fine, but the second img file I load gets the width 632, regardless of what the width actually is.

                Did that make any sense at all?

                And well, do anyone have a possible solution?

                  ophe;10892180 wrote:

                  While it's a good idea, I don't like a solution that downloads a lot of data that isn't used.

                  agreed. 🙂 I'd go for quality solutions too, but those take so much longer to explain.

                    Desdinova;10892184 wrote:

                    agreed. 🙂 I'd go for quality solutions too, but those take so much longer to explain.

                    However, judging by your other problem.. How about loading in all data in an array, as well as image src, and image size. I don't know how much photo's we're talking here, but I think it doesn't have to be that much data really.

                    You could of course do that with Ajax as well.

                      Desdinova;10892185 wrote:

                      However, judging by your other problem.. How about loading in all data in an array, as well as image src, and image size. I don't know how much photo's we're talking here, but I think it doesn't have to be that much data really.

                      You could of course do that with Ajax as well.

                      My plan is to make a photo-gallery, meaning there might be several MBs of pictures, so that isn't a viable solution, I'm afraid.

                        The pictures themselves wouldn't be preloaded, so that's ok. All you need to load up is sources and comments. This should be a couple of KB, unless you're talking 1000s of pictures. The images will be loaded when the src is changed.

                          Desdinova;10892192 wrote:

                          The pictures themselves wouldn't be preloaded, so that's ok. All you need to load up is sources and comments. This should be a couple of KB, unless you're talking 1000s of pictures. The images will be loaded when the src is changed.

                          That doesn't solve the problem with the size of the images.

                          I think part of the error works like this:

                          I load an image, the variables (such as size etc) gets set.
                          I replace the SRC of the image, the variables doesn't update unless the image is in the cache already

                            Yes, but if you know the image dimensions, you could easily calculate what size you should set right? though not in percentages, but in pixels.

                            I don't recommend resizing in-browser, since images will not render smoothly.

                              Desdinova;10892252 wrote:

                              Yes, but if you know the image dimensions, you could easily calculate what size you should set right? though not in percentages, but in pixels.

                              I don't recommend resizing in-browser, since images will not render smoothly.

                              Once the image is loaded, setting the right size isn't a problem. The problem is that, when I use the script above, the script above doesn't detect the size the first time I fetch the image from the database, but it does work the second time I try to watch the image.

                              function loadImg(){
                                  newImg=new Image();
                                  newImg.src="image.php?id="+i;
                                  document.imgSrc.src = newImg.src
                                  document.getElementById('pnum').innerHTML = i+' av '+maxSize;  
                              newWidth= newImg.width; if (newWidth==0){ alert("Picture Error"); } document.imgSrc.style.width=newWidth*(1+sizeVar/4); ajaxFunction(); }

                              The resizing in the browser wont cause that much problem. My intention is to limit the images to about 2000x2000 pixels (about 100kb) but I want it to be possible to watch the images with a few different size-settings, so that people with smaller monitors can look at them without a problem.

                                If you are changing one <img> tag, you don't want to create a new image, just change the current image.

                                  HalfaBee;10892307 wrote:

                                  If you are changing one <img> tag, you don't want to create a new image, just change the current image.

                                  The problem is the resizing of images. If I resize an image, and then load a new image, the new image gets the wrong size. Because of this I tried to first read the width from the new image I wanted to load, and then apply it with a zoom-modifier to the image.

                                  % changes to each image doesn't seem to work, because it doesn't update when I change the image, that is why I reapply the size change every time. I might be making some kind of mistake when I use %; example:
                                  If you go here:
                                  http://www.w3schools.com/js/tryit.asp?filename=try_dom_image_height

                                  And paste the code:

                                  <script type="text/javascript">
                                  function changeSize()
                                  {
                                  document.getElementById("compman").style.width="50%";
                                  }
                                  </script>
                                  
                                  <img id="compman" src="compman.gif"/>
                                  <input type="button" onclick="changeSize()" value="Change Size">
                                  

                                  Why do the image increase in size, when I tell it to be 50%??!?!?

                                  I believe that if I manage to solve that problem with %, I wont have any problems with the size of my images.

                                    Do you need to use percentages? Couldn't you specify the new dimensions in pixels instead? Setting the width to "50%" doesn't mean "50% of the image's current width" or "50% of the image's original width". It means "50% of the width of the containing block", exactly as if you'd written <img style="width:50%">. Usual CSS rules, in other words.

                                    ophe wrote:

                                    the second img file I load gets the width 632, regardless of what the width actually is.

                                    So I'm guessing your window is 1264 pixels wide (things like margins and browser chrome notwithstanding). See what happens if you resize that.

                                      Weedpacket;10892424 wrote:

                                      Do you need to use percentages? Couldn't you specify the new dimensions in pixels instead? Setting the width to "50%" doesn't mean "50% of the image's current width" or "50% of the image's original width". It means "50% of the width of the containing block", exactly as if you'd written <img style="width:50%">. Usual CSS rules, in other words.
                                      So I'm guessing your window is 1264 pixels wide (things like margins and browser chrome notwithstanding). See what happens if you resize that.

                                      Aah, yes, you are right, the image scales with the window. I understand why it behaves like it does now. Thank you!

                                      The reason I prefer percentages in my solution is because I don't want to make pictures larger than they are. For example, I don't want a picture of 400px appear be scaled to 600px, and if it shows as... say 50%, that would never happen.

                                      Is it possible to simply edit the % size of the image instead of just editing the block?
                                      (I realize that my solution doesn't do it, but is there another solution?)

                                        Write a Reply...