login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Overige scripts > [PHP5] MyForm V1.0

[PHP5] MyForm V1.0

Auteur: Stijn - 10 februari 2006 - 18:17 - Gekeurd door: nemesiskoen - Hits: 7383 - Aantal punten: 3.92 (6 stemmen)




Uitleg:

Met deze volledige class (enkel voor PHP 5x servers) kan je complexe en gewone formulieren ontwerpen. Zo kan je blijven in PHP werken ipv je PHP tag te sluiten een streepje code en dan in je HTML dan nog eens PHP zetten om vervolgens terug PHP te beginnen...

[PLUS]
Je kan sneller en meer werken, niet overtuigd? Heb je pech 


Installatie:

1) Je included de class file
2) je maakt een nieuw object aan ($obj = new MyForm(..); ) en je zet er de nodige parameters in -> zie class script!!!
3) Je maakt je formulier maar zonder HTML - TAGS ertussen, wanneer je deel of je formulier af is doe je $obj->ParseForm(); zodat je bijvoorbeeld een <BR> kan stoppen om nog een veld eronder te zetten.
4) Wanneer je helemaal gedaan hebt zet je op het einde dan $obj->EndForm();

Meer uitleg:

1) Zie voorbeeld bestand (kan geen voorbeeld online zetten omdat online servers op PHP4x draaien)
2) PM me gerust 

Have fun

Code:
MyForm.inc.php (class script)
  1. <?php
  2. /**
  3.  
  4. MyForm.INC.PHP
  5.  
  6. A EXTENDED CLASS THAT BUILD YOUR SITE
  7.  
  8. Writer: Stijn - [PSF]*B*oOsTer
  9. Date: 06 - 02 - 2006
  10. Team: PHP Source Fighters © 2006
  11.  
  12. Right 2 use this shit[?] and to share :-)
  13.  
  14. Have fun
  15. The [PSF]-Team
  16.  
  17.  
  18. **/
  19. ###################################
  20. ######### EVENTS LEGEND #########
  21. # {key} : {value} #
  22. ###################################
  23. # 0 : onclick #
  24. # 1 : ondblclick #
  25. # 2 : onkeydown #
  26. # 3 : onkeypress #
  27. # 4 : onkeyup #
  28. # 5 : onmousedown #
  29. # 6 : onmouseover #
  30. # 7 : onmousemove #
  31. # 8 : onmouseout #
  32. # 9 : onmouseup #
  33. # 10 : onreset #
  34. # 11 : onsubmit #
  35. ###################################
  36.  
  37. class MyForm
  38. {
  39. /**
  40. @var protected array Events
  41. @var protected array FormData
  42. @var protected array enctypes
  43. @var protected empty F_Action
  44. @var protected empty F_Method
  45. @var protected empty F_Name
  46. @var protected empty F_Enctype
  47. */
  48. public $Events = array ( 'onclick' , 'ondblclick' , 'onkeydown' , 'onkeypress' , 'onkeyup' , 'onmousedown' , 'onmouseover' , 'onmousemove' , 'onmouseout' , 'onmouseup' , 'onreset' , 'onsubmit' );
  49. public $FormData = array ( );
  50. public $enctypes = array ( 'multipart/form-data' , 'text/plain' , 'application/x-www-form-urlencoded' );
  51.  
  52.  
  53. /**
  54. __construct( );
  55.  
  56. @param string action
  57. @param string method
  58. @param string name
  59. @param string enctype standard value: text/plain
  60. @param array events
  61. */
  62. public function __construct( $action , $method , $name , $enctype = 'multipart/form-data' , $events = array() )
  63. {
  64.  
  65. ( in_array( $enctype , $this->enctypes ) === false ) ? die('Your enctype is wrong') : '' ;
  66.  
  67. ( $method != 'GET' && $method != 'POST' ) ? die('Wrong method: POST <-> GET') : '';
  68.  
  69. ( !isset( $action ) || empty( $action ) ) ? die('Enter a action') : '';
  70.  
  71. $FormData = '<form action='.$action.' method='.$method.' name='.$name.' enctype='.$enctype;
  72.  
  73.  
  74.  
  75. if( count( $events ) > 0 ) {
  76.  
  77. foreach( $events as $key => $value ) {
  78.  
  79. $FormData .= ' ' . $this->Events[ $key ] . '=' . $value;
  80.  
  81. }
  82.  
  83. }
  84.  
  85. $FormData .= '>';
  86.  
  87. $this->FormData[] = $FormData;
  88.  
  89. return true;
  90. }
  91.  
  92.  
  93. /**
  94. InputText( );
  95.  
  96. @param string name
  97. @param string value
  98. @param int size
  99. @param int maxLength
  100. @param int readonly
  101. @param int disabled
  102. @param array events
  103. */
  104. public function InputText( $name , $value , $size = 15 , $maxLength = 999 , $readonly = 0 , $disabled = 0 , $events = array() )
  105. {
  106.  
  107. $FormData = '<input type=text name='.$name;
  108.  
  109. ( isset($value) && !empty( $value ) ) ? $FormData .= ' value='.$value : '';
  110.  
  111. ( isset($size) && !empty( $size ) && is_numeric( $size ) === true ) ? $FormData .= ' size='.$size : '';
  112.  
  113. ( isset($maxLength) && !empty( $maxLength ) && is_numeric( $maxLength ) === true ) ? $FormData .= ' maxLength='.$maxLength : '';
  114.  
  115. ( isset($readonly) && !empty( $readonly ) && is_numeric( $readonly ) === true ) ? $FormData .= ' readonly='.$readonly : '';
  116.  
  117. ( isset($disabled) && !empty( $disabled ) && is_numeric( $disabled ) === true ) ? $FormData .= ' disabled='.$disabled : '';
  118.  
  119.  
  120. if( count( $events ) > 0 ) {
  121.  
  122. foreach( $events as $key => $value ) {
  123.  
  124. ( isset( $key) && !empty( $key ) ) ? $FormData .= ' '.$this->Events[ $key ] . '=' . $value : '' ;
  125.  
  126. }
  127.  
  128. }
  129.  
  130. $FormData .= '>';
  131.  
  132. $this->FormData[] = $FormData;
  133.  
  134. return true;
  135.  
  136. }
  137.  
  138.  
  139. /**
  140. InputPassword( );
  141.  
  142. @param string name
  143. @param string value
  144. @param int size
  145. @param int maxLength
  146. @param int readonly
  147. @param int disabled
  148. @param array events
  149. */
  150. public function InputPassword( $name , $value , $size = 15 , $maxLength = 999 , $readonly = 0 , $disabled = 0 , $events = array() )
  151. {
  152.  
  153. $FormData = '<input type=password name='.$name;
  154.  
  155. ( isset($value) && !empty( $value ) ) ? $FormData .= ' value='.$value : '';
  156.  
  157. ( isset($size) && !empty( $size ) && is_numeric( $size ) === true ) ? $FormData .= ' size='.$size : '';
  158.  
  159. ( isset($maxLength) && !empty( $maxLength ) && is_numeric( $maxLength ) === true ) ? $FormData .= ' maxLength='.$maxLength : '';
  160.  
  161. ( isset($readonly) && !empty( $readonly ) && is_numeric( $readonly ) === true ) ? $FormData .= ' readonly='.$readonly : '';
  162.  
  163. ( isset($disabled) && !empty( $disabled ) && is_numeric( $disabled ) === true ) ? $FormData .= ' disabled='.$disabled : '';
  164.  
  165.  
  166. if( count( $events ) > 0 ) {
  167.  
  168. foreach( $events as $key => $value ) {
  169.  
  170. ( isset( $key) && !empty( $key ) ) ? $FormData .= ' '.$this->Events[ $key ] . '=' . $value : '' ;
  171.  
  172. }
  173.  
  174. }
  175.  
  176. $FormData .= '>';
  177.  
  178. $this->FormData[] = $FormData;
  179.  
  180. return true;
  181.  
  182. }
  183.  
  184.  
  185. /**
  186. InputHidden( );
  187.  
  188. @param string name
  189. @param string value
  190. @param int size
  191. @param int maxLength
  192. @param int readonly
  193. @param int disabled
  194. @param array events
  195. */
  196. public function InputHidden( $name , $value , $size = 15 , $maxLength = 999 , $readonly = 0 , $disabled = 0 , $events = array() )
  197. {
  198.  
  199. $FormData = '<input type=hidden name='.$name;
  200.  
  201. ( isset($value) && !empty( $value ) ) ? $FormData .= ' value='.$value : '';
  202.  
  203. ( isset($size) && !empty( $size ) && is_numeric( $size ) === true ) ? $FormData .= ' size='.$size : '';
  204.  
  205. ( isset($maxLength) && !empty( $maxLength ) && is_numeric( $maxLength ) === true ) ? $FormData .= ' maxLength='.$maxLength : '';
  206.  
  207. ( isset($readonly) && !empty( $readonly ) && is_numeric( $readonly ) === true ) ? $FormData .= ' readonly='.$readonly : '';
  208.  
  209. ( isset($disabled) && !empty( $disabled ) && is_numeric( $disabled ) === true ) ? $FormData .= ' disabled='.$disabled : '';
  210.  
  211.  
  212. if( count( $events ) > 0 ) {
  213.  
  214. foreach( $events as $key => $value ) {
  215.  
  216. ( isset( $key) && !empty( $key ) ) ? $FormData .= ' '.$this->Events[ $key ] . '=' . $value : '' ;
  217.  
  218. }
  219.  
  220. }
  221.  
  222. $FormData .= '>';
  223.  
  224. $this->FormData[] = $FormData;
  225.  
  226. return true;
  227.  
  228. }
  229.  
  230.  
  231. /**
  232. InputCheckbox( );
  233.  
  234. @param string name
  235. @param string value
  236. @param int size
  237. @param int maxLength
  238. @param int checked
  239. @param int readonly
  240. @param int disabled
  241. @param array events
  242. */
  243. public function InputCheckbox( $name , $value , $checked = 0 , $size = 15 , $maxLength = 999 , $readonly = 0 , $disabled = 0 , $events = array() )
  244. {
  245.  
  246. $FormData = '<input type=checkbox name='.$name;
  247.  
  248. ( isset($value) && !empty( $value ) ) ? $FormData .= ' value='.$value : '';
  249.  
  250. ( isset($size) && !empty( $size ) && is_numeric( $size ) === true ) ? $FormData .= ' size='.$size : '';
  251.  
  252. ( isset($maxLength) && !empty( $maxLength ) && is_numeric( $maxLength ) === true ) ? $FormData .= ' maxLength='.$maxLength : '';
  253.  
  254. ( isset($readonly) && !empty( $readonly ) && is_numeric( $readonly ) === true ) ? $FormData .= ' readonly='.$readonly : '';
  255.  
  256. ( isset($disabled) && !empty( $disabled ) && is_numeric( $disabled ) === true ) ? $FormData .= ' disabled='.$disabled : '';
  257.  
  258.  
  259. if( count( $events ) > 0 ) {
  260.  
  261. foreach( $events as $key => $value ) {
  262.  
  263. ( isset( $key) && !empty( $key ) ) ? $FormData .= ' '.$this->Events[ $key ] . '=' . $value : '' ;
  264.  
  265. }
  266.  
  267. }
  268.  
  269. ( isset($checked) && !empty( $checked ) && is_numeric( $checked ) === true ) ? $FormData .= ' checked=checked' : '';
  270.  
  271. $FormData .= '>';
  272.  
  273. $this->FormData[] = $FormData;
  274.  
  275. return true;
  276.  
  277. }
  278.  
  279.  
  280. /**
  281. InputRadio( );
  282.  
  283. @param string name
  284. @param string value
  285. @param int size
  286. @param int maxLength
  287. @param int checked
  288. @param int readonly
  289. @param int disabled
  290. @param array events
  291. */
  292. public function InputRadio( $name , $value , $checked = 0 , $size = 15 , $maxLength = 999 , $readonly = 0 , $disabled = 0 , $events = array() )
  293. {
  294.  
  295. $FormData = '<input type=radio name='.$name;
  296.  
  297. ( isset($value) && !empty( $value ) ) ? $FormData .= ' value='.$value : '';
  298.  
  299. ( isset($size) && !empty( $size ) && is_numeric( $size ) === true ) ? $FormData .= ' size='.$size : '';
  300.  
  301. ( isset($maxLength) && !empty( $maxLength ) && is_numeric( $maxLength ) === true ) ? $FormData .= ' maxLength='.$maxLength : '';
  302.  
  303. ( isset($readonly) && !empty( $readonly ) && is_numeric( $readonly ) === true ) ? $FormData .= ' readonly='.$readonly : '';
  304.  
  305. ( isset($disabled) && !empty( $disabled ) && is_numeric( $disabled ) === true ) ? $FormData .= ' disabled='.$disabled : '';
  306.  
  307.  
  308. if( count( $events ) > 0 ) {
  309.  
  310. foreach( $events as $key => $value ) {
  311.  
  312. ( isset( $key) && !empty( $key ) ) ? $FormData .= ' '.$this->Events[ $key ] . '=' . $value : '' ;
  313.  
  314. }
  315.  
  316. }
  317.  
  318. ( isset($checked) && !empty( $checked ) && is_numeric( $checked ) === true ) ? $FormData .= ' checked=checked' : '';
  319.  
  320. $FormData .= '>';
  321.  
  322. $this->FormData[] = $FormData;
  323.  
  324. return true;
  325.  
  326. }
  327.  
  328.  
  329. /**
  330. InputFile( );
  331.  
  332. @param string name
  333. @param string value
  334. @param int size
  335. @param int maxLength
  336. @param int readonly
  337. @param int disabled
  338. @param array events
  339. */
  340. public function InputFile( $name , $size = 15 , $maxLength = 999 , $readonly = 0 , $disabled = 0 , $events = array() )
  341. {
  342.  
  343. $FormData = '<input type=file name='.$name;
  344.  
  345. ( isset($size) && !empty( $size ) && is_numeric( $size ) === true ) ? $FormData .= ' size='.$size : '';
  346.  
  347. ( isset($maxLength) && !empty( $maxLength ) && is_numeric( $maxLength ) === true ) ? $FormData .= ' maxLength='.$maxLength : '';
  348.  
  349. ( isset($readonly) && !empty( $readonly ) && is_numeric( $readonly ) === true ) ? $FormData .= ' readonly='.$readonly : '';
  350.  
  351. ( isset($disabled) && !empty( $disabled ) && is_numeric( $disabled ) === true ) ? $FormData .= ' disabled='.$disabled : '';
  352.  
  353.  
  354. if( count( $events ) > 0 ) {
  355.  
  356. foreach( $events as $key => $value ) {
  357.  
  358. ( isset( $key) && !empty( $key ) ) ? $FormData .= ' '.$this->Events[ $key ] . '=' . $value : '' ;
  359.  
  360. }
  361.  
  362. }
  363.  
  364. $FormData .= '>';
  365.  
  366. $this->FormData[] = $FormData;
  367.  
  368. return true;
  369.  
  370. }
  371.  
  372.  
  373. /**
  374. InputImage( );
  375.  
  376. @param string name
  377. @param string src
  378. @param string alst
  379. @param int size
  380. @param int maxLength
  381. @param int readonly
  382. @param int disabled
  383. @param array events
  384. */
  385. public function InputImage( $name , $src , $alt = '' , $readonly = 0 , $disabled = 0 , $events = array() )
  386. {
  387.  
  388. $FormData = '<input type=image name='.$name;
  389.  
  390. ( isset($src) && !empty( $src ) ) ? $FormData .= ' src='.$src : die('You have to enter a source!');
  391.  
  392. ( isset($alt) && !empty( $alt ) ) ? $FormData .= ' alt='.$alt : '';
  393.  
  394. ( isset($readonly) && !empty( $readonly ) && is_numeric( $readonly ) === true ) ? $FormData .= ' readonly='.$readonly : '';
  395.  
  396. ( isset($disabled) && !empty( $disabled ) && is_numeric( $disabled ) === true ) ? $FormData .= ' disabled='.$disabled : '';
  397.  
  398.  
  399. if( count( $events ) > 0 ) {
  400.  
  401. foreach( $events as $key => $value ) {
  402.  
  403. ( isset( $key) && !empty( $key ) ) ? $FormData .= ' '.$this->Events[ $key ] . '=' . $value : '' ;
  404.  
  405. }
  406.  
  407. }
  408.  
  409. $FormData .= '>';
  410.  
  411. $this->FormData[] = $FormData;
  412.  
  413. return true;
  414.  
  415. }
  416.  
  417.  
  418. /**
  419. InputButton( );
  420.  
  421. @param string name
  422. @param string value
  423. @param int disabled
  424. @param array events
  425. */
  426. public function InputButton( $name , $value , $disabled = 0 , $events = array() )
  427. {
  428.  
  429. $FormData = '<input type=button name='.$name;
  430.  
  431. ( isset($value) && !empty( $value ) ) ? $FormData .= ' value='.$value : '';
  432.  
  433. ( isset($disabled) && !empty( $disabled ) && is_numeric( $disabled ) === true ) ? $FormData .= ' disabled='.$disabled : '';
  434.  
  435.  
  436. if( count( $events ) > 0 ) {
  437.  
  438. foreach( $events as $key => $value ) {
  439.  
  440. ( isset( $key) && !empty( $key ) ) ? $FormData .= ' '.$this->Events[ $key ] . '=' . $value : '' ;
  441.  
  442. }
  443.  
  444. }
  445.  
  446. $FormData .= '>';
  447.  
  448. $this->FormData[] = $FormData;
  449.  
  450. return true;
  451.  
  452. }
  453.  
  454.  
  455. /**
  456. InputReset( );
  457.  
  458. @param string name
  459. @param string value
  460. @param int disabled
  461. @param array events
  462. */
  463. public function InputReset( $name , $value , $disabled = 0 , $events = array() )
  464. {
  465.  
  466. $FormData = '<input type=reset name='.$name;
  467.  
  468. ( isset($value) && !empty( $value ) ) ? $FormData .= ' value='.$value : '';
  469.  
  470. ( isset($disabled) && !empty( $disabled ) && is_numeric( $disabled ) === true ) ? $FormData .= ' disabled='.$disabled : '';
  471.  
  472.  
  473. if( count( $events ) > 0 ) {
  474.  
  475. foreach( $events as $key => $value ) {
  476.  
  477. ( isset( $key) && !empty( $key ) ) ? $FormData .= ' '.$this->Events[ $key ] . '=' . $value : '' ;
  478.  
  479. }
  480.  
  481. }
  482.  
  483. $FormData .= '>';
  484.  
  485. $this->FormData[] = $FormData;
  486.  
  487. return true;
  488.  
  489. }
  490.  
  491.  
  492. /**
  493. InputSubmit( );
  494.  
  495. @param string name
  496. @param string value
  497. @param int disabled
  498. @param array events
  499. */
  500. public function InputSubmit( $name , $value , $disabled = 0 , $events = array() )
  501. {
  502.  
  503. $FormData = '<input type=submit name='.$name;
  504.  
  505. ( isset($value) && !empty( $value ) ) ? $FormData .= ' value='.$value : '';
  506.  
  507. ( isset($disabled) && !empty( $disabled ) && is_numeric( $disabled ) === true ) ? $FormData .= ' disabled='.$disabled : '';
  508.  
  509.  
  510. if( count( $events ) > 0 ) {
  511.  
  512. foreach( $events as $key => $value ) {
  513.  
  514. ( isset( $key) && !empty( $key ) ) ? $FormData .= ' '.$this->Events[ $key ] . '=' . $value : '' ;
  515.  
  516. }
  517.  
  518. }
  519.  
  520. $FormData .= '>';
  521.  
  522. $this->FormData[] = $FormData;
  523.  
  524. return true;
  525.  
  526. }
  527.  
  528.  
  529. /**
  530. Select( );
  531.  
  532. @param string name
  533. @param int rows
  534. @param int multiple
  535. @param array options
  536. @param int disabled
  537. @param array events
  538. */
  539. public function Select( $name , $rows = 1 , $multiple = 0 , $options = array() , $disabled = 0 , $events = array() )
  540. {
  541.  
  542. $FormData = '<select name='.$name.' rows='.$rows;
  543.  
  544. ( $multiple === 1 ) ? $FormData .= ' multiple=multiple' : '';
  545.  
  546. ( isset($disabled) && !empty( $disabled ) && is_numeric( $disabled ) === true ) ? $FormData .= ' disabled='.$disabled : '';
  547.  
  548. $FormData .= '>';
  549.  
  550. //add options
  551. if( count( $options ) > 0 ) {
  552.  
  553. foreach( $options as $value ) {
  554.  
  555. /**
  556. * Every key has an array with the next values
  557.  
  558. ##########################
  559. ######### LEGEND #########
  560. ##########################
  561. # 0 : value #
  562. # 1 : text #
  563. # 2 : selected ( 0 | 1 ) #
  564. ##########################
  565. */
  566.  
  567. $FormData .= '<option value='.$value[0];
  568. ( $value[2] === 1 ) ? $FormData .= ' selected=selected' : '';
  569. $FormData .= '>';
  570. $FormData .= $value[1];
  571. $FormData .= '</option>';
  572.  
  573. }
  574.  
  575. }
  576.  
  577. if( count( $events ) > 0 ) {
  578.  
  579. foreach( $events as $key => $value ) {
  580.  
  581. ( isset( $key) && !empty( $key ) ) ? $FormData .= ' '.$this->Events[ $key ] . '=' . $value : '' ;
  582.  
  583. }
  584.  
  585. }
  586.  
  587. //close select
  588. $FormData .= '</select>';
  589.  
  590. $this->FormData[] = $FormData;
  591.  
  592. return true;
  593.  
  594. }
  595.  
  596.  
  597. /**
  598. ParseForm( );
  599.  
  600. */
  601. public function ParseForm()
  602. {
  603.  
  604. foreach($this->FormData as $value) {
  605. echo $value;
  606. }
  607.  
  608. $this->FormData = array( ); //make empty
  609.  
  610. }
  611.  
  612.  
  613. /**
  614. EndForm( );
  615.  
  616. */
  617. function EndForm()
  618. {
  619.  
  620. $this->FormData[] = '</form>';
  621.  
  622. foreach($this->FormData as $value) {
  623. echo $value;
  624. }
  625.  
  626. $this->FormData = array( ); //make empty
  627.  
  628. }
  629.  
  630. }
  631. ?>


Voorbeeld.php
  1. <?
  2. include('MyForm.inc.php');
  3.  
  4. $obj = new MyForm($_SERVER['PHP_SELF'] , 'POST' , 'testform');
  5.  
  6. $obj->InputText('TextField' , 'Stijn' , 17 , 999 , 1 );
  7. $obj->ParseForm();
  8.  
  9. echo "<br><br>";
  10. $obj->InputRadio('my_Text' , 'stijn' , 1, 40 , 999 , 0 , 0 , array('', 'alert("hallo");') );
  11. $obj->InputRadio('my_Text', 'jeroen');
  12. $obj->ParseForm();
  13.  
  14. echo "<br><br>";
  15. $obj->InputImage('Image', 'image.png' , 'test' , 50);
  16. $obj->ParseForm();
  17.  
  18. echo "<br><br>";
  19. $obj->InputButton('Button', 'KnopTekst' , 0 , array(6 => 'alert("goeiedag");'));
  20. $obj->ParseForm();
  21.  
  22. echo "<br><br>";
  23. $obj->Select('Select°1' , 5 , 1 ,
  24. array (
  25. array('WMP' , 'Windows Media Player' , 1) ,
  26. array('BG' , 'Bill Gates' , 0) ,
  27. array('MS' , 'Microsoft', 0 ),
  28. array('N44Bs' , 'Noobs' , 0)
  29. ));
  30.  
  31. $obj->EndForm();
  32. ?>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (5)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.083s