简体   繁体   English

从类中调用表单提交中的函数

[英]Calling a function on form submit from within a class

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 编辑:我包括一个Pastebin,所以您可以看到整个代码: http : //pastebin.com/MWJXU1hB

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. 您还可以管理整个应用程序中使用的类加载和变量。

Maybe this is not an answer, but need to be that to code formatting, since OP does not understand my comment. 也许这不是一个答案,但由于OP无法理解我的评论,因此必须是代码格式化的答案。 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;
    }

}

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 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM