Two submit buttons in CodeIgniter without Javascript

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 the Save button, it will process the function in the controller and will automatically redirect me to the previous page (category list), but whenever I click on Save and add new , it should process the function , but reload the same page without redirecting to the page that is defined in the controller function.

Controller:

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

Any tips for achieving it without using Javascript?

+4
source share
2 answers

Use this HTML code:

 <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 as follows:

 $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 have not tried this.

+12
source

This may be useful for you:

Use this in your save function in your controller. Immediately after entering / updating the 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> 
0
source

Source: https://habr.com/ru/post/1481544/


All Articles