1

hello every one i want to pass the php array from the javascript function . please help me out regarding this code

if($check[0]!='')
{
$data=$check[0];
?>
<script type="text/javascript">
window.location="admin.php?page=add_service&$data=<?php echo $data ; ?> "
</script>
<?php
}

it works for the single variable but for array its not working

umar
  • 3,073
  • 5
  • 35
  • 45

5 Answers5

4

You can directly transform a PHP array into a native javascript array at page generation time by doing:

<?php
   $php_array = array('hi' => 'mom', 'good' => 'morning');
?>

<script type="text/javascsript">
   var js_array = <?php echo json_encode($php_array); ?>
</script>   

If you need to pass the array from PHP to javascript sometime AFTER the page was generated and sent to the client, then you'll need to use AJAX, preferably via a toolkit such as jquery or mootools.

Marc B
  • 356,200
  • 43
  • 426
  • 500
2

Do something like this:

function array_to_url_params($data){
  $params = array();
  foreach($data as $value)    
    $params[] = "data[]=".urlencode($value);

  return join("&", $params);
}

<script type="text/javascript">
window.location="admin.php?page=add_service&<? echo array_to_url_params($data); ?> "
</script>
Adrian Serafin
  • 7,665
  • 5
  • 46
  • 67
  • 1
    What if url contains quotes? `"` is perfectly acceptable in a URL, and will break the javascript. Skip the entire loop/build-array stage and just use ``. That'll take care of escaping any javascript metacharacters AND transform it into native javascript automatically. – Marc B Mar 23 '11 at 14:40
  • php already have built in function for this – Your Common Sense Mar 23 '11 at 14:44
1

you can not cast an array as a string, you can use serialize(); or $data['element which you want']

  • Thanks for your concern . yeah i can pass the single element but i want too pass entire array . can you guide me what do you mean by serialize() and how can i do it – umar Mar 23 '11 at 14:35
  • 2
    And then you'd have to write a PHP-style unserialize() for javascript. Use json_encode instead - directly transform a PHP array into a native Javascript array. – Marc B Mar 23 '11 at 14:35
  • 1
    $serialized = serialize($data); $data = unserialize($_GET['data']); – Tufan Barış Yıldırım Mar 23 '11 at 14:36
  • Yes, but the OP wants the array to be sent to javascript. So now you've converted a PHP array into a PHP serialized text string, and it still can't be used in javascript. – Marc B Mar 23 '11 at 14:39
  • Thanks to all for a great help ! – umar Mar 23 '11 at 15:05
1

You can do it with AJAX. AJAX allows you to get content without reloading page. Also to get the content you will need special PHP file that will return data to AJAX script on your site. You should read about it:

w3Schools

JSON can also help you here, it can rperesent an array in easy string, yo ucan encode and decode it on both PHP and JS.

Robik
  • 6,047
  • 4
  • 31
  • 41
1

Array is a variable but you can't interchange variables between programs but only string values.

So, in case of passing data vis query string with HTTP request, you have to encode your array somehow. two possible solutions would be

First:

$data = urlencode(serialize($data));

but it will require $data = unserialize($_GET['data']) upon receiving.

But I'd prefer another one, utilizing http_build_query() function

$data = http_build_query(array('data'=>$data));

and then your code will looks like

<script type="text/javascript">
window.location="admin.php?page=add_service&<?php echo $data ?>"
</script>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345