how to manipulate with codeigniter - postgresql

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

Related

Multiple form_open with button

I have several buttons which should make different things.
To one, a button should make one entry in table 1 and another in table 2.
But if I do it as in the code below.
He always makes an entry in Table 1.
How can I assign the respective buttons?
<?php echo form_open('Karte/insert_artikel'); ?>
<?php echo form_open('Karte/insert_trade'); ?>
<input type="hidden" name="add_karte_id" value="<?php echo $edition_views['tb_karte_id']; ?>">
<input type="hidden" name="add_karten_stats_id" value="<?php echo $edition_views['tb_karte_karten_stats']; ?>">
<input type="hidden" name="add_karte_sprache" value="<?php echo $edition_views['tb_sprache_id']; ?>">
<input type="hidden" name="add_karte_qualitat" value="<?php echo $edition_views['tb_qualitat_id']; ?>">
<input type="hidden" name="add_karte_auflage" value="<?php echo $edition_views['tb_auflage_id']; ?>">
<input type="hidden" name="add_karte_edition_stats" value="<?php echo $edition_views['tb_karte_edition_stats']; ?>">
<input type="hidden" name="add_karte_datum" value="<?php $date = date_create($edition_views['tb_karte_datum']); echo date_format($date,"Y-m-d H:i:s");?>">
<tr>
<td><?php echo $edition_views['tb_edition_kurzel']; ?>-<?php echo $edition_views['tb_sprache_kurzel']; ?><?php echo $edition_views['tb_edition_stats_nummer']; ?></td>
<td><button class="btn btn-default" data-toggle="tooltip" data-placement="top" title="<?php echo $edition_views['tb_qualitat_name']; ?>"><img src="<?php echo base_url(); ?>assets/images/site/qualitat/<?php echo $edition_views['tb_qualitat_id']; ?>.png"/> <?php echo $edition_views['tb_qualitat_short']; ?></button></td>
<td><button class="btn btn-default" data-toggle="tooltip" data-placement="top" title="<?php echo $edition_views['tb_rare_name']; ?>"><?php echo $edition_views['tb_rare_short']; ?></button></td>
<td><?php echo $edition_views['tb_auflage_name_de']; ?></td>
<td><?php $date = date_create($edition_views['tb_karte_datum']); echo date_format($date,"d.m.Y");?></td>
<td><?php echo str_pad($edition_views['tb_karte_anzahl'], 2, '0', STR_PAD_LEFT); ?></td>
<td>
<?php
switch($edition_views['tb_karte_versuche'])
{
case (0):
echo "<FONT COLOR='#000000'>".$edition_views['tb_karte_versuche']."</FONT>";
break;
case (1):
echo "<strong><FONT COLOR='#D7DF01'>".$edition_views['tb_karte_versuche']."</FONT></strong>";
break;
case (2):
echo "<strong><FONT COLOR='#298A08'>".$edition_views['tb_karte_versuche']."</FONT></strong>";
break;
case (3):
echo "<strong><FONT COLOR='#DF0101'>".$edition_views['tb_karte_versuche']."</FONT></strong>";
break;
}
?>
</td>
<td>
<div class="input-group">
<span class="input-group-btn">
<button type="button" class="btn btn-default btn-number" disabled="disabled" data-type="minus" data-field="add_karte_menge[<?php echo $edition_views['tb_karte_id']; ?>]"><span class="glyphicon glyphicon-minus"></span></button>
</span>
<input type="text" name="add_karte_menge[<?php echo $edition_views['tb_karte_id']; ?>]" class="form-control input-number" value="1" min="1" max="<?php echo $edition_views['tb_karte_anzahl']; ?>">
<span class="input-group-btn">
<button type="button" class="btn btn-default btn-number" data-type="plus" data-field="add_karte_menge[<?php echo $edition_views['tb_karte_id']; ?>]"><span class="glyphicon glyphicon-plus"></span></button>
<button type="submit" class="btn btn-default" data-toggle="tooltip" data-placement="top" title="Verkauf Hinzufügen"><span class="glyphicon glyphicon-shopping-cart" aria-hidden="true"></span></button>
<button type="submit" class="btn btn-default" data-toggle="tooltip" data-placement="top" title="Tausch Hinzufügen"><span class="glyphicon glyphicon-transfer" aria-hidden="true"></span></button>
<button type="submit" class="btn btn-default" data-toggle="tooltip" data-placement="top" title="Zur Sammlung"><span class="glyphicon glyphicon-gift" aria-hidden="true"></span></button>
<button type="submit" class="btn btn-default" data-toggle="tooltip" data-placement="top" title="Bearbeiten"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></button>
<button type="submit" class="btn btn-default" data-toggle="tooltip" data-placement="top" title="Verloren"><span class="glyphicon glyphicon-question-sign" aria-hidden="true"></span></button>
</span>
</div>
</td>
</tr>
<?php echo form_close(); ?>
<?php echo form_close(); ?>
You cannot nest forms.
You have a couple of options here:
A) Have separate forms (not nested) with a button in each form. Each form would be directed to its own controller which redirects back to the same controller or view afterwards
B) Have a single form and a single controller, but attach a different name to each button - i.e.
<form action="/some-controller" method="post">
<button name="do_something" value="1">Do Something</button>
<button name="do_something_different" value="1">Do Something Different</button>
</form>
Then in your controller you can look for the posted variables do_something and do_something_different to determine which button the user clicked and determine your action accordingly.

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 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
}

codeigniter form validation for dynamic form input names

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');
}
}
}

mark the input box red if the required field of a form is not filled

i'm using Codeigniter. i have a form which takes information about users. What i want to do is to check whether all the required fields are filled up or not.If any of the required fields is not filled up i want mark that input box red. Right now my codes only check if the required fields are filled up or not. if not it says the "field is required" but how to mark input box.I'm a bit confused how to do this thing.Can somebody help me out with a little hint. Thanks.
the view for my form:
<?php
$attributes = array('class' => '', 'id' => '');
echo form_open('register', $attributes); ?>
<p>
<label for="name">Name <span class="required">*</span></label>
<?php echo form_error('name'); ?>
<br /><input id="name" type="text" name="name" value="<?php
echo set_value('name'); ?>"
</p>
<p>
<label for="username">Username <span class="required">*</span></label>
<?php echo form_error('username'); ?>
<br /><input id="username" type="text" name="username" value="<?php echo set_value('username'); ?>"
</p>
<p>
<label for="password">Password <span class="required">*</span></label>
<?php echo form_error('password'); ?>
<br /><input id="password" type="password" name="password" value="<?php echo set_value('password'); ?>"
</p>
<p>
<label for="email">Email <span class="required">*</span></label>
<?php echo form_error('email'); ?>
<br /><input id="email" type="text" name="email" value="<?php echo set_value('email'); ?>"
</p>
<p>
<label for="phone">Phone</label>
<?php echo form_error('phone'); ?>
<br /><input id="phone" type="text" name="phone" value="<?php echo set_value('phone'); ?>"
</p>
</p>
<p>
<input type="submit" value="Submit information" class="formbutton"/>
</p>
<?php echo form_close(); ?>
Generally, I'd recommend creating an textInputError class where you adjust the input style, and then apply it based on the existence of the error...
class="<?php echo (form_error('username') ? 'textInputError' : '') ?>"
in place in the input element...
<input id="username" type="text" name="username" value="<?php echo set_value('username'); ?>" class="<?php echo (form_error('username') ? 'textInputError' : '') ?>">