Reputation: 10066
I currently am building a class that has some functions defined in it. The first function displays a form for the user to fill out some attributes:
function display_sheet_form($f=array())
{
$count = (isset($f['task_title'])) ? count($f['task_title']) : 3;
if ($count < 3) $count = 3;
echo '<form name="add_sheet" id="dls-sus-modify-sheet" method="post" action="">';
//some additional code here to display the form elements
echo '<input type="hidden" name="mode" value="submitted" />';
echo '<input type="submit" name="Submit" class="button-primary" value="'.esc_attr('Save').'" />';
echo '</form>';
}
I then have another function that I want to do the form processing:
function modify_sheet_page()
{
echo "in the modify sheet page";
// Set mode vars
$edit = (empty($_GET['sheet_id'])) ? false : true;
$add = ($edit) ? false : true;
$submitted = (isset($_POST['mode']) && $_POST['mode'] == 'submitted');
$err = 0;
// Process form if submitted
if($submitted) {
//process the form code
}
}
How can I have the form elements be submitted directly to this function within this class?
EDIT: I included a Pastebin so you can see the whole code: http://pastebin.com/MWJXU1hB
Upvotes: 0
Views: 3834
Reputation: 21
In the first function, edit the action of the form to same page, then first call that function. Then add this code:
if ($_SERVER ["REQUEST_METHOD "]==POST)
{CALL SECOND FUNCTION }
Upvotes: 0
Reputation: 12391
Maybe this is not an answer, but need to be that to code formatting, since OP does not understand my comment. As others says too, it can not be called that directly.
Instead of trying to do that, put some line of codes at the and of the file, to check, shold we display. Alternativaly both.
Here is an example. Basically all classis is in separated files, this is just a demonstration:
//Create the object
$MyClass = new MyClass();
//If form submitted
if (!empty($_POST['modify']) && $_POST["modify"] == 'save') {
//Validate the form
if ($MyClass->validate_sheet_form()) {
//Validation was success, update the data, and redirect if you want.
$MyClass->modify_sheet_page();
header("Location: somewhere.php");
die();
}
}
//Show error if we have.
if (!empty($MyClass->errorMsg)) {
echo '<div class="error">' . $MyClass->errorMsg ."</div>";
}
//Display.
$MyClass->display_sheet_form();
class MyClass {
var $errorMsg = '';
function display_sheet_form($f = array()) {
//....
}
function modify_sheet_page() {
//....
}
function validate_sheet_form() {
//if something fail, set $this->error = "Error message";
//and return false; otherwise, true.
if (empty($_POST["username"])) {
$this->errorMsg = "Username is required<br>";
}
//.... more validation
if (!empty($this->errorMsg)) {
return false;
}
return true;
}
}
Upvotes: 1
Reputation: 6769
In short: you can't.
But a good method to automate the process is to make use of bootstrapping and reroute the incoming requests to the proper classes/functions.
I'm proposing bootstrapping because it is a reusable concept and, in my opinion a good practice. You could additionally manage your class loading and variables used throughout the application.
Upvotes: 3