Code: Select all
public function add_button($value, $class) {
$this->_content .= "<input type=\"submit\" value=\"".$value."\" onClick=\"ajax(".$class.", [";
for ($i = 0; $this->_propertiesArray >= $i && $this->_propertiesArray <= $this->_propertiesCount; $i++) {
$this->_content .= "\"".$this->_propertiesArray[$i]."\", ";
}
$this->_content .= "]);\" />";
}Code: Select all
class form {
private $_id;
private $_content;
private $_propertiesCount = 0;
private $_propertiesArray = array();
private $_output;
public function __construct($id) {
$this->_id = $id;
return(TRUE);
}
public function add_field($label, $name) {
$this->_content .= "<label>".$label."</label><input type=\"text\" name=\"".$name."\" />";
$this->_propertiesArray[$this->_propertiesCount] = $name;
$this->_propertiesCount += 1;
}
public function add_button($value, $class) {
$this->_content .= "<input type=\"submit\" value=\"".$value."\" onClick=\"ajax(".$class.", [";
for ($i = 0; $this->_propertiesArray >= $i && $this->_propertiesArray <= $this->_propertiesCount; $i++) {
$this->_content .= "\"".$this->_propertiesArray[$i]."\", ";
}
$this->_content .= "]);\" />";
}
public function build() {
$this->_output = "<form id=\"".$this->_id."\" action=\" \">";
$this->_output .= $this->_content;
$this->_output .= "</form>";
return($this->_output);
}
public function __destruct() {
return(NULL);
}
}Code: Select all
$form->add_field('First word', 'first');
$form->add_field('Second word', 'second');
$form->add_button('Click me', 'concatenate');
$result = $form->build();
echo $result;- 1) Add 'Click me' as the text on the submit button;
2) Add 'concatenate' as the name of a parameter to use in my ajax() JavaScript function;
3) Add to an array the names of all the fields in the form (in this case, 'first' and 'second');
4) Output the contents of the array to a string in the form of:
5) With the result being HTML that looks like:["first", "second", ]<input type="submit" value="Click me" onClick="ajax(concatenate, ["first", "second", ]);" />