Welcome Guest, Not a member yet? Register   Sign In
Conversion of my $_POST to CI and isset problem
#1

[eluser]Wondering Coder[/eluser]
hello everyone,
My Controller
Code:
function save_char_ref()
    {
        $data['mod'] = strtolower(get_class($this));
    $data['role'] = $this->session->userdata('data_id');    
    $dt = $this->session->userdata('user_id');
    $data['user'] = $this->main->getProfile($dt)->row();
        
        if(isset($_POST['cr_fullname']))
        {  
            for($i=0;$i<count($_POST['cr_fullname']);$i++)
                {
                    $detail[] = array(
                            'user_id' => $dt,
                            'cr_fullname' => $_POST['cr_fullname'][$i],
                            'cr_company' => $_POST['cr_company'][$i],
                            'cr_address' => $_POST['cr_address'][$i],
                            'cr_position' => $_POST['cr_position'][$i]
                        );
                }
        }
            foreach($detail as $item)
                {
                    $this->main->save_detail_char($item);
                }  
        $data['content'] = $this->load->view('panes/stud_profile', $data, TRUE);
        $this->output->enable_profiler(TRUE);
        $this->load->vars($data);
        $this->load->view('template');    
    }

I have a two problem which I need to resolve.
1. First the part where I'm using $_POST[$var], I would like to convert this to CI structure class which is $this->input->post() but don't know how to do this.
2. Second is my isset($_POST) function, even though my input value are empty it still save the data in my db.

For more info this is my view
Code:
&lt;form method="post" action="&lt;?=base_url();?&gt;stud_profile/save_char_ref"&gt;
                        <div id="dynamicRef">
                        <div class="sectortitle" style="padding-left:8px">Character Reference No. 1</div><br /><br /><br />
                            <div class="rleft">Full Name:</div>
                            &lt;input type="text" name="cr_fullname[]" /&gt;&lt;br />
                            <div class="rleft">Company Name:</div>
                            &lt;input type="text" name="cr_company[]" /&gt;&lt;br />
                            <div class="rleft">Address:</div>
                            &lt;input type="text" name="cr_address[]" /&gt;&lt;br />
                            <div class="rleft">Position:</div>
                            &lt;input type="text" name="cr_position[]" /&gt;&lt;br />
                
                        </div><br />
                        &lt;input type="button" value="Add Another Reference"&gt;
                        &lt;input type="submit" value="Save Info" /&gt;
                &lt;/form&gt;

[removed]
var counter = 1;
var limit = 4;
function addRef(divName){
     if (counter == limit)  {
          alert("You have reached the limit of adding Character Reference" + counter + " inputs");
     }
     else {
          var newdiv = document.createElement('div');
          newdiv[removed] = "<div class='sectortitle' style='padding-left:8px'>Character Reference No. " + (counter + 1) + "</div><br /><br /><br /><div class='rleft'>Full Name:</div>&lt;input type='text' name='cr_fullname[]' /&gt;&lt;br /><div class='rleft'>Company Name:</div>&lt;input type='text' name='cr_company[]' /&gt;&lt;br /><div class='rleft'>Address:</div>&lt;input type='text' name='cr_address[]' /&gt;&lt;br /><div class='rleft'>Position:</div>&lt;input type='text' name='cr_position[]' /&gt;&lt;br />";
          document.getElementById(divName).appendChild(newdiv);
          counter++;
     }
}
    
}
[removed]
#2

[eluser]Wondering Coder[/eluser]
anyone? T_T
#3

[eluser]steelaz[/eluser]
Why "cr_fullname[]" and not simply "cr_fullname"?

Edit: nevermind, noticed your JS
#4

[eluser]Wondering Coder[/eluser]
so any idea how to convert this? and my isset function not working.?
#5

[eluser]steelaz[/eluser]
Your isset() function is working fine. When you submit empty text field, it's still going to be included in $_POST array, so isset() will return true. If you want to check if something was entered in your text field, you can use empty() function, i.e.:

Code:
if (!empty($_POST['cr_fullname'][$i])) {  };
#6

[eluser]Wondering Coder[/eluser]
thanks steelaz,

got it working. ^_^.
This is now my new controller:
Code:
function add()
    {
        $data['mod'] = strtolower(get_class($this));
    
        for($i=0;$i<count($_POST['myfield1']);$i++)
        {
  
           $detail[]=array(
                'field1'=>$_POST['myfield1'][$i],
                'field2'=>$_POST['myfield2'][$i],
                'field3'=>$_POST['myfield3'][$i]);
                    
           if(!empty($_POST['myfield1'][$i]))
           {
               foreach($detail as $key=>$value)
                {
                    $this->welcome_model->save($value);
                }
           }
           else
           {
                $data['error'] = "No Entry";
           }
       }
      
        $this->output->enable_profiler(TRUE);
        $this->load->view('success',$data);
    }

Is it ok to use $_POST instead of the $this->input->post in CI class? Even if i want it though I don't know how to do this with my current code. Because all of my other controller function used CI class for input except this one. Don't want it to look messy. (@_@)
#7

[eluser]steelaz[/eluser]
Try this:

Code:
$myfield1 = $this->input->post('myfield1');
$myfield2 = $this->input->post('myfield2');
$myfield3 = $this->input->post('myfield3');

for ($i = 0; $i < count($myfield1); $i++)
{

   $detail[] = array(
        'field1' => $myfield1[$i],
        'field2' => $myfield2[$i],
        'field3' => $myfield3[$i]);

   if (!empty($myfield1[$i]))
   {
       foreach($detail as $key=>$value)
        {
            $this->welcome_model->save($value);
        }
   }
   else
   {
        $data['error'] = "No Entry";
   }
}
#8

[eluser]Wondering Coder[/eluser]
that did it. thank you very much steelaz...

I owe you two. ^_^




Theme © iAndrew 2016 - Forum software by © MyBB