John Higgins
John Higgins

Reputation: 907

Passing a variable to an Ajax call

I am using the code below to rotate and save an image stored on the server.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://beneposto.pl/jqueryrotate/js/jQueryRotateCompressed.js"></script>

<img src="uploads/101822973264163759893-1428320267361.jpeg" id="img"/>

<script> 
$(document).ready(function(){
var value = 0
    $("#img").rotate({ 
       bind: 
         { 
            click: function(){
                value +=90;
                $(this).rotate({ animateTo:value})
            }
         } 

    });


    $(document).on('click', '#img', function() {
                // alert("Hello");

                var idno = "test";
                $.ajax({
                         url: "saveimage.php",
                         dataType: "html",
                         type: 'POST',
                         data: "panelid="+idno, //variable with data
                         success: function(data){
                            // $(".test").html(data);
                                // alert(data);
                         }
                });
    });
});
</script>

saveimage.php

<?Php
$degrees = -90;  //change this to be whatever degree of rotation you want

header('Content-type: image/jpeg');

$filename = 'uploads/101822973264163759893-1428320267361.jpeg';  //this is the original file

$source = imagecreatefromjpeg($filename) or notfound();
$rotate = imagerotate($source,$degrees,0);

imagejpeg($rotate,$filename); //save the new image

imagedestroy($source); //free up the memory
imagedestroy($rotate);  //free up the memory

?>

I found this code here - http://www.codehaven.co.uk/rotate-and-save-an-image-using-php/.

This all works fine but I am trying to pass a PHP variable which holds the image path and name to the ajax call and have no idea on how to do this.

Can anyone help?

thanks,

John

Upvotes: 1

Views: 43

Answers (1)

Daniel F
Daniel F

Reputation: 73

You can pass the variable as URL parameters:

url: "saveimage.php?path=/folder/to/your/image/&image=image_name",

You can use urlencode to make sure all the special characters are pass through

Upvotes: 1

Related Questions