2

How can I pass the data input by user from view to controller in codeigniter Php using get or post method? I'm currently new to codeigniter..thanks!

AddProduct.php (my view)

<body>
    <form method="POST" action="SaveProductController"></br></br></br>
        <table border='1' align='center'>
            <tr>
                <td>ID: </td><td><input type="text" name="id" 
                                        value="<?php echo $GetProductId->id + 1; ?>" readonly="readonly"></td>
            </tr>
            <tr>
                <td>Description: </td><td><input type="text" name="description"></td>
            </tr>
            <tr>
                <td></td><td><input type="submit" name="addProduct" value="Add Product"><td>
            </tr>
        </table>
    </form>
</body>

SaveProductController .php (my Controller)

class SaveProductController extends CI_Controller{

function index($description){
    $this->load->model('ProductDao');
    $data['id'] = $this->id;
    $data['description'] = $this->description;
    print_r($data);
    //$this->ProductDao->saveProduct();
}

}

ProductDao.php

 function saveProduct() {
    $data = array(
    'id' => $this->input->xss_clean($this->input->post('id')),
    'description' => $this->input->xss_clean($this->input->post('description')),
    'price' => $this->input->xss_clean($this->input->post('price')),
    'size' => $this->input->xss_clean($this->input->post('size')),
    'aisle' => $this->input->xss_clean($this->input->post('aisle')),
    );

    $query = $this->db->insert('mytable', $data);
}
1

2 Answers 2

5
<body>
    <form method="POST" action="<?php echo $this->base_url();?>/controllername/methodname"></br></br></br>
        <table border='1' align='center'>
            <tr>
                <td>ID: </td><td><input type="text" name="id" 
                                        value="<?php echo $GetProductId->id + 1; ?>" readonly="readonly"></td>
            </tr>
            <tr>
                <td>Description: </td><td><input type="text" name="description"></td>
            </tr>
            <tr>
                <td></td><td><input type="submit" name="addProduct" value="Add Product"><td>
            </tr>
        </table>
    </form>
</body>

observe the change in action

and in your comtroller specified method get values as shown below

$id=$this->input->post('id')
$idescription=$this->input->post('description')

then send these to model and do what ever you want

load url helper library to make $this->baseurl() to work other wise hard code it using localhost/path...

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

4 Comments

+1, when your using the index function you don't need the method name in the form action, by default, the index method would be launched.
Also base_url is a helper function, no need to use $this, check url helper and form helper.
@Sivagopal Manpragada hi. thanks for your help. i have successfully display the data in my page. if you don't mind, what does this line means? action="<?php echo $this->baseurl();?>/controllername/methodname"> ..i have configure baseurl on /application/config/config/baseurl = 'index.php'
localhost/codeigniter/index.php/ this part is called baseurl() read codeigniter manual of base_url() can you accept my answer
4

I have created controller named : Test_controller and view named : manage_test_controller and using below code you can get data from view file to controller.

Test_controller.php

 class Test_controller extends CI_Controller {

        var $controller = "user";
        var $formValues = array();
    function manage_user() {               

                $this->formValues['formAction'] = SITEURL . '/' . 
                $this->controller . '/manage_' . $this->controller;

                if (isset($_POST['displayName']))
                    $this->formValues['displayName'] = $_POST['displayName']; 
                else
                    $this->formValues['displayName'] = "";
                if (isset($_POST['userEmail']))
                    $this->formValues['userEmail'] = $_POST['userEmail']; else
                    $this->formValues['userEmail'] = "";                   

            $this->load->view('header');
            $this->load->view($this->controller . '/manage_' . 
            $this->controller, $this->formValues);
            $this->load->view('footer');
        }
    }

Manage_test_controller.php

<?php echo form_open_multipart($formAction); ?>
<table>
    <tr>
            <td><?php echo form_label('Display Name'); ?><em>*</em></td>
            <td><?php echo form_input('displayName',$displayName); ?></td>
        </tr>
    <tr>
            <td><?php echo form_label('Email'); ?><em>*</em></td>
            <td><?php echo form_input('userEmail',$userEmail); ?></td>
        </tr>
</table>
<?php echo form_close(); ?>

Hope this code will help you.... :)

Comments

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.