Welcome Guest, Not a member yet? Register   Sign In
double form in a function
#1

[eluser]Rhaziel[/eluser]
Hello guys,

I have a serious problem that i cant solve for about 6 hours now.
this is the controller function i use:

Code:
public function edit()
{

  
   $this->load->helper('form');
   $this->load->library('form_validation');
   $data['title'] = 'Bronie w Erpegi DUOS';
   $data['type'] = 'weapons';
   $data['type_pl'] = 'bron';
   $this->form_validation->set_rules('edit_object', 'Nazwa', 'required');
  if ($this->form_validation->run() === FALSE)
  {
   $data['edit_list'] = $this->db_functions_model->get_list($data['type']);
   $this->load->view('templates_views/header', $data);
   $this->load->view('templates_views/panel');
   $this->load->view('kompendium_views/edit', $data);
   $this->load->view('templates_views/footer');
  }
  else
  {
   $edited_object_name = $this->input->post('edit_object');
   $data['type_pl'] = $edited_object_name;
   $this->form_validation->set_rules('new_description', 'nowy opis', 'required');
   if ($this->form_validation->run() === FALSE)
   {
   $data['entry_description'] = $this->db_functions_model->get_entry_data($data['type'],$edited_object_name);
   $this->load->view('templates_views/header', $data);
   $this->load->view('templates_views/panel');
   $this->load->view('kompendium_views/edit_data', $data);
   $this->load->view('templates_views/footer');
   }
   else
   {
   $this->db_functions_model->set_entry_data($data['type'],$edited_object_name);
   redirect('weapons/');
   }
  }
}

And im sure the problem is in the controller. The model works just fine and updates the DB.

Let me show you the two views though:

edit.php:

Code:
<div id="panel"><h2>Zmien: &lt;?=$type_pl?&gt;</h2></div>

&lt;?php echo validation_errors(); ?&gt;

&lt;?php echo form_open(''.$type.'/edit') ?&gt;

<table class="tborder">
<tr class="trow2">
<td>
<select name="edit_object">
  &lt;?php  foreach ($edit_list as $choice): ?&gt;
  <option value="&lt;?php echo $choice['name'] ?&gt;">
    &lt;?php echo $choice['name'] ?&gt;</option>
    
&lt;?php endforeach  ?&gt;
</select>
</td>
</tr>

<tr class="trow2">
<td>
<br /> <br /><br />
</td>
</tr>

<tr class="trow2">
<td>
&lt;input type="submit" name="submit" value="Edytuj &lt;?=$type_pl?&gt;" /&gt;
</td>
</tr>

</table>
&lt;/form&gt;

and edit_data.php

Code:
<div id="panel"><h2>Edytuj opis: &lt;?=$type_pl?&gt;</h2></div>
&lt;?php echo validation_errors(); ?&gt;

&lt;?php echo form_open(''.$type.'/edit_data') ?&gt;
&lt;form&gt;
<table class="tborder">
<tr class="trow2">
<td>
&lt;textarea name="new_description" &gt;&lt;?=$entry_description?&gt;&lt;/textarea&gt;
</td>
</tr>

<tr class="trow2">
<td>
<br /> <br /><br />
</td>
</tr>

<tr class="trow2">
<td>
&lt;input type="submit" name="submit" value="Zmien &lt;?=$type_pl?&gt;" /&gt;
</td>
</tr>

</table>
&lt;/form&gt;


so what i get is:

I go to controller/edit
it lets me select an item from the list - everything so far is perfect
i go to next view from there - the edit_data
there i get the textarea filled by info from DB(model works just fine as you can see).
BUT! what i see above the text area is an error from form_evaluation!
Quote:The nowy opis field is required.
so i go ahead and modify the info in textarea, hit the button and get: 404 Page Not Found
This is obviously because i have this in my edit_data.php:
Code:
''.$type.'/edit_data'
which takes me to my controller which does not have this function as it has only edit() function.
SO this is driving me nuts! How can i do this??? I cannot evaluate the form because of this!!
#2

[eluser]Aken[/eluser]
You should separate your form functionality into two controllers. You have two different pages with two different forms doing different things - attempting to combine them like you are just invites problems and difficult-to-read code.
#3

[eluser]Rhaziel[/eluser]
That is absolutelly true. But if i make this in two controllers It will become easy for hacking. I do not want people accessing the edit_data part directly. How can i prevent them?

EDIT: I canged it a bit and now basically what i got here is two functions:

Code:
public function edit()
{

   $this->load->helper('form');
   $this->load->library('form_validation');
   $data['title'] = 'Bronie w Erpegi DUOS';
   $data['type'] = 'weapons';
   $data['type_pl'] = 'bron';
   $this->form_validation->set_rules('edit_object', 'Nazwa', 'required');
  if ($this->form_validation->run() === FALSE)
  {
   $data['edit_list'] = $this->db_functions_model->get_list($data['type']);
   $this->load->view('templates_views/header', $data);
   $this->load->view('templates_views/panel');
   $this->load->view('kompendium_views/edit', $data);
   $this->load->view('templates_views/footer');
  }
  else
  {
   $edited_object_name = $this->input->post('edit_object');
   redirect('weapons/edit_data');
  }

Code:
public function edit_data()
  {
   echo $edited_object_name;
   $data['title'] = 'zmien bron w Erpegi DUOS';
   $this->load->helper('form');
   $this->load->library('form_validation');
   $data['type'] = 'weapons';
   $data['type_pl'] = 'bron';
   $this->form_validation->set_rules('new_description', 'nowy opis', 'required');
   if ($this->form_validation->run() === FALSE)
   {
   $data['entry_description'] = $this->db_functions_model->get_entry_data($data['type'],$edited_object_name);
   $this->load->view('templates_views/header', $data);
   $this->load->view('templates_views/panel');
   $this->load->view('kompendium_views/edit_data', $data);
   $this->load->view('templates_views/footer');
   }
   else
   {
   $this->db_functions_model->set_entry_data($data['type'],$edited_object_name);
   redirect('weapons/');
   }
  
  }

now you see I do not call the edit_data() function instead I make a redirect. This is because for some reason if i call a function the URL stays the same (that is 'weapons/edit') and this is causing the problems.
But if i change the url to 'weapons/edit_data' then it all works perfectly.
But there are two problems with this:
- I cannot pass the variable from edit() which contains the name of object i want to edit
- Users have direct access to edit_data() which is probably a security flaw.
THIS IS MADNESS!
why this cant be easy?
I want to call a func, and it should change the URL!
#4

[eluser]Rhaziel[/eluser]
please, can anyone help me please....




Theme © iAndrew 2016 - Forum software by © MyBB