php CodeIgniter 表单中没有 Javascript 的两个不同提交按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16627241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:27:37  来源:igfitidea点击:

Two different submit buttons in CodeIgniter form without Javascript

phpcodeigniter-2

提问by aspirinemaga

I have a form where I can add a new category item.

我有一个表单,可以在其中添加新的类别项。

<form method="POST" action="backend/categories/form">
    <input type="text" name="title" value="" />

    <button type="submit">Save</button>
    <button type="submit">Save and add new</button>
</form>

What I want to do, is that if i click on Savebutton, it will process a function in controller and redirect me automatically into the previous page (list of categories page), but whenever I click on Save and add new, it should process the function but reload the same page without redirecting to the page which is defined in controller's function.

我想要做的是,如果我点击Save按钮,它会处理控制器中的一个函数并自动将我重定向到上一页(类别列表页面),但是每当我点击时Save and add new,它应该处理该函数但重新加载相同的页面而不重定向到控制器函数中定义的页面。

Controller:

控制器:

function form($id){

        // Process the form
        // ...

        // Redirect to the category list page
        redirect($this->config->item('backend_folder').'/categories');
}

Any tips to achieve it without using the Javascript ?

在不使用 Javascript 的情况下实现它的任何提示?

回答by likeitlikeit

Use this HTML:

使用这个 HTML:

<form method="POST" action="backend/categories/form">
    <input type="text" name="title" value="" />

    <button type="submit" name="submitForm" value="formSave">SAVE</button>
    <button type="submit" name="submitForm" value="formSaveNew">SAVE AND ADD NEW</button>
</form>

Then check the POST data like this:

然后像这样检查POST数据:

$formSubmit = $this->input->post('submitForm');
if( $formSubmit == 'formSaveNew' )
    redirect($this->config->item('backend_folder').'/categories/form');
else
    redirect($this->config->item('backend_folder').'/categories');

Disclaimer: I didn't try that.

免责声明:我没有尝试过。

回答by Vijaya Pandey

This might be helpful for you:

这可能对您有帮助:

Use this in your save function in your controller. Immediate after Insert/Update code:

在控制器的保存功能中使用它。插入/更新代码后立即:

$task = $_POST['submit'];
//echo $task //Save or SaveNew depending on pressed button

    switch ($task)
    {
        case 'Save':

             $this->session->set_flashdata('message',$this->lang->line('changes_has_been_saved_successfully'));
            $link = redirect('cashdrawer/cashdrawerform/12'); //Link after save button with id
            break;


    case 'SaveNew':

            $this->session->set_flashdata('message',$this->lang->line('this_order_has_been_saved_successfully'));
                $link = redirect('cashdrawer/cashdrawerform'); //Link after save and new
            //echo $link;


    }


     redirect($link);

And Change your button as:

并将您的按钮更改为:

<button type="submit">Save</button>
    <button type="submit">SaveNew</button>