0

Absolutely new to PHP and so far it isn't pretty. Anyway, I'm trying to pass a variable over to a PHP script, do a couple things with it, and pass it back to my Javascipt code.

Here's where I pass it off to PHP:

   var src=encodeURIComponent("http://www.someonlinesite.com/file.swf");
   $.ajax({
       url:'test.php?src='+src,
       dataType:'json',
       success:function(response){
           alert(response)
       }
   });

and here's the script:

<?php
   $src=isset($_GET['src'])?$_GET['src']:'';
   $size=getimagesize($src);
   echo json_encode(array('size'=>$size));
?>

I'm trying to pass the URL of a .SWF video file over to a small PHP script that will use getImagesize() to figure it's dimensions and pass them back.... but I'm not seeing anything in the response and the alert isn't firing.

What's going wrong?

UPDATE:

I've updated the code with the most recent - according to the advice from some nice SO members. When I hardcode the $src variable and navigate directly to the test.php it echoes everything perfectly. So, it looks like the PHP is working. However, it appears like either the callback is never firing or the PHP file isn't returning the data. In the console there still isn't anything in the response.

2 Answers 2

4

You need to concatenate your url string parameter in get():

$.get('test.php?src=' + src, function(data){
   alert(data);
});

And also, your src variable begins with a double quote and is closed with a single quote. That will cause issues.

var src="http://www.someonelinesite.com/file.swf";

Also, it's probably a bad idea to do this via $_GET since you are passing a URL. $_POST would be better or encode the URL before you pass it. The current url you are passing right now would look like this in a browser:

http://www.mysite.com/test.php?src=http://www.someonelinesite.com/file.swf

That's not pretty. Using encodeURIComponent(), your whole URL will end up looking like this:

http://www.mysite.com/test.php?src=http%3A%2F%2Fwww.someonelinesite.com%2Ffile.swf 

Edit to $.ajax

$.get above would work just fine, but going with the implementation of $.ajax works too:

$.ajax({
   url:'test.php',
   type: 'GET', //Add the type
   dataType:'json',
   data: {'src': src}, //Add the data, leave it out the url
   success:function(data){
       alert(data)
   }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Hmmm... that looks like exactly how I have it. The quote issue was a typo and was ok in my code. I don't need to worry about revealing the url since I'm just wanting to call the php file, have it get the dimensions, and return the dimensions - so the url wouldn't change at all. I've switched it to post and am getting the same results.
You fixed the concatenation part? You have 'test.php?src=src. You need to do this 'test.php?src=' + src
Yeah, that's fixed. Any in the debugger I can see it passing in the correct path to the file. If I run the script directly it works as well. For whatever reason though it just isn't firing the callback
Just did, but must have been doing it when you posted this since I overwrote the old code.
0

Try this :

In Jquery :

    var src="http://www.someonelinesite.com/file.swf";
    $.ajax({
       url:'test.php?src='+src,
       dataType:'json',
       success:function(response){
              alert(response.size);
       }
    });

In php

    $src=isset($_GET['src'])?$_GET['src']:'';  
    $size=getimagesize($src);
    echo json_encode(array('size'=>$size));

?>

6 Comments

Nope, tried this as well. If I navigate directly to the php file it pulls all the info I need. The problem is in passing that info back into the ajax request.
In jquery encode url with encodeURIComponent(src) function and passed to the ajax function
Just tried added the encoding to id, and can see the resulting character changes in the console, but still no response or callback being fired. There's been a bit of progress though so I'll update the question.
You alert an json object try this alert(response.size)
getimagesize function will return an array which contain the image height, width and mime type The server not allowing you to execute the getimagesize() may be in the configuration it is not allowed or use @getimagesize($src)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.