codeigniter form validation for dynamic form input names - forms

I have a codeigniter app. My view uses the database row ID to append to the input name to get a unique ID. this allows me to use all inputs in my form action, which is update.
my View syntax:
<?php if(isset($records)) {?>
<table id="hor-minimalist-a">
<tr>
<th> </th><th> </th><th>Customer Name</th><th>postalcode</th>
<tr>
<?php if(isset($records)) : foreach ($records as $row) : ?>
<tr>
<td>
<?php echo anchor('masterdata/confirm_delete_customer/'.$row->id, img(array('src'=>'images/delete_icon.png','border'=>'0','alt'=>'Delete'))); ?>
</td>
<td>
<input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>">
</td>
<td>
<input class="inputwide" type="text" name="customer_name_<?php echo $row->id ?>" id="customer_name_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" >
</td>
<td>
<input class="inputnarrow" type="text" name="postalcode_<?php echo $row->id ?>" id="postalcode_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" >
</td>
</tr>
<?php endforeach ; ?>
</table>
<input type="submit" value="Update Checked Customers">
<?php endif; ?>
<?php echo form_close(); ?>
<?php } else {?>
<h4 id="warning"> No Customers currently in database</h4>
<?php } ?>
as you can see the input name's and id's are then unique.
My controller syntax is below:
function manage_customers()
{
$data['title']="Manage Customers";
//query model to get data results for form
$data=array();
if($query=$this->model_master_data->get_customer_records()){
$data['records']=$query;
}
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_manage_customers",$data);
$this->load->view("master_data/view_master_data_footer");
$editcustomer = $this->input->post('editcustomer');
// single update - working
if( $this->input->post('editcustomer') != false )
{
foreach ($editcustomer as $row_id)
{
$data = array(
'postalcode' => $this->input->post('postalcode_'.$row_id),
'customer_name' => $this->input->post('customer_name_'.$row_id) );
$this->model_master_data->update_customer_records( $row_id, $data );
}
$this->session->set_flashdata('dbaction', 'Selected Records have been updated successfully');
redirect('masterdata/manage_customers', 'refresh');
}
}
How do I make use of the codeigniter validation class to ensure the users modify the input boxes with credible data?
How can the
$this->form_validation->set_rules("primary_contact_tell","Contact Person tell","required|xss_clean|min_length[10]|max_length[14]");
reference the correct dynamic name of the input field? form currently has only customer name and postal code but need to add the rest of the fields.
Thanks in advance, as always.

You can loop through your $records in controller as you are doing it in view to achieve dynamic input validation rules.
foreach($records as $row)
{
$this->form_validation->set_rules("customer_name_" . $row->id, "Customer name", "required|xss_clean|min_length[10]|max_length[14]");
$this->form_validation->set_rules("postalcode_" . $row->id, "Customer name", "required|xss_clean|min_length[10]|max_length[14]");
}
Edit:
Think a little. I don't have ability to check what variables in your controller are. As far as I know basing on code you wrote here, this should be working:
foreach($editcustomer as $row_id)
{
$this->form_validation->set_rules("customer_name_" . $row_id, "Customer name", "required|xss_clean|min_length[10]|max_length[14]");
$this->form_validation->set_rules("postalcode_" . $row_id, "Customer name", "required|xss_clean|min_length[10]|max_length[14]");
}

Working solution, big thanks to #yabol on this one. I still need to clean up the syntax a little but desired functionality working.
View
<?php
$attributes=array(
'name'=>'updatecustomer',
'id'=>'updatecustomer',
);
echo form_open('masterdata/manage_customers',$attributes);
?>
<div id="validation_failed">
<?php
echo validation_errors();
?>
</div>
<?php if(isset($records)) {?>
<table id="hor-minimalist-a">
<tr>
<th> </th><th> </th><th>Customer Name</th><th>Address Line 1</th><th>Address Line 2</th><th>Suburb</th><th>City</th><th>Postal Code</th><th>Contact Name</th><th>Contact Email</th><th>Contact Tel</th>
<tr>
<?php if(isset($records)) : foreach ($records as $row) : ?>
<tr>
<td>
<?php echo anchor('masterdata/confirm_delete_customer/'.$row->id, img(array('src'=>'images/delete_icon.png','border'=>'0','alt'=>'Delete'))); ?>
</td>
<td>
<input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>">
</td>
<td>
<input class="inputwide" type="text" name="customer_name_<?php echo $row->id ?>" id="customer_name_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" >
</td>
<td>
<input class="inputmedium" type="text" name="address_line_1_<?php echo $row->id ?>" id="address_line_1_<?php echo $row->id ?>" value="<?php echo $row->address_line_1 ; ?>" >
</td>
<td>
<input class="inputmedium" type="text" name="address_line_2_<?php echo $row->id ?>" id="address_line_2_<?php echo $row->id ?>" value="<?php echo $row->address_line_2 ; ?>" >
</td>
<td>
<input class="inputmedium" type="text" name="suburb_<?php echo $row->id ?>" id="suburb_<?php echo $row->id ?>" value="<?php echo $row->suburb ; ?>" >
</td>
<td>
<input class="inputmedium" type="text" name="city_<?php echo $row->id ?>" id="city_<?php echo $row->id ?>" value="<?php echo $row->city ; ?>" >
</td>
<td>
<input class="inputnarrow" type="text" name="postalcode_<?php echo $row->id ?>" id="postalcode_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" >
</td>
<td>
<input class="inputmedium" type="text" name="primary_contact_name_<?php echo $row->id ?>" id="primary_contact_name_<?php echo $row->id ?>" value="<?php echo $row->primary_contact_name ; ?>" >
</td>
<td>
<input class="inputmedium" type="text" name="primary_contact_email_<?php echo $row->id ?>" id="primary_contact_email_<?php echo $row->id ?>" value="<?php echo $row->primary_contact_email ; ?>" >
</td>
<td>
<input class="inputmedium" type="text" name="primary_contact_tell_<?php echo $row->id ?>" id="primary_contact_tell_<?php echo $row->id ?>" value="<?php echo $row->primary_contact_tell ; ?>" >
</td>
</tr>
<?php endforeach ; ?>
</table><br>
<input type="submit" value="Update Checked Customers">
<?php endif; ?>
<?php echo form_close(); ?>
Controller
function manage_customers()
{
$data['title']="Manage Customers";
//query model to get data results for form
$data=array();
if($query=$this->model_master_data->get_customer_records()){
$data['records']=$query;
}
$editcustomer = $this->input->post('editcustomer');
if( $this->input->post('editcustomer') != false ){
foreach($editcustomer as $row_id)
{
$this->form_validation->set_rules("customer_name_" . $row_id, "Customer name", "required|min_length[6]");
$this->form_validation->set_rules("address_line_1_". $row_id,"`Address Line 1`","required|xss_clean|min_length[6]");
$this->form_validation->set_rules("address_line_2_". $row_id,"`Address Line 2`","xss_clean|min_length[6]");
$this->form_validation->set_rules("suburb_". $row_id,"`Suburb`","required|xss_clean|min_length[6]");
$this->form_validation->set_rules("city_". $row_id,"`City`","required|xss_clean|min_length[6]");
$this->form_validation->set_rules("postalcode_". $row_id,"`Postal Code`","required|xss_clean|min_length[4]|max_length[5]");
$this->form_validation->set_rules("primary_contact_name_". $row_id,"`Contact Person Name`","required|xss_clean|min_length[6]");
$this->form_validation->set_rules("primary_contact_email_". $row_id,"`Contact Person email`","required|valid_email|xss_clean");
$this->form_validation->set_rules("primary_contact_tell_". $row_id,"`Contact Person tell`","required|xss_clean|min_length[10]|max_length[14]");
}
}
if ($this->form_validation->run() == FALSE){
$data["message"]="";
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_manage_customers",$data);
$this->load->view("master_data/view_master_data_footer");
} else {
// single update - working
if( $this->input->post('editcustomer') != false )
{
foreach ($editcustomer as $row_id)
{
$data = array(
'customer_name' => $this->input->post('customer_name_'.$row_id),
'address_line_1' => $this->input->post('address_line_1_'.$row_id),
'address_line_2' => $this->input->post('address_line_2_'.$row_id),
'suburb' => $this->input->post('suburb_'.$row_id),
'city' => $this->input->post('city_'.$row_id),
'postalcode' => $this->input->post('postalcode_'.$row_id),
'primary_contact_name' => $this->input->post('primary_contact_name_'.$row_id),
'primary_contact_email' => $this->input->post('primary_contact_email_'.$row_id),
'primary_contact_tell' => $this->input->post('primary_contact_tell_'.$row_id),
);
$this->model_master_data->update_customer_records( $row_id, $data );
}
$this->session->set_flashdata('dbaction', 'Selected Records have been updated successfully');
redirect('masterdata/manage_customers', 'refresh');
}
}
}

Related

how to manipulate with codeigniter

How I get my data array from view? and how to insert in my database?
my view, I try much code but still an error, sorry about my question and my bad English
<form role="form" action="<?php echo base_url().'potongan/validasi'?>" action="GET">
<div class="box-body">
<div class="box-header with-border">
<div class="row">
<div class="col-xs-3">
<label >NIM</label>
<input type="text" class="form-control" name="nim" id="nim" placeholder="NIM" value="<?php echo $nime ?>" readonly>
</div>
<div class="col-xs-3">
<label >Nama Mahasiswa</label>
<input type="text" class="form-control" name="namamhs" placeholder="Nama Mahasiswa" value="<?php echo $nim[0]->namamhs ?>" disabled>
</div>
<div class="col-xs-3">
<label >Jurusan</label>
<input type="text" class="form-control" name="jurusan" placeholder="Jurusan" value="<?php echo $nim[0]->jurusan ?>" disabled>
</div>
</div>
<br>
<div class="form-group">
<label >Kode Beasiswa</label>
<input type="text" class="form-control " name="kdbeasiswa" value="<?php echo $kdbeasiswa ?>" readonly>
</div>
<div class="form-group">
<label >Keterangan</label>
<input type="text" class="form-control " name="keterangan" value="<?php echo $keterangan ?>" readonly>
</div>
<div>
<table class="table table-bordered table-striped">
<tr>
<th></th>
<th>Kode</th>
<th>Deskripsi</th>
<th>Tahun Akademik</th>
<th>semester</th>
<th>Tagihan</th>
<th>Beasiswa</th>
<th>Terbayar</th>
<th>Potongan</th>
</tr>
<?php foreach ($nim as $n): ?>
<tr>
<td>
<input type="hidden" class="form-control " name="id[]" value="<?php echo $n->thakad.$n->smtakad.$n->kdkeu; ?>" maxlength="3" >
</td>
<td><input type="hidden" class="form-control " name="kdkeu[]" value="<?php echo $n->kdkeu; ?>" maxlength="3" ><?php echo $n->kdkeu; ?></td>
<td><?php echo $n->deskeu; ?></td>
<td><input type="hidden" class="form-control " name="thakad[]" value="<?php echo $n->thakad; ?>" maxlength="3" ><?php echo $n->thakad; ?></td>
<td><input type="hidden" class="form-control " name="smtakad[]" value="<?php echo $n->smtakad; ?>" maxlength="3" ><?php echo $n->smtakad_view; ?></td>
<td><?php echo $n->tagihan; ?></td>
<td><?php echo $n->beasiswa; ?></td>
<td><?php echo $n->terbayar; ?></td>
<td>
<input type="text" class="form-control " name="prosentase_bea[]" value="<?php echo $n->potongan; ?>" maxlength="3">
</td>
</tr>
<?php endforeach ?>
</table>
</div>
</div>
<br>
<button class="btn btn-info" style="float: right;">submit</button>
</div>
<!-- /.box-body -->
</form>
my controller looks like this when I submit no data insert to the database and error like
Filename: C:/xampp/htdocs/tagihan/system/database/DB_query_builder.php
Line Number: 1498
$data = array();
$count = count($this->input->post('device'));
for($i=0; $i<$count; $i++) {
$data[] = array(
$thakad= $this->input->get('thakad'),
$smtakad= $this->input->get('smtakad'),
$kb = $this->input->get('kdbeasiswa'),
$kk = $this->input->get('kdkeu'),
$nim= $this->input->get('nim'),
$pb = $this->input->get('prosentase_bea'),
$status = 1,
$user = $this->session->userdata('username'),
$kt = $this->input->get('keterangan'),
$curentDate = date("Y-m-d"));
}
$this->db->insert_batch('keu_beasiswapermhs',$data);
redirect('http://localhost/tagihan/potongan');
Make a habit using the proper MVC
VIEW
<form action="URL" method="post">
</form>
CONTROLLER
$this->model->function_name();
redirect('URL');
MODEL
function function_name(){
$dataArr = array(
'field_name'=>$this->input->post('form_input_name'),
);
$this->db->INSERT('tbl_name',$dataArr);
}
You have fields like "namamhs" and "jurusan" that are not being put into the db.
You do not need for or while loops to insert data
You are not assigning what fields in your db each posted item goes to. Your controller has no idea where each value should be inserted. Do not use get, you should use post. Here is an example for your controller you can adapt to your form:
$data = [
'url' => $this->input->post('url'),
'name' => $this->input->post('name'),
'genre' => $this->input->post('genre'),
'counter' => $this->input->post('counter'),
'type' => $this->input->post('type')
];
where 'url' is a field in your db that you are putting $this->input->post('url') into.
Next you check the values:
$this->form_validation->set_rules('url', 'url', 'required|trim');
$this->form_validation->set_rules('name', 'Name', 'required|trim|callback_trims');
$this->form_validation->set_rules('genre', 'Genre', 'required|trim');
$this->form_validation->set_rules('counter', 'Counter', 'numeric');
$this->form_validation->set_rules('type', 'Type', 'required|min_length[3]');
If the values pass the validation, inserting is simple
if( $this->form_validation->run() == FALSE) {
echo validation_errors();
}else
{
$this->db->insert('music', $data);
}
You do not need batch insert. Just use insert
Your $data array is empty...always check if array not empty before insert batch...that cause error DB_query_builder.php Line Number: 1498

Magento Unable to post review

I am trying to add a review to a product in magento. However, submitting the review form of a product in the frontend gives me an "Unable to post review message".
I haven't found a solution to the problem, that's why im calling in your help :)
This is the code from my review form:
<div class="form-add">
<h2><?php echo $this->__('Write Your Own Review') ?></h2>
<?php if ($this->getAllowWriteReviewFlag()): ?>
<form action="<?php echo $this->getAction() ?>" method="post" id="review-form">
<?php echo $this->getBlockHtml('formkey'); ?>
<?php echo $this->getChildHtml('form_fields_before') ?>
<h3><?php echo $this->__("You're reviewing:"); ?>
<span><?php echo $this->escapeHtml($this->getProductInfo()->getName()) ?></span>
</h3>
<div class="fieldset">
<?php if ($this->getRatings() && $this->getRatings()->getSize()): ?>
<h4><?php echo $this->__('How do you rate this product?') ?> <em class="required">*</em></h4>
<span id="input-message-box"></span>
<table class="data-table review-summary-table ratings" id="product-review-table">
<col width="1"/>
<col/>
<col/>
<col/>
<col/>
<col/>
<thead>
<tr>
<th> </th>
<th>
<div class="rating-box">
<span class="rating-number">1</span>
<span class="rating nobr"
style="width:20%;"><?php echo $this->__('1 star') ?></span>
</div>
</th>
<th>
<div class="rating-box">
<span class="rating-number">2</span>
<span class="rating nobr"
style="width:40%;"><?php echo $this->__('2 star') ?></span>
</div>
</th>
<th>
<div class="rating-box">
<span class="rating-number">3</span>
<span class="rating nobr"
style="width:60%;"><?php echo $this->__('3 star') ?></span>
</div>
</th>
<th>
<div class="rating-box">
<span class="rating-number">4</span>
<span class="rating nobr"
style="width:80%;"><?php echo $this->__('4 star') ?></span>
</div>
</th>
<th>
<div class="rating-box">
<span class="rating-number">5</span>
<span class="rating nobr"
style="width:100%;"><?php echo $this->__('5 star') ?></span>
</div>
</th>
</tr>
</thead>
<tbody>
<?php foreach ($this->getRatings() as $_rating): ?>
<tr>
<th><?php echo $this->escapeHtml($_rating->getRatingCode()) ?></th>
<?php foreach ($_rating->getOptions() as $_option): ?>
<td class="value"><label
for="<?php echo $this->escapeHtml($_rating->getRatingCode()) ?>_<?php echo $_option->getValue() ?>"><input
type="radio" name="ratings[<?php echo $_rating->getId() ?>]"
id="<?php echo $this->escapeHtml($_rating->getRatingCode()) ?>_<?php echo $_option->getValue() ?>"
value="<?php echo $_option->getId() ?>" class="radio"/></label></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<input type="hidden" name="validate_rating" class="validate-rating" value=""/>
<script type="text/javascript">decorateTable('product-review-table')</script>
<?php endif; ?>
<ul class="form-list">
<li>
<label for="review_field"
class="required"><em>*</em><?php echo $this->__('Let us know your thoughts') ?></label>
<div class="input-box">
<textarea name="detail" id="review_field" cols="5" rows="3"
class="required-entry"><?php echo $this->escapeHtml($data->getDetail()) ?></textarea>
</div>
</li>
<li class="inline-label">
<label for="summary_field"
class="required"><em>*</em><?php echo $this->__('Summary of Your Review') ?></label>
<div class="input-box">
<input type="text" name="title" id="summary_field" class="input-text required-entry"
value="<?php echo $this->escapeHtml($data->getTitle()) ?>"/>
</div>
</li>
<li class="inline-label">
<label for="nickname_field"
class="required"><em>*</em><?php echo $this->__("What's your nickname?") ?></label>
<div class="input-box">
<input type="text" name="nickname" id="nickname_field" class="input-text required-entry"
value="<?php echo $this->escapeHtml($data->getNickname()) ?>"/>
</div>
</li>
</ul>
</div>
<div class="buttons-set">
<button type="submit" title="<?php echo $this->__('Submit Review') ?>" class="button">
<span><span><?php echo $this->__('Submit Review') ?></span></span></button>
</div>
</form>
<script type="text/javascript">
//<![CDATA[
var dataForm = new VarienForm('review-form');
Validation.addAllThese(
[
['validate-rating', '<?php echo $this->__('Please select one of each of the ratings above') ?>', function (v) {
var trs = $('product-review-table').select('tr');
var inputs;
var error = 1;
for (var j = 0; j < trs.length; j++) {
var tr = trs[j];
if (j > 0) {
inputs = tr.select('input');
for (i in inputs) {
if (inputs[i].checked == true) {
error = 0;
}
}
if (error == 1) {
return false;
} else {
error = 1;
}
}
}
return true;
}]
]
);
//]]>
</script>
<?php else: ?>
<p class="review-nologged" id="review-form">
<?php echo $this->__('Only registered users can write reviews. Please, log in or register', $this->getLoginLink(), Mage::helper('customer')->getRegisterUrl()) ?>
</p>
<?php endif ?>
Looks like the standard rwd theme layout. If i delete my custom one so the rwd default pops up it still doesn't work.
The url that i'm posting from is as following:
https://www.example.com/review/product/post/id/73/
Hope someone can help me in the right direction. It's driving me crazy!!!
I solved the problem myself.
It had nothing to do with the reviews themselves. My first thought was a module that blocked access to the reviews, and after searching in de controller for reviews we found out that the problem was a module that couldn't have a certain length of characters stored in the database. This somehow conflicted with the reviews post.
After contacting the developers of that module we were able to solve the problem. The module was MailPlus and we solved it by going to the module its configuration and edited the following for Main website:
After changing this the reviews form started working again.
Thanks everybody for helping me!

Form button not working properly in IE11(and probably more)

I've made page where you can get an overview over invoices.
Everything works great, except from with Internet Explorer...
When I click the see order button, nothing happens in IE.
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" onSubmit="window.location.reload()">
<tr><td><?php
$Date = $row['date'];
echo $Date;
?></td><td><?php echo $row['autoid'];?></td>
<input name='implodehideu' type='hidden' value='<?php echo $row['implodeuke'];?>;'>
<input name='implodehidew' type='hidden' value='<?php echo $row['implodehelg'];?>;'>
<input name='personid' type='hidden' value='<?php echo $row['primeid'];?>;'>
<input name='invoiceid' type='hidden' value='<?php echo $row['autoid'];?>'>
<td><input type="submit" name="seeorder" id="submit" value="Order overview"></td>
<td><?php echo $row['firstname'] . " " . $row['surname'];?></td>
<td><?php echo $row['amount'];?></td>
<?php
if ($paidboolean)
echo "<td bgcolor='#3AA849'>YES</td>";
else
echo "<td bgcolor='#e94336'>NO</td>";
if (!$paidboolean) {
?>
<td><input type="submit" name="confirm" id="submit" value="Mark as paid"></td>
<?php
} else {
?>
<td><input type="submit" name="notconfirm" id="submit" value="Mark as not paid"></td>
<?php
}
?>
<td><input type="submit" name="slett" id="submit" value="Slett"></td>
</form>
<?php
if (isset($_POST['seeorder'])) {
.....Things to show up
}
Try to set the id argument of the submit button to something else than 'submit'. That helped me with the same problem.
<input type="submit" name="slett" id="btnSubmit" value="Slett" />

Codeigniter How to save data from a form to a database

Codeigniter
Hi. I want to send my form data to a database. The data are checked ok but what to do next?
My controlls
<?php
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Imie', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('surname', 'Nazwisko', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('number', 'Numer telefonu', 'required|alpha_numeric|max_length[10]');
$this->form_validation->set_rules('brand', 'Marka', 'required|alpha');
$this->form_validation->set_rules('model', 'Model', 'required|alpha');
$this->form_validation->set_rules('year', 'Rok produkcji', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('km', 'Ilosc KM', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('licenceseplate', 'Tablica rejestracyjna', 'required|max_length[15]');
$this->form_validation->set_rules('description', 'Opis', 'required|max_length[300]');
$this->form_validation->set_rules('city', 'Miasto', 'required|max_length[30]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
}
?>
My view
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo form_open('form'); ?>
<h5>Wpisz swoje imiÄ™</h5>
<?php echo form_error('name'); ?>
<input type="text" name="name" value="<?php echo set_value('name'); ?>" size="50" />
<h5>Nazwisko</h5>
<?php echo form_error('surname'); ?>
<input type="text" name="surname" value="<?php echo set_value('surname'); ?>" size="50" />
<h5>Email Address</h5>
<?php echo form_error('email'); ?>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
<h5>Numer telefonu</h5>
<?php echo form_error('number'); ?>
<input type="text" name="number" value="<?php echo set_value('number'); ?>" size="50" />
<h5>Marka</h5>
<?php echo form_error('brand'); ?>
<input type="text" name="brand" value="<?php echo set_value('brand'); ?>" size="50" />
<h5>Model</h5>
<?php echo form_error('model'); ?>
<input type="text" name="model" value="<?php echo set_value('model'); ?>" size="50" />
<h5>Rok produkcji</h5>
<?php echo form_error('year'); ?>
<input type="text" name="year" value="<?php echo set_value('year'); ?>" size="50" />
<h5>km</h5>
<?php echo form_error('km'); ?>
<input type="text" name="km" value="<?php echo set_value('km'); ?>" size="50" />
<h5>Rejestracja </h5>
<?php echo form_error('licenceseplate'); ?>
<input type="text" name="licenceseplate" value="<?php echo set_value('licenceseplate'); ?>" size="50" />
<h5>Opis </h5>
<?php echo form_error('description'); ?>
<input type="text" name="description" value="<?php echo set_value('description'); ?>" size="50" />
<h5>Miasto </h5>
<?php echo form_error('city'); ?>
<input type="text" name="city" value="<?php echo set_value('city'); ?>" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
I don't expect the final solution, but I need help what is the next step
My datebase:
After the form validation you can save data
In your controller
if ($this->form_validation->run() == FALSE){
$this->load->view('myform');
}
else {
$this->your_model->save($data);
}
your_model.php
public function save($data) {
$this->db->set($data);
$this->db->insert(table name here);
$this->db->insert_id();
}
ok
My view looks like this:
<?php
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Imie', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('surname', 'Nazwisko', 'required|min_length[5]|max_length[25]|required|alpha');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('number', 'Numer telefonu', 'required|alpha_numeric|max_length[10]');
$this->form_validation->set_rules('brand', 'Marka', 'required|alpha');
$this->form_validation->set_rules('model', 'Model', 'required|alpha');
$this->form_validation->set_rules('year', 'Rok produkcji', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('km', 'Ilosc KM', 'required|alpha_numeric|max_length[5]');
$this->form_validation->set_rules('licenceseplate', 'Tablica rejestracyjna', 'required|max_length[15]');
$this->form_validation->set_rules('description', 'Opis', 'required|max_length[300]');
$this->form_validation->set_rules('city', 'Miasto', 'required|max_length[30]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
/*$this->load->view('formsuccess');*/
$this->load->model ( 'form' );
$this->form->save($data);
}
}
}
?>
my model
class form extends Model{
function form(){
parent::Model();
$this->load->helper('url');
}
public function save($data) {
$this->db->set($data);
$this->db->insert(form);
$this->db->insert_id();
}
}
table in date base name form
I have no idea how you want it
You have to send the data to the model
There you can use active record or normal query to save the data
It must be noted you need to include the model in the controller
$this->load->model ( 'Your model name' ); this should be included in the controller
$this->yourmodelname->functioninmodel this is the controller function call to the model

Codeigniter update mysql table data from form with checkbox

New to codeigniter and trying to get my head around updating checked rows from a user form.
My view generates a form with MySQL data as below:
<?php
echo form_open('masterdata/update_customers');
?>
<table>
<tr>
td> </td><td>Customer Name</td><td>postalcode</td>
<tr>
<?php if(isset($records)) : foreach ($records as $row) : ?>
<tr>
<td>
<input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>">
</td>
<td>
<input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" >
</td>
<td>
<input type="text" name="postalcode_<?php echo $row->id ?>" id="postalcode_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" >
</td>
</tr>
<?php endforeach ; ?>
</table>
<input type="submit" value="Update Selected">
<?php else : ?>
<h2> No Records Found</h2>
<?php endif; ?>
<?php echo form_close(); ?>
This works perfectly well as I get my unique name and values for all input fields.
My issue is now trying to pass the selected checkboxes and there values to codeigniter and get it to update each row.
traditionally I would use foreach($_POST['editcustomer'] as $editcustomer){ but cant get my head around this in codeigniter.
my controller function, update_customers at this stage is very basic:
function update_customers()
{
$this->form_validation->set_rules("customer_name","`Customer Name`","required|min_length[6]|xss_clean");
$this->form_validation->set_rules("postalcode","`Postal Code`","required|xss_clean|min_length[6]");
if ($this->form_validation->run() == FALSE){
$data["message"]="";
$data['title']="Master Data Home Page";
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_manage_customers");
$this->load->view("master_data/view_master_data_footer");
} else {
$data = array(
'customer_name' => $this->input->post('customer_name'),
'postalcode' => $this->input->post('postalcode'),
);
$this->model_master_data->update_customer_records($data);
$this->customers_updated();
}
}
My model function, update_customer_records is:
function update_customer_records($data)
{
$this->db->where('id',$this->input->post('id'));
$this->db->update('customers',$data);
}
I know there is quite a bit missing here, like processing only the rows checked. but not sure how to add this in. secondly, my unique name and id's being generated, as per my view is:
name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>"
how do get the update to apply to the unique form input name? I need to append the prefix customername_ and postalcode_ to the input->post(?) value?
Any direction or assistance here will be appreciated. thanks a million in advance.
With CodeIgniter, if you have to get checkbox values, you have to use $this->input->post('name_of_your_checkbox'). It's return an array.
For example, if you have that in a view:
<input type="checkbox" name="editcustomer[]" value="1" />
<input type="checkbox" name="editcustomer[]" value="2" />
<input type="checkbox" name="editcustomer[]" value="3" />
From the controller:
public function update_customers() {
$editcustomer = $this->input->post('editcustomer');
}
Return:
Array {
[0] => 1
[1] => 2
[2] => 3
}