1

I'm new in codeigniter and I've already seen some similar types of questions here. But none worked in my case. I've a form in my view and I've passed the input value from this form to a method named "Insert" inside the controller. Now,I'm trying to move this value from "insert" method "to another method named "post_action". But couldn't make it possible.

This is the Controller:

public function insert() {
        $data['values'] = $this->Final_model->insert();
        $this->load->view('info/insert',$data);


        $val = $this->input->post('coursecode'); 

        echo $val;


    $this->post_action($val); //passing data into another function

    }


public function post_action($val='')
    {   
        $temp1 = $this->input->post('textbox');
    if($temp1== "")
    {
        $message = "You can't send empty text";

    }
    else
    {
        print_r($val);

        if($val == NULL) echo 'Value is null bro...';


        $grades = $_POST['grade'];
        $msg = $_POST['my'];

        $message = $_POST['textbox'];
        $this->Final_model->build_post($msg,$val,$message,$grades);
        echo "Value added successfully";
        }

    }

This is my 'index' view :

<?php echo form_open('Home_Controller/insert'); ?>
 <div class="form-group">
<label>Course Code</label>
<input type="text" class="form-control" name = "coursecode"
placeholder="Add Course Code">
<br> <br>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>

This is my 'insert' view :

  <?php echo form_open('Home_controller/post_action'); ?>
<td> <input type="text" name="textbox" id = "textbox"> </td>
<td><input type="text" name="grade" id = "grade1"></td>
<input type="hidden" name = "my" value = "<?php  echo $value['id']; ?>" 
id 
= "my"/>
<td><input type="submit" value="Submit"></td>
</form>

Now, it is echoing the correct value in the insert method passed by the view. But in the post_action method, it is echoing "value is null bro" and a 0 is passing to the database table(this one is an insert operation done in the model).

My question is,why can't I get the input value passed from the view, in this post_action method.

P.S: I've already spent about 2 days in this problem but haven't got anything. So, please forgive me if the question is too naive. Thanks in advance.

1 Answer 1

1

Please try with this Controller:

<?php


public function insert()
{

    $data['values'] = $this->Final_model->insert();
    $this->load->view('info/insert',$data);

    $val['coursecode'] = $this->input->post('coursecode');
    echo $val['coursecode']; //echoing value from post

    if(!empty($val)){
        $this->post_action($val); //passing val into another function
    } else {
        echo "Didn't get value from post";
    }

}

public function post_action($val = '')
{
    if(is_array($val) && count($val) > 0)
    {
        $grades = $_POST['grade'];
        $msg = $_POST['my'];

        $message = $_POST['textbox'];
        $this->Final_model->build_post($msg,$val,$message,$grades);
        echo "Value added successfully";
    } 
    else 
    {
       echo "No result found";
    }
}


?>
Sign up to request clarification or add additional context in comments.

1 Comment

In the insert function, it is echoing the passed input value. But in the post_action function, it is echoing "No result found".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.