views:

62

answers:

1

Hey. Basically it's just:

 success: function(msg){
alert(msg);
}

What comes out alert´s. But is it possible for me if i have a var in the file that the ajax call:

$filename = time() . "10";

to use in success?

So i could do

 success: function(msg){
alert($filename);
}

(now its not correct) but how can i do this?

    $.ajax({
           type: "POST",
           url:"functions.php?action=crop",
           data: {x: $('#x').val(),y: $('#y').val(),w: $('#w').val(),h: $('#h').val(),fname:$('#fname').val(),fixed:fixed,size:size},
           success: function(msg){
               if(!fixed)
                    $("#crop_preview").css({overflow:"auto"})
               $("#crop_preview").html($(document.createElement("img")).attr("src", msg.filename)).show();
           $("#crop_preview").after("Here is your Cropped Image :)").show();
           $("#image").slideUp();
           }
         });

And php:

    $time = time().".jpg";
    echo '(';
echo json_encode(array(
    'filename'=>$time
));
echo ')';
+5  A: 

Use PHP json_encode, to return full objects to client:

echo '(';
echo json_encode(array(
    'filename'=>'anything',
    'var2'=>'something',
));
echo ')';

and use jquery's getJSON instead of normal get, or make a post/json request:

$.ajax({
    type: "POST",
    dataType: 'json',
    url:"functions.php?action=crop",
    success: function(response){
        alert(response.filename);
        alert(response.var2);
    }
.....
aularon
im using ajax so how should i do with the postJSON? Please check updated question
Karem
As you can see at the script above i tried do msg.filename but no results
Karem
I edited my answer, update and check.
aularon
My other stuff isnt working now in success, and im not getting an alert, in firebug response: ({"filename":"anything"})1
Karem
is your current php/javascript the same in the current edit of the question? also, where did that `1` come from? (your latest comment, after `({"filename":"anything"})`)
aularon
removed echo 1 which where at the bottom of the function file. Works great now, thank you for teaching me
Karem
You are welcome anytime : )
aularon