0

I've created a PHP page that outputs a string variable from my database (this string simulate a multidimensional array) , I want to use this string as a variable in a jquery code (external JavaScript file). the problem that its not working.

I've tried:

var data = [$.get( "string.php" );];

how can i insert a PHP page output into a jquery variable ?

user2828251
  • 235
  • 2
  • 7
  • 21

1 Answers1

4

You probably want something like this:

$.get( "string.php", function(response){
    var data = response;
});

Seeing from your comment that you are expecting to get an Array as a response. You need to parse that. I suggest you make your response into a JSON object (this makes parsing a lot easier). Than you could just do :

var data = $.parseJSON(response);

And you will get an Array with objects.

To make your response JSON you could use json_encode to encode your Array.

putvande
  • 15,068
  • 3
  • 34
  • 50
  • its still not working. maybe i'll explain it more specific: i have this variable in jquery : var data = [{address:'sweden vaxjo' ,title:'test',description:'hi',area:'Kronoberg',category:'Activities',frequency:'One time',when:'17:00'},{address:'sweden kalmar' ,title:'hi',description:'hi',area:'Kalmar',category:'Activities',frequency:'One time',when:'hh'}];.... now the PHP page gives the same output = so how can i put this output in my script? – user2828251 Jan 10 '14 at 12:02
  • 1
    Looks like JSON. You want it to be an Array? – putvande Jan 10 '14 at 12:03
  • iv'e already build this Array manually (as a string) in my PHP page... – user2828251 Jan 10 '14 at 12:05
  • @user2828251 You should use [json_encode](http://uk3.php.net/json_encode) instead of writing it yourself. – Lexi Jan 10 '14 at 12:27