0

While learning how to use Code Igniter, i have encountered a problem when submitting form values. I'm trying to do an insert if $_POST and redirect to the form if it wasn't successful, but every time i submit the form i see there are no form values in my header. Been unable to figure out what am doing wrong even after going through several related post, would appreciate the help of the experts here. Here's what I'm working with

View: new_product.php

<form action="products/new_product" method="POST" role="form">
    <legend>Upload New Item</legend>

    <div class="form-group">
        <label for="">Name</label>
        <input type="text" class="form-control" id="name" placeholder="Input field">
        <label for="">Description</label>
        <input type="text" class="form-control" id="description" placeholder="Input field">
        <label for="">Category</label>
        <input type="text" class="form-control" id="category" placeholder="Input field">
        <label for="">Price</label>
        <input type="text" class="form-control" id="price" placeholder="Input field">
    </div>

    <button type="submit" class="btn btn-primary">Upload</button>
</form>

Controller: products.php

public function new_product() 
{
    if ($_POST) {
        $data = array(
            'category' => $this->input->post('category'),
            'name' => $this->input->post('name'),
            //'photo' => $this->input->post('photo'),
            'description' => $this->input->post('description'),
            'price' => $this->input->post('price')
        );
        $this->product->upload_product($data);
        redirect(base_url().'products/');
    } else {
        $this->load->view('header', FALSE);
        $this->load->view('new_product');
        $this->load->view('footer', FALSE);
    }
}

Model: product.php

function upload_product($data) {
    $this->db->insert('products', $data);
    return $this->db->insert_id();
}

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

header recorder during form submission

header information

2
  • where is your 'csrf_token'? Commented Jul 19, 2016 at 10:26
  • I'm sorry, don't know what you are referring to. Please bare with me, I'm a new to Codeigniter Commented Jul 19, 2016 at 10:29

4 Answers 4

2

Try adding this inside your form,

<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">

So your mark up will be like this now,

<form action="products/new_product" method="POST" role="form">
    <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
    <legend>Upload New Item</legend>

    <div class="form-group">
        <label for="">Name</label>
        <input type="text" class="form-control" id="name" placeholder="Input field">
        <label for="">Description</label>
        <input type="text" class="form-control" id="description" placeholder="Input field">
        <label for="">Category</label>
        <input type="text" class="form-control" id="category" placeholder="Input field">
        <label for="">Price</label>
        <input type="text" class="form-control" id="price" placeholder="Input field">
    </div>

    <button type="submit" class="btn btn-primary">Upload</button>
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

Appreciate the help. What's the csrf_token for?
It is a security feature of CI (CodeIgniter) when you are sending form data. It is required. Read more about this here.
2

Try this.. you missed name attribute

<form action="products/new_product" method="POST" role="form">
    <legend>Upload New Item</legend>

    <div class="form-group">
        <label for="">Name</label>
        <input type="text" class="form-control" name="name" id="name" placeholder="Input field">
        <label for="">Description</label>
        <input type="text" class="form-control" name="description" id="description" placeholder="Input field">
        <label for="">Category</label>
        <input type="text" class="form-control" name="category" id="category" placeholder="Input field">
        <label for="">Price</label>
        <input type="text" class="form-control" id="price" name="price" placeholder="Input field">
    </div>

    <button type="submit" class="btn btn-primary">Upload</button>
</form>

Comments

1

Your form should be llike this (NO name and value attributes given)

       <form action="products/new_product" method="POST" role="form">
              <legend>Upload New Item</legend>

             <div class="form-group">
                 <label for="">Name</label>
                   <input type="text" name="name" value="" class="form-control" id="name" placeholder="Input field">
                   <label for="">Description</label>
                   <input type="text" name="description" value="" class="form-control" id="description" placeholder="Input field">
                  <label for="">Category</label>
                 <input type="text" name="category" value="" class="form-control" id="category" placeholder="Input field">
                 <label for="">Price</label>
                  <input type="text" name="price" value="" class="form-control" id="price" placeholder="Input field">
            </div>

          <button name='submit' type="submit" class="btn btn-primary">Upload</button>
     </form>

http://www.w3schools.com/html/html_forms.asp

Comments

1

Ofcourse, CSRF will be required but your problem is different I think. You have missed to add a name property to each of your input tags.

e.g

<input type="text" class="form-control" id="name" name="first_name" placeholder="Input field">

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.