home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / jsiface.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  4.2 KB  |  156 lines

  1. Javscript Interface 
  2.  
  3. I'm starting to create some objects to grab an array (2 column) from mysql and put it into an equivalent javscript array. This is my first cut. This example requires a mysql database named foods, with a 2 column table in it named FRUIT. The table has two columns: FRUIT_ID, int(11) and FRUIT_NAME, varchar(25). Comments/ Improvements are welcome. 
  4.  
  5.  
  6. <?php 
  7.  
  8. // generic helpful error handling stuff 
  9. function log_exit( $message, $line, $file ) 
  10.     error_log($message.  " line: ". $line. " in ". $file, 0); 
  11.     exit; 
  12.  
  13. // define a class sArray2D, that creates a server side php Array 
  14. class sArray2D 
  15.    var $elem;     // actual array 
  16.    var $num_elem = 0; 
  17.  
  18.     // add elements to end of array 
  19.    function push( $a ) 
  20.    { 
  21.       $this->elem[ $this->num_elem ] = $a ; 
  22.       $this->num_elem++; 
  23.    } 
  24.  
  25.    function retrieve( $a ) 
  26.    { 
  27.       return( $this->elem[ $a ]); 
  28.    } 
  29.  
  30.    function rows() 
  31.    { 
  32.      return( $this->num_elem ); 
  33.    } 
  34.  
  35.    function cp2client( $js_arrayname ) 
  36.    { 
  37.       print( "<SCRIPT LANGUAGE=Javascript1.2>\n"); 
  38.     
  39.       print( $js_arrayname. "= new Array();" ); 
  40.       for( $i = 0; $i < $this->num_elem ; ++$i){ 
  41.      print($js_arrayname. "[$i]=\"".$this->elem[ $i ]. "\";\n") ; 
  42.       }     
  43.       print( "</SCRIPT>\n"); 
  44.    } 
  45.  
  46.    function cp_mysql_2col_table( $query_handle ) 
  47.    { 
  48.       for( $i = 0; $i<mysql_num_rows( $query_handle ); ++$i){ 
  49.         if( ! $row=mysql_fetch_array( $query_handle )) 
  50.          log_exit( "Could not execute iteration $i of db query", __LINE__, __FILE__); 
  51.         $this->push($row[1]); 
  52.       }   
  53.    }  
  54.  
  55. ?> 
  56.  
  57. === end of include 
  58.  
  59. == here is index.php3 
  60. <?php 
  61. require  "phplib/stdutils.inc" ;   
  62.  
  63. // server side stuff 
  64.  
  65. // conn to server 
  66. if(($mysql_link=mysql_connect( "localhost",  "scott",  "tiger")) == FALSE) 
  67.     log_exit( "Could not connect to MySQL", __LINE__, __FILE__); 
  68.  
  69.  
  70. //connect to correct database 
  71. if( ! mysql_select_db( "foods", $mysql_link) ) 
  72.     log_exit( "Could not connect to database", __LINE__, __FILE__); 
  73.  
  74. // setup query handle 
  75. if( ! $qh = mysql_query( " select * from FRUIT " )) 
  76.     log_exit( "Could not setup db query", __LINE__, __FILE__); 
  77.  
  78. $desserts = new sArray2D ; 
  79. $desserts->cp_mysql_2col_table( $qh ); 
  80.  
  81. // END PHP (most server side stuff) 
  82. // ======= 
  83. ?> 
  84.  
  85. <HTML> 
  86. <head> 
  87. <title>Test of JavaScript form object</title> 
  88.  
  89. <SCRIPT LANGUAGE=Javascript1.2> 
  90.  
  91. function bForm( arr ) 
  92.     function resettable( rflag ) 
  93.     { 
  94.        if ( rflag == "Y" || rflag == "y" || rflag == "t" || rflag == "T" ) 
  95.            this.rflag = 1; 
  96.        else 
  97.            this.rflag = 0; 
  98.     } 
  99.     this.resettable = resettable; 
  100.  
  101.     function radio( subvar ) 
  102.     { 
  103.         document.write("<FORM type='radio' name=choice size=40>\n"); 
  104.         for(i=0; i< arr.length; ++i){ 
  105.            document.write("<input type='radio' name="  
  106.               + subvar + " value='" + i + (i==0 ? "' checked " : "' ") + ">" +  
  107.            arr[i] + "<p>\n"); 
  108.         } 
  109.         document.write("<input type='submit' value='submit'><p>"+ 
  110.         ( this.rflag==1 ? "<input type='reset' value='reset'></FORM><p>" : "</FORM><p>" )); 
  111.     } 
  112.     // now make it a method 
  113.     this.radio = radio; 
  114.  
  115.     function select( subvar ) 
  116.     { 
  117.         document.write("<FORM type='SELECT'><SELECT name ='" + subvar + "'>\n"); 
  118.         for(i=0; i< arr.length; ++i){ 
  119.            document.write("<OPTION VALUE=" + i + ">" + arr[i]+ "\n"); 
  120.         } 
  121.         document.write("</SELECT><p>\n"); 
  122.         document.write("<input type='submit' value='submit'><p>"+ 
  123.         ( this.rflag==1 ? "<input type='reset' value='reset'></FORM><p>" : "</FORM><p>" )); 
  124.     } 
  125.     this.select = select; 
  126. </SCRIPT> 
  127.  
  128. </head> 
  129. <body> 
  130. <h1>Test of java form object</h1> 
  131.  
  132. <?php  $desserts->cp2client(  'fruitlist' );  ?> 
  133.  
  134. <SCRIPT LANGUAGE=Javascript> 
  135. // switch to client control now 
  136.  
  137. var FruitSelect = new bForm(fruitlist); 
  138. FruitSelect.resettable("Y"); 
  139.  
  140. FruitSelect.radio( "whichfruit" ); 
  141. // FruitSelect.select( "whichfruit" ); 
  142. </SCRIPT> 
  143.  
  144. <address> 
  145. <a href="mailto:apeda@interpublic.com"></a> 
  146. </address> 
  147. </body> 
  148. </HTML> 
  149.  
  150.