Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/177845-form-class-help-oop-php5/
Share on other sites

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.

 

 

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?

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 ;)

  • 2 months later...

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);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.