1

I want to return an array from a php function to my ajax call. After that I want to use the array values from the page the ajax call is made.

So this is my ajax call:

        $(function() {
        $("#find").click(function() {

            var url = $("#form_url").val();
            var dataString = 'url=' + url;

            $.ajax({
                type: "POST",
                url: "/ajax/add_url.php",
                data: dataString,
                }).done(function( result ) {
                    myresult(result);
            });

            return false;
        });
    });


 function myresult(result) {
var result_lines = result.split("<splitter>");    
if (result_lines[0] == '1') { 
    $('#content_error').html(result_lines[1]).fadeIn(250);
    $('#content_error').delay(1500).fadeOut(500);
} else if (result_lines[0] == '2') { 
    $('#content_success').html('Succesfully get images').fadeIn(250);
    $('#url_result').delay(500).fadeIn(500);
    $('#content_success').delay(1500).fadeOut(500);
    alert(eval(data));
}
return true;   

}

and this is my php script:

 if($_POST['url']) {

    $url = $Db->escape($_POST['url']);

        $html = file_get_html($url);
        $count = 0;
        $goodfiles = array();

        foreach($html->find('img') as $element) {

            $pic = url_to_absolute($url, $element->src);

            if(!empty($pic)){

                $pics = parse_url($pic);
                list($width, $height, $type, $attr) = getimagesize($pic);

                if($pics["scheme"]=="http" && $width >= 300 && $height >= 250) {

                    array_push($goodfiles,$pic);
                    $_SESSION['pictures'] = $goodfiles;
                    $count++;

                }
            }
      }


        if($count == 0){ 

            $_SESSION['count'] = 'empty'; 
            echo "1<splitter>"; 
            echo "No items found with the correct size"; 


        }else{

            $_SESSION['count'] = $count;
            echo "2<splitter>";
            echo json_encode($_SESSION['pictures']); 

        }

            $_SESSION['url'] = $url;
            $html->clear();
            $empty = 1;
    }

  }

when the ajax call is successful I use json_encode on the array to use it on my php page. But I don't know how I get this array to a javascript on the page the ajax call was made of.

right now I'm receiving the following content:

["image.jpeg","image.jpg"]

And I want to put this in a javascript array...

2
  • You should only have one echo in your PHP function Commented Aug 18, 2012 at 23:02
  • Firebug shows you the request and the response when an ajax call is made. You might find that very useful. Commented Aug 18, 2012 at 23:05

2 Answers 2

2

The error is this with this line:

var result_lines = result.split("<splitter>");

result (the AJAX response) is an object or array (depending on the nature of your JSON) but you are trying to call a string method (split()) on it.

This would cause an error in your JS console - always check the console.

Finally, eval() is evil and almost never required except in exceptional circumstances. Try to avoid it.

Sign up to request clarification or add additional context in comments.

6 Comments

The splitter is to determine an error (splitter 1) or a successful call (splitter 2). You can call the results by using the function result lines. So calling the json_encode echo can be done by using $('#id').html(result_lines[1]).... But is html the right way?! And how do I place the array in a javascript then?!
Not sure I follow. The fact remains result is an object or array, neither of which has a method split(). Are you saying result is NOT an object or array, but is coming back as a string? jQuery normally identifies JSON responses and parses it as such. You can enforce this by adding a param to your AJAX request: dataType: 'json'
If it's an array then my answer stands - arrays have no method split(). If it's already an array then you don't need to do anything - it's already usable, e.g. result[0] will get the first key of the array. (If you're sure it's an array and not an object).
I want to put the output of json_encode($test) in a javascript var, so I can use it in a javascript. So for example: Var images = ["image.jpg", "image.jpg"] Right now json_encode($test) gives me ["image.jpg", "image.jpg"], so that's oke. Now I need to know how to put this output in the var images. A var doesn't have an id or class, so I don't know how to put it there....
But you've already got it in a var - result. That is the parsed result of what your PHP output.
|
0

I don't know how to work with $.ajax but here is an alternative.

Replace this

$.ajax({
                type: "POST",
                url: "/ajax/add_url.php",
                data: dataString,
                }).done(function( result ) {
                    myresult(result);
            }); 

with

$.post("/ajax/add_url.php",{dataString:dataString},function(data){
           alert(data['you array index']);
},'json')

I repeat ,this is my alternative so don't take it hard!

1 Comment

Why would that change anything?

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.