WEB Mixing Javascript With PHP... Possible?

Discussion in 'OT Technology' started by brds, Aug 6, 2008.

  1. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,637
    Likes Received:
    12
    Location:
    Atlanta
    Hey guys, I have a question about javascript and php. I found a javascript script that checks to see if a link points to a valid image. If it does, it alerts "Link is good" if the link is invalid, it alerts "Link is no good".

    What i would like to have happen is, if the link is good, run a sql command in php. So, is there a way i can set a variable in javascript if the link is valid, and then use php to see if the variable is set... and run the sql command if it is.

    Here's the code:
    Code:
    $filelocation = $_POST['urladd'];
    $pieces = explode("/", $filelocation);
    $piecescount = count($pieces);
    $piecescount = $piecescount - 1;
    $filename = $pieces[$piecescount];
    echo "
        <script type='text/javascript'>
            img = new Image()
            img.onerror = function() {alert ('Site is bad.')}
            img.onload = function() {alert ('Site is good.')}
            img.src = '$filelocation'
        </script>
    ";
    $insertpicture = "INSERT INTO TABLE_NAME
        (
            VARIABLE1, 
            VARIABLE2, 
            VARIABLE3, 
            VARIABLE4
        )
        VALUES
        (
            '$filename', '$filelocation', 0, 0
        );";
    $connection->runQuery($insertpicture);
    
    Thanks in advance :wavey:
     
  2. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    make an AJAX call to a php page in your JS when your condition succeeds
     
  3. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,637
    Likes Received:
    12
    Location:
    Atlanta
    Do you have an example? i know very very very little javascript and no ajax. :wtc:
     
  4. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
  5. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    just add your params to you url, and pick them up on the php side
     
  6. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,637
    Likes Received:
    12
    Location:
    Atlanta
    but i'm trying to do it all dynamically... so the user has a form where they submit a url. the click the submit button, and the next page checks the link with the javascript i posted in the first post, and then (depending on the result of the javascript) either runs the php code or gives the user an error warning...
     
  7. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    I think you're complicating the problem. Why are you doing image validation in JScript? Just do it on the post of the php. Unless you got some crazy shit going on
     
  8. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    is the URL to the image on YOUR server or any server?
     
  9. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    you could do something like this instead:

    Code:
    <?php
        if([email protected]("URL TO IMAGE")){
            echo "bad image";
        }
        else{
            echo "good image";
        }
    ?>
    
    there's imagecreate functions for all kinds of filetypes, which you could tell by the file extension if you're doing more than jpg

    http://us2.php.net/manual/en/book.image.php
     
  10. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    if the posted image URLs are not on your server, and you have cURL installed, you can try this:

    Code:
    <?php
    	$filelocation = $_POST['urladd'];
    	$pieces = explode("/", $filelocation);
    	$filename = $pieces[count($pieces) - 1];
    	$ch = curl_init();
    		curl_setopt($ch, CURLOPT_URL, $filelocation);
    		curl_setopt($ch, CURLOPT_USERAGENT, "CURL");
    		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    		curl_setopt($ch, CURLOPT_NOBODY, 1);
    	$buffer = curl_exec($ch);
    	$filetype = curl_getinfo($ch, CURLINFO_CONTENT_TYPE );
    	curl_close($ch);
    	
    	if (substr($filetype, 0, 5)=='image')
    		$connection->runQuery("INSERT INTO TABLE_NAME (VARIABLE1, VARIABLE2, VARIABLE3, VARIABLE4) VALUES ('$filename', '$filelocation', 0, 0);");
    ?>
     
  11. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,637
    Likes Received:
    12
    Location:
    Atlanta
    Sled - I'm doing some crazy shit

    noon - Link is on my server, picture is elsewhere

    I'll try to explain best i can:
    I have a page that allows the user to submit a picture via upload or link submission. The upload part works just fine.
    If the user wants to submit a link to be held on to, i would like to test it first. So, the user types in a link, and hits the submit button. The form takes the user to the page where the link will be checked. The javascript listed in my first post checks the link. Right below that, still on the link checking page, i would like to (depending on the outcome of the javascript) either add the link to the database, or reject it.

    Hope that made sense :dunno:
     
  12. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    doesn't really make sense. what does 'the user wants to submit a link to be held on to' mean?
     
  13. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,637
    Likes Received:
    12
    Location:
    Atlanta
    Let's say, you see a picture on a site and you copy the url to the picture. Instead of hosting the actual picture on my site, you can submit the link where it's already being hosted. so, a user can have my site 'hold onto a link' for them if they dont want to upload the picture to my site but, at the same time, dont want to lose the picture.

    Did that help?
     
  14. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    what's wrong with just using the code i posted to test it? why do you have to use javascript?
     
  15. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    my code should work
     
  16. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,637
    Likes Received:
    12
    Location:
    Atlanta
    I'm not sure if it's because cURL might not be installed or what, but i changed the code to say "yes" if it worked and "no" if it doesn't, and i always get no. :wtc:

    Here's what i did, in case it helps:
     
  17. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,637
    Likes Received:
    12
    Location:
    Atlanta
    i just used the "getimagesize" function to see if the image sizes could be retrieved. If they are, then obviously the link is valid.
     
  18. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    curl just seems like overkill for this. here i added multiple extensions for the code i provided before and you can use this.

    Code:
    <?php
        $url = "http://i.cdn.turner.com/cnn/.element/img/2.0/global/nav/header/header_cnn_com_logo.gif";
        switch(strtolower(substr($url, -3))){
            case 'jpg':
                $img = @imagecreatefromjpeg($url);
                break;
            case 'gif':
                $img = @imagecreatefromgif($url);
                break;
            case 'png':
                $img = @imagecreatefrompng($url);
        }
        if(!$img)
            echo "no";
        else
            echo "yes";
    ?>
    that's probably even a better idea. although with mine you could edit the image and add watermarks or whatever, but i doubt you want to do that :mamoru:
     

Share This Page