All pastes #566802 Raw Edit

Untitled

public text v1 · immutable
·published 2007-06-14 17:05 UTC
rendered paste body
<?php

function drupal_to_js($var) {
  switch (gettype($var)) {
    case 'boolean':
      return $var ? 'true' : 'false'; // Lowercase necessary!
    case 'integer':
    case 'double':
      return $var;
    case 'resource':
    case 'string':
      return '"'. str_replace(array("\r", "\n", "<", ">", "&"),
                              array('\r', '\n', '\x3c', '\x3e', '\x26'),
                              addslashes($var)) .'"';
    case 'array':
      // Arrays in JSON can't be associative. If the array is empty or if it
      // has sequential whole number keys starting with 0, it's not associative
      // so we can go ahead and convert it as an array.
      if (empty ($var) || array_keys($var) === range(0, sizeof($var) - 1)) {
        $output = array();
        foreach ($var as $v) {
          $output[] = drupal_to_js($v);
        }
        return '[ '. implode(', ', $output) .' ]';
      }
      // Otherwise, fall through to convert the array as an object.
    case 'object':
      $output = array();
      foreach ($var as $k => $v) {
        $output[] = drupal_to_js(strval($k)) .': '. drupal_to_js($v);
      }
      return '{ '. implode(', ', $output) .' }';
    default:
      return 'null';
  }
}
if($_GET['y'] == 'TRUE') {
  $a = array();
  switch($_GET['x']) {
    case 'one':
      $a['a'] = 'A';
      $a['b'] = 'B';
      $a['c'] = 'C';
      break;
    case 'two':
      $a['four'] = '4';
      $a['five'] = '5';
      $a['six']  = '6';
  }
  exit(drupal_to_js($a));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <script type="text/javascript" src="misc/jquery.js"></script>
    <script type="text/javascript" src="misc/drupal.js"></script>
    <script type="text/javascript" charset="utf-8">
    $(function() {
      $('#t').hide();
      $('#adf').bind('change', function() {
        if($('#adf option:selected').val() != 's'){
          $.get('/dmitrizone_theme/pwolanin.php', {x: $('#adf option:selected').val(), y: 'TRUE'}, function(data) {
            d = Drupal.parseJson(data);
          
            $('#t').empty().show();
            for(i in d) {
              $('#t').append('<option value="'+ i +'">'+ d[i] +'</option>');
            }
          });
        }
        else {
          $('#t').hide();
        }
      });
    });
    </script>
  </head>

  <body>
    <form>
      <select id="adf">
        <option value="s">Select one</option>
        <option value="one">One</option>
        <option value="two">Two</option>
      </select>
      <select id="t">
      </select>
    </form>
  </body>
</html>