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!
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.
//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.
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?
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?
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).
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.
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
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.