CodeIgniter is giving me a Disallowed Key Characters error. even after successfully inserting data in database.
here is my code.:
Controller:
class applyleavectrl extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('fill_in','fill'); //model fill in
$this->load->model('mdl_employee','emp');
$id = $this->session->userdata('id');
}
public function index()
{
$id = $this->session->userdata('id');
$this->form_validation->set_rules('leave_type', 'Leave Type', 'required|callback_check_select');
$this->form_validation->set_message('check_select', 'You need to select a leave type');
$this->form_validation->set_rules('from_date', 'From Date', 'required');
$this->form_validation->set_rules('to_date', 'To Date', 'required');
$this->form_validation->set_error_delimiters('<font size="1" face="helvetica" color="red">', '</font>');
if ($this->form_validation->run() == FALSE)
{
$data['employee_header_menus'] = $this->load->view('employee_header_menus', NULL, TRUE);
$data['employee_header_logout'] = $this->load->view('employee_header_logout', NULL, TRUE);
$data['leave_type'] = $this->fill->fill_leave_type();
$this->load->view('employee/applyleave', $data);
}
else
{
$id = $this->session->userdata('id');
$P1 = $this->input->post('leave_type');
$P2 = $this->input->post('from_date');
$P3 = $this->input->post('to_date');
$P4 = $this->input->post('comments');
$this->emp->insert_employee_leave($id, $P1, $P2, $P3, $P4);
redirect('employee/applyleavectrl');
}
}
function check_select($post_string)
{
return $post_string == '0' ? FALSE : TRUE;} }
model
public function insert_employee_leave($id, $P1, $P2, $P3, $P4)
{
$value = array(
'empid' => $id,
'leave_type' => $P1,
'from_date' => $P2,
'to_date' => $P3,
'comments' => $P4);
$this->db->insert('employee_leave', $value);
}
and the view is
<?php
$attributes = array('autocomplete' => "off");
echo form_open('employee/applyleavectrl', $attributes);
?>
<div class="row">
<div class="span3 offset2">
<label>Leave Type</label>
<select name="leave_type">
<option selected value="0">Select Leave Type</option>
<?php foreach($leave_type as $row): ?>
<option value="<?php echo $row['ID']; ?>"><?php echo $row['type']; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="span3 offset2">
<label>From Date</label>
<input id="from_date" name="from_date" type="date">
</div>
</div>
<div class="row">
<div class="span3 offset2">
<label >Leave Balance</label>
<input name="leave_bal" id='leave_bal' type="text" disabled>
</div>
<div class="span3 offset2">
<label>To Date</label>
<input id="to_date" name="to_date" type="date">
</div>
</div>
<div class="row">
<div class="span2 offset2">
View Details
</div>
</div>
</br>
<div class="row">
<div class="span3 offset2">
<label>Comments</label>
<textarea name="comments" rows="3"></textarea>
</div>
</div>
<div><?php echo validation_errors(); ?></div>
<div class="line"></div>
<div class="row">
<div class="span1 offset2">
<button type="submit" class="button-red">Apply</button>
</div>
</div>
<?php echo form_close(); ?>
if you are using
echo form_open('employee/applyleavectrl', $attributes);
in your view then you should have a controller like employee not applyleavectrl.
change it and let me know if its solved your problem or not.
Related
I'm attempting my hand at ajax which i'm not the greatest at, my code for the page:
<?php
include('includes/db_connection.php');
include('includes/sessions.php');
include('includes/functions.php');
include('includes/header.php');
include('includes/navbar-logged.php');
require_once('api-sape.php');
// init sape
$sape = new SapeApi();
$userId = $sape->login(SAPE_USER, SAPE_PASS);
?>
<style type="text/css">
.modal-loading {
top: 0;
left: 0;
position: fixed;
z-index: 999;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.1);
display: none;
}
.modal-loading .loading {
width: 64px;
height: 64px;
margin: 200px auto;
}
</style>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<div class="modal-loading"><div class="loading"><img src="http://wz.sera5902.myjino.ru/loading.gif"></div></div>
<?php
// isset
if (isset($_POST['search'])) {
// show var_dump
if ($member == SAPE_USER) { showPre($_POST); }
// try...
try
{
// vars
$sapeURLID = $_POST['url_id'];
$sapeDepth = $_POST['domain_level'];
$sapeInInG = $_POST['indexed_in_google'];
$sapeOrder = $_POST['order'];
$sapePLeve = $_POST['page_level'];
$sapeSdays = $_POST['search_days'];
$sapeKwdos = $_POST['keyword'];
$sapeKwpla = $_POST['keyword_placement'];
$sapeSdmoz = $_POST['site_in_dmoz'];
$sapeExtLi = $_POST['external_links'];
$sapePriFr = $_POST['price_from'];
$sapePriTo = $_POST['price_to'];
$sapeMajC1 = $_POST['mj_cf_1'];
$sapeMajC2 = $_POST['mj_cf_2'];
$sapeMajT1 = $_POST['mj_tf_1'];
$sapeMajT2 = $_POST['mj_tf_2'];
$sapePageN = $_POST['page_number'];
// arrays
$zones = array();
if (isset($_POST['zones'])) {
if (count($_POST['zones']) > 0) {
foreach ($_POST['zones'] as $key => $value) {
$zones[] = $value;
}
}
}
// arrays
$cats = array();
if (isset($_POST['categories'])) {
if (count($_POST['categories']) > 0) {
foreach ($_POST['categories'] as $key => $value) {
$cats[] = $value;
}
}
}
// xml-rpc query
$userSearch = $sape->search($sapeURLID, array("order"=>$sapeOrder,"domain_level"=>$sapeDepth,"domain_zones"=>$zones,"categories"=>$cats,"flag_blocked_in_google"=>$sapeInInG,"level_from"=>$sapePLeve,"days_old_whois"=>$sapeSdays,"words"=>str_replace(" ", "+", $sapeKwdos),"words_type"=>$sapeKwpla,"in_dmoz"=>$sapeSdmoz,"ext_links"=>$sapeExtLi,"pr_from"=>$sapePriFr,"pr_2"=>$sapePriTo,"mj_cf_1"=>$sapeMajC1,"mj_cf_2"=>$sapeMajC2,"mj_tf_1"=>$sapeMajT1,"mj_tf_2"=>$sapeMajT2), $sapePageN);
// is there any results?
if (count($userSearch) == 0) {
stderr("No results with those filters, try again with more options selected.");
}
// loop results
foreach ($userSearch as $val1) {
// ignore blank urls
if (!empty($val1['url'])) {
if ($member == SAPE_USER) {
echo "<pre>";
echo "<br />";
echo "<b>ID:</b> " . $val1['id'];
echo "<br />";
echo "<b>URL:</b> " . $val1['url'];
echo "<br />";
echo "<br />";
}
foreach ($val1['pages'] as $key => $val2) {
$urlCheck = DB::getInstance()->selectOne(
'
SELECT DISTINCT site_sape_url, site_uri
FROM `sites`
WHERE `site_sape_url` = :site_sape_url
AND `site_uri` = :site_uri',
[
'site_sape_url' => $val1['url'],
'site_uri' => $val2['uri']
]
);
// count check
if (!count($urlCheck)) {
// ten op for the site level
$level = ($val2['uri'] == "/") ? '1' : $val2['level'];
// get site ip
$ip = gethostbyname(parse_url($val1['url'], PHP_URL_HOST));
// insertion
DB::getInstance()->insert(
'sites',
[
'site_sape_id' => $val1['id'],
'site_sape_url' => $val1['url'],
'site_uri_id' => $val2['id'],
'site_uri' => $val2['uri'],
'site_uri_price' => $val2['price'],
'site_level' => $level,
'site_pages_in_google' => $val1['nof_pages_in_google'],
'site_ip' => $ip,
'site_added' => date('Y-m-d H:i:s')
]);
}
////////////////////////////////////////////////////////////////////////////////////
// have we already checked the link stats?
/* $already = DB::getInstance()->select("SELECT * FROM `metrics` WHERE `metrics_url`='".trim($val1['url'] . $val2['uri'])."'");
if (!count($already)) {
// vars
$fURL = returnDomDetails(trim($val1['url'] . $val2['uri']));
// domdetailer
$domMajestic = json_decode($fURL);
if ($domMajestic) {
// avoid "PHP Notice: Trying to get property of non-object" warnings
DB::getInstance()->insert(
'metrics',
[
'metrics_user_id' => 1,
'metrics_url' => $metricsURL,
'metrics_moz_links' => $domMajestic->mozLinks,
'metrics_mozPA' => $domMajestic->mozPA,
'metrics_mozDA' => $domMajestic->mozDA,
'metrics_mozRank' => $domMajestic->mozRank,
'metrics_mozTrust' => $domMajestic->mozTrust,
'metrics_FB_comments' => $domMajestic->FB_comments,
'metrics_FB_shares' => $domMajestic->FB_shares,
'metrics_google_plus_one' => $domMajestic->google_plus_one,
'metrics_pinterest_pins' => $domMajestic->pinterest_pins,
'metrics_linkedin' => $domMajestic->linkedin,
'metrics_majesticLinks' => $domMajestic->majesticLinks,
'metrics_majesticRefDomains' => $domMajestic->majesticRefDomains,
'metrics_majesticCF' => $domMajestic->majesticCF,
'metrics_majesticTF' => $domMajestic->majesticTF,
'metrics_date' => date('Y-m-d H:i:s')
]);
}
} */
////////////////////////////////////////////////////////////////////////////////////
if ($member == SAPE_USER) {
echo "<br />";
echo "<b>------------->Page ID:</b> " . $val2['id'];
echo "<br />";
echo "<b>------------->Page:</b> " . $val2['uri'];
echo "<br />";
echo "<br />";
}
}
if ($member == SAPE_USER) {
echo "</pre>";
}
}
}
// show var_dump
if ($member == SAPE_USER) { showPre($userSearch); }
// message
stdmsg('Search complete, view the results here.');
} catch (SapeApiException $e) {
if ($e->getMessage() == "SAPE.RU: Search result is too big :o(") {
stderr('Your search query has too many results, try refining your search options to be more specific.');
} else {
stderr($e->getMessage());
}
}
}
// get the logged in users id
$row = get_logged_in_users_details($member);
// the ID
$hID = $row['member_id'];
?>
<div class="panel panel-primary">
<div class="panel-heading">Search - To get a Link ID you need to create a project.</div>
<div class="panel-body">
<form id="search-form" action="search.php" method="post" class="form-horizontal container-fluid" role="form">
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="txtLinkID" class="control-label">Link ID:</label>
</div>
<div class="col-sm-6">
<select id="txtLinkID" name="url_id" class="form-control" required="required">
<?php
$rows = DB::getInstance()->select('SELECT * FROM `projects` WHERE `project_member_id`='.$hID);
?>
<?php foreach ($rows as $row) { ?>
<option value="<?php echo htmlspecialchars($row['project_sape_link_id']) ?>"><?php echo htmlspecialchars($row['project_sape_link_id']) ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="txtLevelDepth" class="control-label">Domain Level:</label>
</div>
<div class="col-sm-6">
<select id="txtLevelDepth" name="domain_level" class="form-control" required="required">
<?php
$level = array(0 => "1",1 => "2",2 => "3");
?>
<?php foreach ($level as $key => $value) { ?>
<option value="<?php echo htmlspecialchars($key) ?>"><?php echo htmlspecialchars($value) ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="txtLevelDepth" class="control-label">Nesting Pages:</label>
</div>
<div class="col-sm-6">
<select id="txtLevelDepth" name="page_level" class="form-control" required="required">
<?php
$nesting = array(0 => "page_level_1",1 => "page_level_2",2 => "page_level_3");
?>
<?php foreach ($nesting as $key => $value) { ?>
<option value="<?php echo htmlspecialchars($key) ?>"><?php echo htmlspecialchars($value) ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="" class="control-label">Categories:</label>
</div>
<div class="col-sm-6">
<?php
$rows = DB::getInstance()->select('SELECT * FROM `categories`');
?>
<?php foreach ($rows as $row) { ?>
<input type="checkbox" name="categories[]" value="<?php echo htmlspecialchars($row['cat_sape_id']) ?>"> - <?php echo htmlspecialchars($row['cat_name']) ?><br>
<?php } ?>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="" class="control-label">Zones:</label>
</div>
<div class="col-sm-6">
<?php
$rows = DB::getInstance()->select('SELECT * FROM `zones`');
?>
<?php foreach ($rows as $row) { ?>
<input type="checkbox" name="zones[]" value="<?php echo htmlspecialchars($row['zone_sape_id']) ?>"> - <?php echo htmlspecialchars($row['zone_name']) ?><br>
<?php } ?>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="" class="control-label">Indexed in Google:</label>
</div>
<div class="col-sm-6">
<select id="" name="indexed_in_google" class="form-control" required="required">
<?php
$depth = array(0 => "Yes",1 => "No",2 => "Does not matter");
?>
<?php foreach ($depth as $key => $value) { ?>
<option value="<?php echo htmlspecialchars($key) ?>"><?php echo htmlspecialchars($value) ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="" class="control-label">Site in DMOZ:</label>
</div>
<div class="col-sm-6">
<select id="" name="site_in_dmoz" class="form-control" required="required">
<?php
$dmoz = array(1 => "No", 0 => "Yes");
?>
<?php foreach ($dmoz as $key => $value) { ?>
<option value="<?php echo htmlspecialchars($key) ?>"><?php echo htmlspecialchars($value) ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="txtCR" class="control-label">Site Must Be older Than (in days):</label>
</div>
<div class="col-sm-6">
<input type="text" id="" name="search_days" value="0" class="form-control" size="40" required="required"/>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="txtCR" class="control-label">Site Must Contain This Keyword:</label>
</div>
<div class="col-sm-6">
<input type="text" id="" name="keyword" value="" class="form-control" size="40" />
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="txtCR" class="control-label">Keyword Should Be:</label>
</div>
<div class="col-sm-6">
<select id="" name="keyword_placement" class="form-control" required="required">
<?php
$depth = array(0 => "On the page somewhere",1 => "In the <title>Keyword(s)</title> tags");
?>
<?php foreach ($depth as $key => $value) { ?>
<option value="<?php echo htmlspecialchars($key) ?>"><?php echo htmlspecialchars($value) ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="txtCR" class="control-label">No More Than This Number of External Links On-Page:</label>
</div>
<div class="col-sm-6">
<input type="text" id="" name="external_links" value="0" class="form-control" size="40" required="required"/>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="txtCR" class="control-label">Price (From):</label>
</div>
<div class="col-sm-6">
<input type="text" id="" name="price_from" value="0" class="form-control" size="40" required="required"/>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="txtCR" class="control-label">Price (To):</label>
</div>
<div class="col-sm-6">
<input type="text" id="" name="price_to" value="500" class="form-control" size="40" required="required"/>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="txtCR" class="control-label">Majestic CF (Min):</label>
</div>
<div class="col-sm-6">
<input type="text" id="" name="mj_cf_1" value="0" class="form-control" size="40" required="required"/>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label class="control-label">Majestic CF (Max):</label>
</div>
<div class="col-sm-6">
<input type="text" id="" name="mj_cf_2" value="0" class="form-control" size="40" required="required"/>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label class="control-label">Majestic TF (Min):</label>
</div>
<div class="col-sm-6">
<input type="text" id="" name="mj_tf_1" value="0" class="form-control" size="40" required="required"/>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label class="control-label">Majestic TF (Max):</label>
</div>
<div class="col-sm-6">
<input type="text" id="" name="mj_tf_2" value="0" class="form-control" size="40" required="required"/>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label for="" class="control-label">Order Results By:</label>
</div>
<div class="col-sm-6">
<select id="" name="order" class="form-control" required="required">
<?php
$depth = array(9 => "Lowest price first",19 => "Highest price first",4 => "Category Descending",14 => "Category Ascending");
?>
<?php foreach ($depth as $key => $value) { ?>
<option value="<?php echo htmlspecialchars($key) ?>"><?php echo htmlspecialchars($value) ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label class="control-label"># Of Pages Of Results:</label>
</div>
<div class="col-sm-6">
<select id="" name="page_number" class="form-control" required="required">
<?php for ($x = 1; $x <= 5; $x++) { ?>
<option value="<?php echo $x ?>"><?php echo $x ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="row form-group">
<div class="col-sm-6 text-right">
<label class="control-label" style="display: inline-block;"> </label>
</div>
<div class="col-sm-6 text-right">
<button type="submit" name="search" class="btn btn-default">Search</button>
<input type="hidden" name="hiddenID" value="<?php echo $hID; ?>" />
</div>
</div>
</form>
</div>
<div class="panel-footer">Search for links using <b>your</b> criteria.</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#search-form').submit(function(){
$('.modal-loading').fadeIn();
var url = $(this).attr('action');
$.post(url, $(this).serialize(), function(html){
$('.modal-loading').fadeOut();
var newDoc = document.open("text/html", "replace");
newDoc.write(html);
newDoc.close();
});
return false;
});
});
</script>
<?php
include('includes/footer.php');
Once the "search" is pressed, i wanted a loading .gif to appear to show the user it's working (this shows fine) the problem is, the form doesn't seem to be submitting, the if (isset($_POST['search'])) { is not being triggered for some reason, can anyone see anything i have missed?
This is the major problem with W3Schools and people following it.
if (isset($_POST['search'])) {
Will never be sent! Or triggered. You need to check the Network Tab for what all is going to the POST request.
You need to check with:
if (count($_POST) > 0) {
Or something that you are sending.
I have simple code thats not working. I made validation for checkbox and I get error:
Unable to access an error message corresponding to your field name accept_terms_checkbox.(accept_terms)
This is my code:
Controller:
public function formularz2()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$data['szkolenia'] = $this->Szkolenie_m->pobierz();
//VALIDATION!!
$this->form_validation->set_rules('imie', 'Imię', 'required');
$this->form_validation->set_message('required', 'Błąd: wypełnij powyższe pole');
$this->form_validation->set_rules('accept_terms_checkbox', 'checkbox', 'callback_accept_terms');
//CALLBACK FUNCTION!!
function accept_terms()
{
if ($this->input->post('accept_terms_checkbox'))
{
return TRUE;
}
else
{
$error = 'Please read and accept our terms and conditions.';
$this->form_validation->set_message('accept_terms', $error);
return FALSE;
}
}
//Po przesłaniu danych
if (!empty($_POST))
{
$konsultant = $this->uri->segment(3);
$dane = array(
'email1' => $this->input->post('email'),
'imie' => $this->input->post('imie'),
'nazwisko' => $this->input->post('nazwisko'),
'nazwa_firmy' => $this->input->post('firma'),
'konsultant_id' => $konsultant,
);
//Trzeba jeszcze tutaj ogarnąć wrzucanie pivotów zaznaczonych checkboxów
$boxes = $_POST['formChecks'];
$N = count($boxes);
$ostatni_id = $this->Osoby_m->ostatni_id();
for($i=0; $i < $N; $i++)
{
$this->Osoby_m->nowy_wpis_formularz($boxes[$i]);
}
//Pobrać id dodanej osoby = policzyć ile osób w bazie +1
$this->Osoby_m->nowa_osoba($dane);
}
if ($this->form_validation->run() == FALSE)
{
$this->load->view('formularz', $data);
}
else
{
$this->load->view('wyslano_formularz');
}
}
My View:
<?php echo form_open(); ?>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email:</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" value="<?php echo set_value('email'); ?>">
<p class="help-block"><?php echo form_error('email'); ?></p>
</div>
</div>
<div class="form-group">
<label for="imie" class="col-sm-2 control-label">Imię:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="imie" name="imie" placeholder="Imię" value="<?php echo set_value('imie'); ?>">
<p class="help-block"><?php echo form_error('imie'); ?></p>
</div>
</div>
<div class="form-group">
<label for="nazwisko" class="col-sm-2 control-label">Nazwisko: </label>
<div class="col-sm-10">
<input type="text" class="form-control" id="nazwisko" name="nazwisko" placeholder="Nazwisko" value="<?php echo set_value('nazwisko'); ?>">
<p class="help-block"><?php echo form_error('nazwisko'); ?></p>
</div>
</div>
<div class="form-group">
<label for="firma" class="col-sm-2 control-label">Firma:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="firma" name="firma" placeholder="Firma" value="<?php echo set_value('firma'); ?>">
<p class="help-block"><?php echo form_error('firma'); ?></p>
</div>
</div>
<div class="col-sm-12" style="margin-bottom: 30px; margin-top: 30px;"> <h3>Wybierz interesujące Cię szkolenia</h3></div>
<div class="form-group">
<label class="col-sm-2 control-label">Szkolenia:</label>
<div class="col-sm-10">
<?php foreach ($szkolenia as $szkolenie): ?>
<div class="checkbox">
<label><input id="szkolenie<?php echo $szkolenie->id; ?>" type="checkbox" name="formChecks[]" value="<?php echo $szkolenie->id; ?>"> <?php echo $szkolenie->nazwa_szkolenia; ?></label>
<br>
</div>
<?php endforeach; ?>
</div>
</div>
//CALLBACK CHECKBOX
<div class="form-group">
<div class="col-sm-12" style="margin-top: 50px;">
<input type="checkbox" name="accept_terms_checkbox" value="1"/> Zgadzam się na otrzymywanie maili od firmy Gamma<br>
<p><?php echo form_error('accept_terms_checkbox') ?></p>
</div>
</div>
<button type="submit" class="btn btn-lg btn-success" style="margin-top: 50px;">Odbierz Voucher</button>
<?php echo form_close(); ?>
In order to use your own callback method with codeigniter's validation class, you must declare the method. IN your case ( add this to your controller or a helper etc...):
public function accept_terms($user_input){
if($user_input != ""){
return True;
}else{
$this->form_validation->set_message('accept_terms_checkbo', 'The %s field can not be empty"');
return False;
}
I fixed this error by simple validation without callback function. I figure out that I don't need callback function for Terms of Services checkbox.
$this->form_validation->set_rules('accept_terms_checkbox', 'Checkbox', 'required');
$this->form_validation->set_message('required', 'You need to accept Terms of Services');
I am unable to identify the mistake
model (here I am matching the id to update)
function get_account_record($a_id)
{
$this->db->where('a_id', $a_id);
$this->db->from('account_info');
$query = $this->db->get();
return $query->result();
}
controller (receiving everything via post accept the ID i-e a_id )
function update($a_id)
{
$data['a_id'] = $a_id;
$data['view'] = $this->sf_model->get_account_record($a_id);
$this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');
form Validation
if ($this->form_validation->run() == FALSE)
{
$this->load->view('viewUpdate', $data);
}
else {
$data = array(
'a_name' => $this->input->post('a_name'),
'a_website' => $this->input->post('a_web'),
'a_billingStreet' => $this->input->post('a_billingStreet'),
'a_mobile' => $this->input->post('a_mobile'),);
$this->db->where('a_id', $a_id);
$this->db->update('account_info', $data);
display success message
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Successfully Updated!</div>');
//redirect('salesforce' . $a_id);
}
}
view (ID and Name)
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_id" class="control-label">Account ID</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_id" name="a_id" placeholder="" disabled="disabled" type="text" class="form-control" value="<?php echo $a_id;?>" />
<span class="text-danger"><?php echo form_error('a_id'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_name" class="control-label">Account Name</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_name" name="a_name" placeholder="Enter Account Name" type="text" class="form-control" value="<?php echo $view[0]->a_name; ?>" />
<span class="text-danger"><?php echo form_error('a_name'); ?></span>
</div>
</div>
</div>
If you don't want to allow the user to edit then add readonly instead of disabled attribute
<div class="col-lg-8 col-sm-8">
<input id="a_id" name="a_id" placeholder="" readonly="readonly" type="text" class="form-control" value="<?php echo $a_id;?>" />
<span class="text-danger"><?php echo form_error('a_id'); ?></span>
</div>
If you want to disable then add the hidden element
<input id="a1_id" name="a1_id" type="hidden" value="<?php echo $a_id;?>" />
Controller Function
function update($a_id)
{
//read the value using input library
$a_id = $this->input->post('a_id');
//$a1_id = $this->input->post('a1_id'); your hidden element value can be get here.
$data['a_id'] = $a_id;
$data['view'] = $this->sf_model->get_account_record($a_id);
$this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');
}
My problem Solved after editing the where clause in update query
$this->db->where('a_id', $_POST['a_id']);
I want to make subsector dependent on sector, i am not using jquery nor ajax
This is my Controller
<?php
/*
*/
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Salesforce extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->database();
$this->load->library('form_validation');
//load the employee model
$this->load->model('sf_model');
}
//index function
function index()
{
//...
$data['sector'] = $this->sf_model->get_sector();
$data['subsector'] = $this->sf_model->get_subsector();
$data['type'] = $this->sf_model->get_type();
//set validation rules
$this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');
$this->form_validation->set_rules('a_type', 'Type', 'callback_combo_check');
$this->form_validation->set_rules('a_sector', 'Sector', 'callback_combo_check');
$this->form_validation->set_rules('a_subsector', 'Sub Sector', 'callback_combo_check');
if ($this->form_validation->run() == FALSE)
{
//fail validation
//echo 'Validation failed';
//print_r($_REQUEST);
$this->load->view('sf_view', $data);
//
}
else
{
//pass validation
$data = array(
'a_name' => $this->input->post('a_name'),
'a_website' => $this->input->post('a_web'),
't_id' => $this->input->post('a_type'),
's_id' => $this->input->post('a_sector'),
'ss_id' => $this->input->post('a_subsector'),
'a_billingStreet' => $this->input->post('a_billingStreet'),
'a_billingState' => $this->input->post('a_billingState'),
'a_billingZip' => $this->input->post('a_billingZip'),
'a_billingCountry' => $this->input->post('a_billingCountry'),
'a_billingCity' => $this->input->post('a_billingCity'),
'a_phone' => $this->input->post('a_phone'),
'a_mobile' => $this->input->post('a_mobile'),
'a_fax' => $this->input->post('a_fax'),
);
//print_r($data);
//exit;
//insert the form data into database
$this->db->insert('account_info', $data);
//display success message
$this->session->set_flashdata('msg', '<div class="alert alert-success text-center">New Account Created</div>');
redirect('salesforce/index');
}
}
//custom validation function for dropdown input
function combo_check($str)
{
if ($str == '-SELECT-')
{
$this->form_validation->set_message('combo_check', 'Valid %s Name is required');
return FALSE;
}
else
{
return TRUE;
}
}
//custom validation function to accept only alpha and space input
function alpha_only_space($str)
{
if (!preg_match("/^([-a-z ])+$/i", $str))
{
$this->form_validation->set_message('alpha_only_space', 'The %s field must contain only alphabets or spaces');
return FALSE;
}
else
{
return TRUE;
}
}
}
This is my Model
<?php
class Sf_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function get_sector()
{
$results = $this->db->select('s_id, s_name')->from('account_sector')->get()->result();
$s_id = array('-SELECT-');
$s_name = array('-SELECT-');
for ($i = 0; $i < count($results); $i++)
{
array_push($s_id, $results[$i]->s_id);
array_push($s_name, $results[$i]->s_name);
}
return $sector_result = array_combine($s_id, $s_name);
}
function get_subsector()
{
$results = $this->db->select('ss_id, ss_name')->from('account_subsector')->get()->result();
$ss_id = array('-SELECT-');
$ss_name = array('-SELECT-');
for ($i = 0; $i < count($results); $i++)
{
array_push($ss_id, $results[$i]->ss_id);
array_push($ss_name, $results[$i]->ss_name);
}
return $subsector_result = array_combine($ss_id, $ss_name);
}
function get_type()
{
$results = $this->db->select('t_id, t_name')->from('account_type')->get()->result();
$t_id = array('-SELECT-');
$t_name = array('-SELECT-');
for ($i = 0; $i < count($results); $i++)
{
array_push($t_id, $results[$i]->t_id);
array_push($t_name, $results[$i]->t_name);
}
return $type_result = array_combine($t_id, $t_name);
}
}
>
This is my View
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SalesForce | Edit / New Account Details</title>
<!--link the bootstrap css file-->
<link href="<?php echo base_url("bt/css/bootstrap.css"); ?>" rel="stylesheet" type="text/css" />
<!-- link jquery ui css-->
<link href="<?php echo base_url('bt/css/jquery-ui.min.css'); ?>" rel="stylesheet" type="text/css" />
<!--include jquery library-->
<script src="<?php echo base_url('bt/js/jquery-1.11.3.js'); ?>"></script>
<!--load jquery ui js file-->
<script src="<?php echo base_url('bt/js/jquery-ui.min.js'); ?>"></script>
<style type="text/css">
.colbox {
margin-left: 0px;
margin-right: 0px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-offset-3 col-lg-6 col-sm-6 well">
<legend>Account Information</legend>
<?php
$attributes = array("class" => "form-horizontal", "id" => "accounts", "name" => "accounts");
echo form_open("salesforce", $attributes);?>
<fieldset>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_name" class="control-label">Account Name</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_name" name="a_name" placeholder="Enter Account Name" type="text" class="form-control" value="<?php echo set_value('a_name'); ?>" />
<span class="text-danger"><?php echo form_error('a_name'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_web" class="control-label">Website</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_web" name="a_web" placeholder="Enter Website" type="text" class="form-control" value="<?php echo set_value('a_web'); ?>" />
<span class="text-danger"><?php echo form_error('a_web'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_type" class="control-label">Type</label>
</div>
<div class="col-lg-8 col-sm-8">
<?php
$attributes = 'class = "form-control" id = "a_type"';
echo form_dropdown('a_type',$type,set_value('a_type'),$attributes);?>
<span class="text-danger"><?php echo form_error('a_type'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_sector" class="control-label">Sector</label>
</div>
<div class="col-lg-8 col-sm-8">
<?php
$attributes = 'class = "form-control" id = "a_sector"';
echo form_dropdown('a_sector',$sector,set_value('a_sector'),$attributes);?>
<span class="text-danger"><?php echo form_error('a_sector'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_subsector" class="control-label">Sub Sector</label>
</div>
<div class="col-lg-8 col-sm-8">
<?php
$attributes = 'class = "form-control" id = "a_subsector"';
echo form_dropdown('a_subsector',$subsector, set_value('a_subsector'), $attributes);?>
<span class="text-danger"><?php echo form_error('a_subsector'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_billingStreet" class="control-label">Billing Street</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_billingStreet" name="a_billingStreet" placeholder="Enter Billing Street" type="text" class="form-control" value="<?php echo set_value('a_billingStreet'); ?>" />
<span class="text-danger"><?php echo form_error('a_billingStreet'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_billingState" class="control-label">B-State/Province</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_billingState" name="a_billingState" placeholder="Enter State/Province" type="text" class="form-control" value="<?php echo set_value('a_billingState'); ?>" />
<span class="text-danger"><?php echo form_error('a_billingState'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_billingZip" class="control-label">B-Zip/Postal</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_billingZip" name="a_billingZip" placeholder="Enter Zip/Postal" type="text" class="form-control" value="<?php echo set_value('a_billingZip'); ?>" />
<span class="text-danger"><?php echo form_error('a_billingZip'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_billingCountry" class="control-label">Billing Country</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_billingCountry" name="a_billingCountry" placeholder="Enter Billing Country" type="text" class="form-control" value="<?php echo set_value('a_billingCountry'); ?>" />
<span class="text-danger"><?php echo form_error('a_billingCountry'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_billingCity" class="control-label">Billing City</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_billingCity" name="a_billingCity" placeholder="Enter Billing City" type="text" class="form-control" value="<?php echo set_value('a_billingCity'); ?>" />
<span class="text-danger"><?php echo form_error('a_billingCity'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_phone" class="control-label">Phone</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_phone" name="a_phone" placeholder="Enter Phone Number" type="text" class="form-control" value="<?php echo set_value('a_phone'); ?>" />
<span class="text-danger"><?php echo form_error('a_phone'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_mobile" class="control-label">Mobile Number</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_mobile" name="a_mobile" placeholder="Enter Mobile Number" type="text" class="form-control" value="<?php echo set_value('a_mobile'); ?>" />
<span class="text-danger"><?php echo form_error('a_mobile'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_fax" class="control-label">Fax Number</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_fax" name="a_fax" placeholder="Enter Fax Number" type="text" class="form-control" value="<?php echo set_value('a_fax'); ?>" />
<span class="text-danger"><?php echo form_error('a_fax'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-lg-8 col-sm-8 text-left">
<input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Insert" />
<input id="btn_cancel" name="btn_cancel" type="reset" class="btn btn-danger" value="Cancel" />
</div>
</div>
</fieldset>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
</div>
</body>
</html>
Controller
$data['sector'] = $this->sf_model->get_sector();
if(isset($data['sector'])&&$data['sector']!="")
$data['subsector'] = $this->sf_model->get_subsector($data['sector']);
Model, In model pass sector as parameter to get_subsector function and then during database select please pass it to where or as per your requirement you may procced.
Also Please avoid array_push() function in loops, use $ss_id[] = $results[$i]->ss_id; because it is much faster.
function get_subsector($sector)
{
$results = $this->db->select('ss_id, ss_name')->from('account_subsector')->get()->where($sector)->result();
$ss_id = array('-SELECT-');
$ss_name = array('-SELECT-');
for ($i = 0; $i < count($results); $i++)
{
array_push($ss_id, $results[$i]->ss_id);
array_push($ss_name, $results[$i]->ss_name);
}
return $subsector_result = array_combine($ss_id, $ss_name);
}
using some js and ajax
<script>
$(document).ready(function(){
$('#a_subsector').attr('disabled','disabled');
$('#a_sector').change(function(){
$.ajax({
url:'/salesforce/fill_ss',
type:'post',
data: {sector:$('#a_sector').val()},
success:function(result){
$('#a_subsector').removeAttr('disabled');
//$('#a_subsector').(result);
document.getElementById('a_subsector').innerHTML=result;
}
});
});
});
</script>
Adding a Function to controller
function fill_ss()
{
$this->sf_model->get_subsector();
$sector = $_POST['sector'];
$data = $this->sf_model->matching($sector);
//print_r($data);
for ($i = 0; $i < count($data); $i++)
{
echo '<option value="'.$data[$i]->ss_id.'">'.$data[$i]->ss_name.'</option>';
//echo '<option value="'.$data[$i]->ss_id.'"></option>';
//array_push($t_id, $data[$i]->t_id);
//array_push($t_name, $data[$i]->t_name);
}
}
Model
function view_accounts()
{
$results = $this->db->select('a_name,t_id,a_website,a_billingStreet,a_billingZip,a_billingCountry,a_billingCity,a_phone,a_mobile')->from('account_info')->get()->result();
return $results;
}
function matching($match)
{
$results =$this->db->select('ss_id,ss_name')->from('account_subsector')->where('s_id',$match)->get()->result();
return $results;
}
I have a strange issue with one form in Codeigniter. I am also using DataMapper PHP. So I have one form with two select statements with a couple of options. If one is selected the other one becomes 0 if the other is selected then the first one becomes 0. Any ideas why the strange behavior ?
Controller
public function edit($id)
{
// Check if the user is logged in
if (!$this->ion_auth->logged_in())
{
redirect('/');
}
elseif ($this->ion_auth->is_admin())
{
// Create Object
$predictions = new Prediction_model();
$predictions->where('id', $id)->get();
$categories = new Categories_model();
$bookmakers = new Bookmakers_model();
$categories->order_by('id', 'asc')->get();
$bookmakers->order_by('id', 'asc')->get();
// Default Object Options
$defaultCategory = new Categories_model();
$defaultCategory->where('id', $predictions->category_id)->get();
$defaultBookmaker = new Bookmakers_model();
$defaultBookmaker->where('id', $predictions->bookmaker_id)->get();
$recent_categories = array();
foreach ($categories as $category)
{
$single_category = array
(
'category_id' => $category->id,
'sport' => $category->sport,
);
array_push($recent_categories, $single_category);
}
$recent_bookmakers = array();
foreach ($bookmakers as $bookmaker)
{
$single_bookmaker = array
(
'bookmaker_id' => $bookmaker->id,
'bookmaker' => $bookmaker->bookmaker,
);
array_push($recent_bookmakers, $single_bookmaker);
}
// Validation rules
$rules = $this->prediction_model->rules;
$this->form_validation->set_rules($rules);
$data = array
(
'prediction_id' => $predictions->id,
'defaultCategory' => $defaultCategory->sport,
'defaultBookmaker' => $defaultBookmaker->bookmaker,
'eventDate' => $predictions->eventDate,
'event' => $predictions->event,
'tip' => $predictions->tip,
'bestOdd' => $predictions->bestOdd,
'bookmakers' => $recent_bookmakers,
'categories' => $recent_categories,
'admin_content' => 'admin/predictions/edit',
);
if ($this->input->post('submit'))
{
$predictions->eventDate = $this->input->post('eventDate');
$predictions->event = $this->input->post('event');
$predictions->tip = $this->input->post('tip');
$predictions->bestOdd = $this->input->post('bestOdd');
$predictions->bookmaker_id = $this->input->post('bookmaker');
$predictions->category_id = $this->input->post('icon');
}
if ($this->form_validation->run() == FALSE)
{
$this->parser->parse('admin/template_admin', $data);
}
else
{
$predictions->save();
redirect('admin/predictions/');
}
}
else
{
// NO ACCESS
}
} // function edit
and here is the VIEW:
<?php echo form_open('admin/predictions/edit/{prediction_id}', 'class="form-horizontal"'); ?>
<div class="form-group">
<label for="category" class="col-sm-2 control-label">Category</label>
<div class="col-sm-10">
<select name="icon" class="form-control">
<option selected='selected'>{defaultCategory}</option>
<option>--------</option>
{categories}
<option value="{category_id}">{sport}</option>
{/categories}
</select>
</div>
</div>
<div class="form-group">
<label for="event" class="col-sm-2 control-label">Event</label>
<div class="col-sm-10">
<input type="text" name="event" class="form-control" value="{event}">
</div>
</div>
<div class="form-group">
<label for="tip" class="col-sm-2 control-label">Tip</label>
<div class="col-sm-10">
<input type="text" name="tip" class="form-control" value="{tip}">
</div>
</div>
<div class="form-group">
<label for="bookmaker" class="col-sm-2 control-label">Bookmaker</label>
<div class="col-sm-5">
<select name="bookmaker" class="form-control">
<option selected='selected'>{defaultBookmaker}</option>
<option>--------</option>
{bookmakers}
<option value="{bookmaker_id}">{bookmaker}</option>
{/bookmakers}
</select>
</div>
<label for="bestOdd" class="col-sm-2 control-label">Odd</label>
<div class="col-sm-3">
<input type="text" name="bestOdd" class="form-control" value="{bestOdd}">
</div>
</div>
<div class="form-group">
<label for="date" class="col-sm-2 control-label">Date</label>
<div class="col-sm-4">
<div class="input-group">
<input size="16" name="eventDate" type="text" class="form-control eventDate" value="{eventDate}" readonly>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" name="submit" value="Save" class="btn btn-success btn-lg">
</div>
</div>
<?php echo form_close(); ?>