Page 1 of 2

OO: working with forms(Solved)

Posted: Tue Aug 09, 2005 1:25 pm
by raghavan20
I was trying to create classes for every INPUT element and use them in the PHP code. It displays every element but when I try to put them in the form, none of the elements get submitted.

1. Do you guys create INPUT elements as classes in OO?
2. If so, help me with your code.

Code: Select all

//inputElements.class.php
<?php
class TextBox{
	var $id;
	var $size;
	var $value;
	
	function TextBox($id, $value = "", $size = "50"){
		$this->id = $id;
		$this->size = $size;
		$this->value = $value;
		$this->createTextBox();
	}
	
	function createTextBox(){
		echo "<input type = 'text' id = '$this->id' value = '$this->value'>";
	}
	
}
?>

<?php
class Button{
	var $id;
	var $value;
	var $onClickAction;
	
	function Button($id, $value, $onClickAction = ""){
		$this->id = $id;
		$this->value = $value;
		$this->onClickAction = $onClickAction;
		$this->createButton();
	}
	
	function createButton(){
		echo "<input type = 'button' id = '$this->id' value = '$this->value' onclick = '$this->onClickAction';'>";
	}
	
}
?>

<?php
class SubmitButton{
	var $id;
	var $value;
	
	function SubmitButton($id, $value){
		$this->id = $id;
		$this->value = $value;
		$this->createSubmitButton();
	}
	
	function createSubmitButton(){
		echo "<input type = 'submit' id = '$this->id' value = '$this->value' >";
	}
	
}
?>

<?php
echo "<form name = 'frmDemo' method = 'GET' action = ''>";
echo "<input type = 'text' name = 'text1' value = ''>";//this control submits but not the others
//create a text box
$textInstance = new TextBox("txtName", "Name");
echo "<br />";
//create a button
$buttonInstance = new Button("btLink", "Go to Yahoo", "javascript:alert(\"http://www.yahoo.com\")");
echo "<br />";
//create a submit button
$submitInstance = new SubmitButton("subButton", "Submit Now");
echo "</form>";

//read GET variable
echo $_GET["txtName"];
?>

Posted: Tue Aug 09, 2005 1:42 pm
by nielsene
I probably wouldn't use seperate classes for each element. I would probably have a FormGenerator class that has methods for addTextElement, addSubmit, ... ending with renderForm(), etc. But that's just a personal preference (I hate single use objects. Ie something that you only create and call at most one function on.)


However, that said have you done a view source on the resulting webpage to check if the elements are being output'ed properly before the submission?

Posted: Tue Aug 09, 2005 2:09 pm
by feyd
you aren't calling create* on your classes.. so you just set the internal properties and let them die.

Posted: Tue Aug 09, 2005 2:17 pm
by raghavan20
Thats really a good idea and i am going to create a FormGenerator class.

Then regarding the problem, it was my fault. I thought this would work

Code: Select all

<input type = 'text' id = 'txtName' value = '' />
instead of 
<input type = 'text' name = 'txtName' value = '' />
Why should not the former work?

Posted: Tue Aug 09, 2005 2:19 pm
by feyd
oops, my bad.. I didn't see the create call in the constructor..

a form field requires a name so it can be encoded for transmission. An id is useful, but not required.. ever, really..

Posted: Tue Aug 09, 2005 2:29 pm
by raghavan20
cant we work with 'name' in DHTML, are 'ids' compulsory?
ex:
changing innerHTML of a text box using 'name' property; Is it possible? Or Ids can only do that?

Posted: Tue Aug 09, 2005 2:31 pm
by nielsene
Just as an example my FormGenerator looks something like this

Code: Select all

class HtmlFormGenerator {
  var $method;
  var $action;
  var $elements;

  function HtmlFormGenerator($action="",$method="post") {
    if ($action=="") $action=$_SERVER["PHP_SELF"];
    $this->method=$method;
    $this->action=$action;
    $this->elements="";
  }
  function addTextElement($name,$label, $value="", $msg="", $size=20) {
     $this->elements.=<<<END_ELEMENT
// Template for form elements, DIV's, etc
END_ELEMENT;
}
  function addTextAreaElement//...
  function addCheckBoxes//...
  //...
  // some more custom ones like addAddressEntry, addDatePicker....

  function renderForm($formStyle="form") {
    $form =<<<END_FORM
<div class="$formStyle">
<form action="{$this->action}" method="{$this->method}">
  <fieldset>
  $this->elements
  </fieldset>
</form>
</div>
END_FORM;
   return $form;
  }
}
Glossing over a few details (like I think I have a <legend> in there somewhere and I have one more parameter to all the addText's (Instructions).

but you can use it basically like

Code: Select all

$form= new HtmlFormGenerator();
$form->addTextElement("username","Username");
$form->addPasswordElement(); // more defaults 
echo $form->render();

Posted: Tue Aug 09, 2005 2:46 pm
by feyd
ids and names are available through DHTML. The browser itself requires names in order to do a submission though.

Posted: Tue Aug 09, 2005 2:52 pm
by raghavan20
This is wot I managed to do now. but the renderForm is ugly. I should think of incorporating your logic inside.


Any more suggestions appreciated.

Code: Select all

<?php

class FormGenerator{
	var $name;
	var $method;
	var $action;
	
	function FormGenerator($name, $method = 'post', $action = ''){
		$this->name = $name;
		$this->method = $method;
		$this->action = $action;
		echo "<form name = '$name' method = '$method' action = '$action'>";
	}
	
	function addTextBox($name, $value = "", $size = "50"){
		echo "<input type = 'text' id = '$name' name = '$name' value = '$value' size = '$size' />";
	}
	
	function addCheckBox($name, $checked = ''){
		echo "<input type = 'checkbox' name = '$name' $checked />";
	}

	function addRadioButton($name, $checked = ''){
		echo "<input type = 'radio' name = '$name' $checked />";
	}
	
	function addTextArea($name, $value, $cols = '40', $rows = '5'){
		echo "<textarea id = '$name' name = '$name' rows = '$rows' cols = '$cols'>$value</textarea>";
	}
	
	function addButton($name, $value, $onClickAction = ""){
		echo "<input type = 'button' name = '$name' id = '$name' value = '$value' onclick = '$onClickAction';'>";
	}
	
	function addSubmitButton($name, $value){
		echo "<input type = 'submit' id = '$name' name = '$name' value = '$value' >";
	}
	
	function renderForm(){
		echo "</form>";
	}
	
}
?>

Code: Select all

$form =<<<END_FORM ... END_FORM;
<<<END_ELEMENT ... END_ELEMENT
What are these?

Posted: Tue Aug 09, 2005 3:02 pm
by nielsene
That's what's called Heredoc's. Its a way to place large blocks of text that allow variable interpolating, without needed to escape all the double quotes inside.... Its wondeful for outputting blocks of HTML.

I'm also a firm beleiver in avoiding side-effects as much as possible, thus the class's methods shouldn't echo to the screen, rather they should either return strings (for the caller to echo) or build up the internal element string for eventual rendering.

This lets you have the renderForm wrap the elements in whatever markup you need.

Posted: Tue Aug 09, 2005 4:51 pm
by raghavan20
do you think its worth creating a class for table?
If so, can you give me the specifications on which the class is to be built?

Posted: Tue Aug 09, 2005 4:56 pm
by nielsene
I've never found a table class useful. I do often have List Objects that can render themselves out to simple XHTML tables.

Posted: Tue Aug 09, 2005 5:57 pm
by raghavan20
If you dont mind, can you tell me what do you mean by 'List objects'?

I have the following classes now, what common classes should I have other than these for any OO application?
1. DbMysql - for connection
2. DbMysqlStatement - for executing queries
3. FormGenerator - for creating forms

Posted: Tue Aug 09, 2005 5:59 pm
by s.dot
raghavan20 wrote:cant we work with 'name' in DHTML, are 'ids' compulsory?
ex:
changing innerHTML of a text box using 'name' property; Is it possible? Or Ids can only do that?
I have found that IE will allow you to go by name of an element whereas more standard compliant browsers strictly go by ID.

Posted: Tue Aug 09, 2005 6:16 pm
by raghavan20
I think thats why most of the sites assign for both 'name' and 'id'
ex: <input type = 'text' name = 'txtName' id = 'txtName' value = ''>

scrotaye, if possible can you give me the list of browsers which make use of 'id'?