0

I'm using this script I found off the internet to detect a visitors web browser details.

That script gets run when I send a request via an ajax call.

As you can see at the bottom of the php script, there is an array,

  return array(
        'userAgent' => $u_agent,
        'name'      => $bname,
        'version'   => $version,
        'platform'  => $platform,
        'pattern'    => $pattern
    );

and this

$ua=getBrowser();
$yourbrowser=
 "Your browser: " . $ua['name'] . "
 " . $ua['version'] . " on " .$ua['platform'] . " reports: <br >" . $ua['userAgent'];

print_r($yourbrowser);

How can I pass each of those variables back to ajax in such a fashion I could do this.

$.ajax({                                      
 url: 'analyze.php',    
 data: "active=active",
 success: function(data)
  { 
  var userAgent = data[1];
  var name = data[2];
  var version = data[3];
   $("#div1").html(userAgent);
   $("#div2").html(userAgent);
   $("#div3").html(userAgent);
  }      
});
2
  • What do you get when you console.log(data)? Commented Oct 29, 2014 at 19:41
  • You could encode it as JSON data and return that. Commented Oct 29, 2014 at 19:44

4 Answers 4

3

You can echo the data as json:

echo json_encode($myArray);

Then add dataType:'json' and access the properties by name in javascript:

dataType: 'json',
success: function(data) { 
    var userAgent = data.userAgent;
    var name = data.name;
    var version = data.version;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Make sure to add dataType: 'json' to the AJAX call for this to work.
2

You have to encode the array as a json object

echo json_encode($array);

and then the ajax call will look like this:

$.ajax({                                      
url: 'analyze.php',    
data: "active=active",
dataType: "json",
success: function(data)
{ 
    var userAgent = data.userAgent;
    var name = data.name;
    var version= data.version;
    ....
}

2 Comments

@SLin Your variable $array, where's that come from? Am I supposed to store the array I posted at the top of my explanation inside $array? $array = array( 'userAgent' => $u_agent etc?
@AshleyBrown Yes, the array that you get from the script should be $array.
0

Instead of having the php script return an array, have it echo the info like this:

echo "$u_agent,$bname,$version,$platform,$pattern";

Then convert that to an array:

var dataArray = data.split(',');

Then you can access each variable like so:

var userAgent = dataArray[1];
var name = dataArray[2];
var version = dataArray[3];

Comments

0

In your .php file

header('Content-Type: application/json'); // Set the header type to json at the top of the file.

echo json_encode($array); // Echo out the array as a json-string.

In your ajax success function

function (data) {
  var foo = data.bar;
}

Comments

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.