form class help (oop php5)

Posted on 16th Feb 2014 by admin

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.

Code: [Select]<?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

Other forums