home *** CD-ROM | disk | FTP | other *** search
Wrap
Javscript Interface 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. <?php // generic helpful error handling stuff function log_exit( $message, $line, $file ) { error_log($message. " line: ". $line. " in ". $file, 0); exit; } // define a class sArray2D, that creates a server side php Array class sArray2D { var $elem; // actual array var $num_elem = 0; // add elements to end of array function push( $a ) { $this->elem[ $this->num_elem ] = $a ; $this->num_elem++; } function retrieve( $a ) { return( $this->elem[ $a ]); } function rows() { return( $this->num_elem ); } function cp2client( $js_arrayname ) { print( "<SCRIPT LANGUAGE=Javascript1.2>\n"); print( $js_arrayname. "= new Array();" ); for( $i = 0; $i < $this->num_elem ; ++$i){ print($js_arrayname. "[$i]=\"".$this->elem[ $i ]. "\";\n") ; } print( "</SCRIPT>\n"); } function cp_mysql_2col_table( $query_handle ) { for( $i = 0; $i<mysql_num_rows( $query_handle ); ++$i){ if( ! $row=mysql_fetch_array( $query_handle )) log_exit( "Could not execute iteration $i of db query", __LINE__, __FILE__); $this->push($row[1]); } } } ?> === end of include == here is index.php3 <?php require "phplib/stdutils.inc" ; // server side stuff // conn to server if(($mysql_link=mysql_connect( "localhost", "scott", "tiger")) == FALSE) log_exit( "Could not connect to MySQL", __LINE__, __FILE__); //connect to correct database if( ! mysql_select_db( "foods", $mysql_link) ) log_exit( "Could not connect to database", __LINE__, __FILE__); // setup query handle if( ! $qh = mysql_query( " select * from FRUIT " )) log_exit( "Could not setup db query", __LINE__, __FILE__); $desserts = new sArray2D ; $desserts->cp_mysql_2col_table( $qh ); // END PHP (most server side stuff) // ======= ?> <HTML> <head> <title>Test of JavaScript form object</title> <SCRIPT LANGUAGE=Javascript1.2> function bForm( arr ) { function resettable( rflag ) { if ( rflag == "Y" || rflag == "y" || rflag == "t" || rflag == "T" ) this.rflag = 1; else this.rflag = 0; } this.resettable = resettable; function radio( subvar ) { document.write("<FORM type='radio' name=choice size=40>\n"); for(i=0; i< arr.length; ++i){ document.write("<input type='radio' name=" + subvar + " value='" + i + (i==0 ? "' checked " : "' ") + ">" + arr[i] + "<p>\n"); } document.write("<input type='submit' value='submit'><p>"+ ( this.rflag==1 ? "<input type='reset' value='reset'></FORM><p>" : "</FORM><p>" )); } // now make it a method this.radio = radio; function select( subvar ) { document.write("<FORM type='SELECT'><SELECT name ='" + subvar + "'>\n"); for(i=0; i< arr.length; ++i){ document.write("<OPTION VALUE=" + i + ">" + arr[i]+ "\n"); } document.write("</SELECT><p>\n"); document.write("<input type='submit' value='submit'><p>"+ ( this.rflag==1 ? "<input type='reset' value='reset'></FORM><p>" : "</FORM><p>" )); } this.select = select; } </SCRIPT> </head> <body> <h1>Test of java form object</h1> <?php $desserts->cp2client( 'fruitlist' ); ?> <SCRIPT LANGUAGE=Javascript> // switch to client control now var FruitSelect = new bForm(fruitlist); FruitSelect.resettable("Y"); FruitSelect.radio( "whichfruit" ); // FruitSelect.select( "whichfruit" ); </SCRIPT> <address> <a href="mailto:apeda@interpublic.com"></a> </address> </body> </HTML>