OO: working with forms(Solved)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

OO: working with forms(Solved)

Post 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"];
?>
Last edited by raghavan20 on Tue Aug 09, 2005 2:22 pm, edited 1 time in total.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you aren't calling create* on your classes.. so you just set the internal properties and let them die.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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?
Last edited by raghavan20 on Tue Aug 09, 2005 2:20 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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();
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ids and names are available through DHTML. The browser itself requires names in order to do a submission though.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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?
Last edited by raghavan20 on Tue Aug 09, 2005 3:09 pm, edited 1 time in total.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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'?
Post Reply