Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeIgniter - Two Forms, One Page
    primarykey
    data
    text
    <p>I'm having a fundamental problem in understanding the concept of MVC and displaying more than one form at a time. I've tried a variety of methods but I'm still stuck - and that's because I don't think I'm understanding CI and MVC correctly.</p> <p>I tried using 2 different views for the two different forms. Didn't work. I tried using one function per form in my controller. That didn't work either. I don't know what to do.</p> <p>Should I be doing this;</p> <ol> <li>Create a controller and have an index() function in it.</li> <li>Build up my form elements for each form within this index()</li> <li>Create 1 view that displays both forms and call it from within index()</li> <li>Use form_open to direct the submit action to another function - call it validate()</li> <li>Validate everything that comes in, send back errors</li> <li>Somehow, and this is the main bit I don't get, complete an action if the form has been filled in correctly.</li> </ol> <p>6 Is my biggest problem. Don't know how to do that. For example, on successful completion of the form I want my user to have created a directory at a chosen location - so I'm using mkdir() - so do I need an if statement within the validate() function or what??</p> <p><em><strong>UPDATE</em></strong></p> <p>Here is the code I have created so far;</p> <p>Controller:</p> <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); </code></pre> <p>// Forms CodeIgniter controller class Admin extends CI_Controller {</p> <pre><code>// Controller constructor public function __construct() { parent::__construct(); // Load form helper required to validate forms $this-&gt;load-&gt;helper(array('form', 'url')); $this-&gt;load-&gt;library('form_validation'); } //*************************************************// // Prepare data for the view to output the forms public function index() { //*****************************************************// //returns a drop down list of radio buttons, one for each directory $map_one = $this-&gt;recursive_model-&gt;iterate_add_folder_names(); $data['folder_list_add'] = $map_one; //****************************************************// //*****************************************************// //also returns a drop down list of radio buttons (slightly different), one for each directory $map_two = $this-&gt;recursive_model-&gt;iterate_folder_names(); $data['folder_list_select'] = $map_two; //****************************************************// //load the views and the forms $this-&gt;load-&gt;view('templates/header.php'); $this-&gt;load-&gt;view('admin/add_new_folder.php', $data); $this-&gt;load-&gt;view('admin/add_new_file.php', $data); $this-&gt;load-&gt;view('templates/small_footer.php'); } //*************************************************// //function if adding a new directory to the current structure public function add_folder() { //need to select a directory for it to go under $this-&gt;form_validation-&gt;set_rules('new_folder', 'New Folder', 'required'); //and name the new directory $this-&gt;form_validation-&gt;set_rules('new_folder_name', 'New Folder Name', 'required'); if ($this-&gt;form_validation-&gt;run() === FALSE) { $this-&gt;index(); } else { if($this-&gt;input-&gt;post()) { $new_folder = $this-&gt;input-&gt;post('new_folder'); $new_folder_name = $this-&gt;input-&gt;post('new_folder_name'); $folder_path = "/var/www/html/mike/content".$new_folder."/".$new_folder_name; mkdir($folder_path, 0777); $this-&gt;index(); } } } //*************************************************// public function add_file() { //folder location and name of file $folder_name = $this-&gt;input-&gt;post('folder_name'); $new_folder_name = $this-&gt;input-&gt;post('file_name'); //validation rules $this-&gt;form_validation-&gt;set_rules('folder_name', 'Folder Name', 'required'); $this-&gt;form_validation-&gt;set_rules('file_name', 'File Name', 'required'); //if there is an error with validation if ($this-&gt;form_validation-&gt;run() === FALSE) { //gets stuck here every time when trying to upload a new folder :( $this-&gt;index(); } //if there is not an error with validation else { //$folder_name will be something like "http://www.example.com/publications/people/reports" $config['upload_path'] = $folder_name; $config['allowed_types'] = 'gif|jpg|png|html|pdf|xls'; $this-&gt;load-&gt;library('upload', $config); //if file cannot be loaded (due to $config perhaps?) if ( ! $this-&gt;upload-&gt;do_upload()) { $error = array('error' =&gt; $this-&gt;upload-&gt;display_errors()); $this-&gt;index(); } else { $data = array('upload_data' =&gt; $this-&gt;upload-&gt;data()); $this-&gt;index(); } } } //*************************************************// </code></pre> <p>}</p> <p>Here is one view (add_new_file.php);</p> <pre><code>&lt;div id="container"&gt; &lt;h1&gt;Upload A File/Publication&lt;/h1&gt; &lt;div id="body"&gt; &lt;?php //echo $error;?&gt; &lt;?php echo form_open_multipart('admin/add_file');?&gt; &lt;?php echo $folder_list_select; ?&gt; &amp;nbsp;&amp;nbsp; &lt;input type="file" name="file_name" size="20" /&gt; &amp;nbsp;&amp;nbsp; &lt;input type="submit" value="upload" /&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p></p> <p>Here is the other (add_new_folder.php)</p> <pre><code>div id="container"&gt; &lt;h1&gt;Add A New Folder&lt;/h1&gt; &lt;div id="body"&gt; &lt;?php echo validation_errors(); ?&gt; &lt;?php echo form_open('admin/add_folder');?&gt; &lt;?php echo $folder_list_add; ?&gt; &amp;nbsp;&amp;nbsp; New Folder Name: &lt;input type="text" name="new_folder_name"&gt; &amp;nbsp;&amp;nbsp; &lt;input type="submit" value="upload" /&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p></p> <p>I hope this helps answer this thread.</p> <p>Basically, I can get the first section to work - adding a folder - but I cannot get the adding a file to work. This is because if <strong>($this->form_validation->run() === FALSE)</strong> is always returning false. I think it might be looking at the form elements in the other form - which it shouldn't do. What am I missing?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload