Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two different submit buttons in CodeIgniter form without Javascript [duplicate]

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 Save button, 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.

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 ?

like image 979
aspirinemaga Avatar asked Dec 09 '22 15:12

aspirinemaga


1 Answers

Use this 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:

$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.

like image 56
likeitlikeit Avatar answered May 19 '23 11:05

likeitlikeit