fivestringsurf Posted October 15, 2009 Share Posted October 15, 2009 Hidy Ho Neighbors, I'm forcing myself to learn oop/classes for php5. It seems like a good idea for repetitious coding such as with forms. I've been through the oop tutorials here and elswhere in books.-if i see another Pet example..uhhg lol - and they do make sense but when trying to implement my own stuff it all falls apart. My pathetic, broken example below attempts at creating form text inputs and checking the inputs when submitted. And if submitted the "value=" attribute of the form is updated with the posted value thereby making the form "sticky". With procedural methods I have no problems creating vars dynamically from loops, checking and manipulating forms etc...but this object/class stuff has me stumped. <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); class Form { private $inp; private $flag; private $count; private $maxChar; public function Check_form(){ if (isset($_POST['form'])){ $d = array_pop($_POST);//remove "submit" value from $_POST array foreach($_POST as $key=>$value){ static $i=1; $inp = 'inp'.$i; $this->$inp = $value; $i++; } $this->count = count($_POST); return true; }else{ return false; } } function text_input($text='enter',$maxChar = '10') { if($this->Check_form()){ //make forms sticky (replace input values if filled out) echo 'form is set<br />';//test for($i=1; $i<=$this->count; $i++) { $text = $inp . $i; } } $this->maxChar = 'maxlength = ' .'"'.$maxChar.'"'; $maxChar = $this->maxChar; static $i=1; echo "<p><input type=\"text\" name=\"text_$i\" value=\"$text\" $maxChar /></p>"; $i++; } }// End of Form class. echo '<form action="oopForm2.php" method="post" enctype="multipart/form-data" name="theForm">'; $myForm = new Form(); $input_box1 = $myForm->text_input('enter text',20); $input_box2 = $myForm->text_input('enter more',20); echo '<input type="submit" name="form" value="submit" />'; echo '</form>'; unset($myForm); ?> It's terrible I know, some advice would be greatly appretiated..sample code? even better. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/177845-form-class-help-oop-php5/ Share on other sites More sharing options...
sKunKbad Posted October 16, 2009 Share Posted October 16, 2009 I learned PHP OOP from Larry Ullman's PHP5 Advanced book, but didn't really understand using it until working with it on a regular basis. I too was disgusted with the pet examples. PHP OOP is amazingly easy to work with when you finally understand. You ought to look at one or two of the popular php frameworks and see how they are handling the tasks of building, validating, and populating forms. Quote Link to comment https://forums.phpfreaks.com/topic/177845-form-class-help-oop-php5/#findComment-937767 Share on other sites More sharing options...
fivestringsurf Posted October 16, 2009 Author Share Posted October 16, 2009 funny you mentioned that...I am using Ulman's book currently. And I actually did take a look at some form classes i found online...the problem is they are so large and complicated I find them impossible to learn from. Like when i thought i could "learn php" by looking at a wordpress installation way back when...yeah good luck The only way I learned php was by hand typing simple stuff one line at a time building slowly and adding features to try to solve problems. I can't seem to do it with php5 classes and good resources are somwhat lacking. got any simple form examples? One of the classes didn't even use isset() but yet is seemed to work..so i just don't get it? Quote Link to comment https://forums.phpfreaks.com/topic/177845-form-class-help-oop-php5/#findComment-937798 Share on other sites More sharing options...
mikesta707 Posted October 16, 2009 Share Posted October 16, 2009 Why not try a simpler class problem? maybe a log in class, or a class for handling sql query data? Quote Link to comment https://forums.phpfreaks.com/topic/177845-form-class-help-oop-php5/#findComment-937799 Share on other sites More sharing options...
fivestringsurf Posted October 16, 2009 Author Share Posted October 16, 2009 That's just it, in procedural coding i thought a simple form parsing script was kind of simple so it would make a good place to start, but a useful place to start writing/learning my own code. I know handling forms can get very detailed, I wasn't trying to do all that just yet Quote Link to comment https://forums.phpfreaks.com/topic/177845-form-class-help-oop-php5/#findComment-937800 Share on other sites More sharing options...
fivestringsurf Posted December 22, 2009 Author Share Posted December 22, 2009 I'm disregarding the 30 day warning here and answering my own darn question. Only because there's nothing worse than reading to the bottom of some long post and not getting the answers you were looking for so here goes... So after giving oop a break and trying to re-learn it I think I wrote something useful at least to the folks reading this post in hopes of getting a handle on at least starting some form classes. My code below is by no means a finished product and obviously lacks many inputs and variables but it does seem to work just fine. enjoy! class Form{ public $val; public static $i=0; public $value; public static $n=0; function __construct($val='some default'){ $this->val=$val; //allows var to be used in subsequent methods(functions) } function set_form(){ echo "\n"; self::$i++; return '<form name="form_'.self::$i.'" method="post" enctype="multipart/form-data" action="'. substr($_SERVER["REQUEST_URI"],1).'"/>'; } function inputText(){ self::$n++; echo "\n"; return '<input type="text" name="inputText_'.self::$n.'" />'; } function submit(){ echo "\n"; return '<input type="submit" name="submitForm_'.self::$i.'" />'; } function parseForm(){ echo "\n"; echo "</form>"; if(isset($_POST['submitForm_'.self::$i])){//process form $r=array_pop($_POST); foreach($_POST as $this->value){ echo $this->value.'<br />'; } } } function __destruct(){ self::$n=0; //restart counters } } ?> <?php $f = new Form(); echo $f->set_form(); echo $f->inputText(); echo $f->inputText(); echo $f->submit(); $f->parseForm(); unset($f); $ff = new Form(); echo $ff->set_form(); echo $ff->inputText(); echo $ff->inputText(); echo $ff->submit(); $ff->parseForm(); unset($ff); Quote Link to comment https://forums.phpfreaks.com/topic/177845-form-class-help-oop-php5/#findComment-982759 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.