tags:

views:

78

answers:

3

Hi. i'm looking for a php script server side that load an image in a div on client side.

first shot : ajax.php

if((isset($_POST['id'])) && ($_POST['id']=="loadphoto") && (ctype_digit($_POST['idp']))) {
    $query=mysql_query("SELECT articleid, photoid FROM articles_photos WHERE id='".$_POST['idp']."'", $mydb);
    if(mysql_num_rows($query)!=0){
        while($row=mysql_fetch_array($query, MYSQL_NUM)) {
            $path="./articles/photos/".$row[0]."/".$row[1];
        }

        echo $path;
    }
}

myjs.js

function loadPhoto(mexid) {
$.ajax({
    type: 'POST',
    cache: false,
    url: './auth/ajax.php',
    data: 'idp='+escape(mexid)+'&id=loadphoto',
    success: function(msg) {
        $('.articleviewphoto1').find("img").each(function(){
            $(this).removeClass().addClass('photoarts');
        });

        $('#'+mexid).removeClass().addClass('photoartsb');
        $('#visualizator').html('<img src="'+msg+'" class="photoartb" />');
    }
});
return false;

}

tnx for the helps

A: 

Using the PHP GD Library

YouBook
A: 

I'm assuming the image already exists somewhere and isn't being generated on the fly. In the PHP script, you can access the variables with $_POST['idp'] and $_POST['id']. Do whatever processing you need, and then simply echo the URL. The variable msg in the javascript will be everything echo'd by the PHP script. Then, you could just do this:

$('#visualizator').html('<img src="' + msg + '" />');
Tom Smilack
A: 

Are you looking to host a list of possible images in the PHP file and load different images based on the id you send? If so, you can set:

$Img = $_POST['idp'];

And use a switch statement to pass back the image file path:

switch ($Img){
  case (some_value):
      echo 'images/some_file.jpg';
      break;

  case (another_value):
      echo 'images/another_file.jpg';
      break;

}

Then place the file path in an tag and load that into $('#visualizator').html();

Am I on track at least with what you want? If so, I can flesh the answer out a bit.

d2burke
yeah! that's what i have to do. I have edited the post and put first example. what do you think about? tnx
markzzz
Well, there are a few options and parameters in there that I don't know the reason for, but that's to be expected now having the entire project to look at...but it looks like it will work just fine.
d2burke
I've a problem about this. I have the images as thumbs in a div (size max 100x100px trough a css rule) and the big photo in other div (size 700x700px trhough another css rule). the problem is that i load img (with size of 3-4mb, 2048x2112 for example), but with 5-6 photos with that size is too slow for the browser. The page is very slow. There is a solution? Maybe resize the image on server side when i upload them?
markzzz
d2burke
uhm, i found a manual about php GD. So i make the thumbs with the original photo ;) if it doesnt work, i try this! tnx man ;)
markzzz