Dependend DropDown using Codeigniter, No Jquery nor Ajax - php

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

Related

file upload error messages are not displaying in codeigniter

I'm trying to insert data into database through a form. I'm uploading 2 image files, in database path of the files will be stored. I'm trying to do server side validation. when user does not upload a file or when user is uploading wrong file type, an error messages should display and page should return to same page with no data inserted into database.
This is my view:
<div class="main-panel">
<div class="content-wrapper">
<div class="row">
<div class="col-md-12 grid-margin stretch-card">
<div class="card">
<div class="card-body">
<h4 class="card-title">Add News & Event</h4>
<p class="card-description"> </p>
<?php echo validation_errors("<div class='alert alert-danger'>","</div>");?>
<?php if($failed = $this->session->flashdata('addNewsFailed')){
echo '<div class="alert alert-danger">' ;
echo $failed;
echo '</div>';
}?>
<form class="forms-sample" action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="exampleInputName1">Title</label>
<input type="text" class="form-control" id="exampleInputName1" value="<?php echo
set_value("title");?>" name="title">
</div>
<div class="form-group">
<label for="exampleInputName1">Sub Title</label>
<input type="text" class="form-control" id="exampleInputName1" value="<?php echo set_value("sub");?>" name="sub_title">
</div>
<div class="form-group">
<label for="exampleInputName1">Event Date</label>
<input type="text" class="form-control" id="exampleInputName1" name="date" value="<?php echo date('d - M - y ');//echo set_value("date");?>">
</div>
<div class="container">
<div class="row">
<div class="col-md">
<div class="form-group">
<label>Feature Image</label>
<div class="input-group col-xs-12">
<input type="file" class="form-control file-upload-info" placeholder="Upload Image" name="img" style="z-index: 9999; opacity: 0;" id="image">
<div id="imageName"></div>
<span class="input-group-append">
<button class="file-upload-browse btn btn-info" type="button" style="position: absolute; left: 0; padding: 6px 23px;border-radius:3px;">Upload</button> </span>
</div>
<?php if(isset($upload_error)){
echo $upload_error;
}?>
</div>
</div>
<div class="col-md">
<div class="form-group">
<label> Banner Image</label>
<div class="input-group col-xs-12">
<input type="file" class="form-control file-upload-info" placeholder="Upload Image" name="banner" id="banner" style="opacity: 0; z-index: 9999;">
<div id="bannerName"></div>
<span class="input-group-append">
<button class="file-upload-browse btn btn-info" type="button" style="position: absolute;left: 0;padding: 6px 23px;border-radius: 3px;">Upload</button></span>
</div>
<?php if(isset($upload_error1)){
echo $upload_error1;
}?>
</div>
</div>
</div>
</div>
<span class="input-group-append">-->
<div class="form-group">
<label for="exampleTextarea1">Text</label>
<textarea class="form-control" id="exampleTextarea1" rows="2" name="para" value=""><?php echo set_value("para");?></textarea>
</div>
<button type="submit" class="btn btn-success mr-2">Add</button>
<button class="btn btn-light">Cancel</button>
</form>
</div>
</div>
</div>
</div>
</div>
This is my controller:
public function addEvent()
{
//load library
$this->load->library('form_validation');
$this->load->helper('form');
//set rules for validation
$this->form_validation->set_rules("title", "Title", "required");
$this->form_validation->set_rules("date", "Event Date", "required");
$this->form_validation->set_rules("para", "Text", "required");
//template
$this->output->set_template('admin_layout');
//image upload
$config = [
'upload_path' => './uploads/',
'allowed_types' => 'gif|jpg|png',
];
$this->load->library('upload', $config);
//validation
if ($this->form_validation->run()) {
$post = $this->input->post();
if (!$this->upload->do_upload('img')) {
$upload_error = $this->upload->display_errors();
$this->load->view('dashboard/pages/forms/addEvent', compact('upload_error'));
} else {
$Feature1 = $this->upload->data();
$image_path = base_url('uploads/' . $Feature1['raw_name'] . $Feature1['file_ext']);
$post['image_path'] = $image_path;
}
if (!$this->upload->do_upload('banner')) {
$upload_error1 = $this->upload->display_errors();
$this->load->view('dashboard/pages/forms/addEvent', compact('upload_error1'));
} else {
$carsousel = $this->upload->data();
$banner = base_url('uploads/' . $carsousel['raw_name'] . $carsousel['file_ext']);
$post['banner'] = $banner;
}
$this->load->model('loginmodel', 'addNews');
$insert_id = $this->addNews->addNews($post);
if ($insert_id) {
$this->session->set_flashdata("addNewsSuccess", 'News & Event Added Successfully');
redirect('dashboard/update_news/' . $insert_id);
} else {
$this->session->set_flashdata("addNewsFailed", 'Failed');
$this->load->view('dashboard/pages/forms/addEvent');
}
} else {
$upload_error = $this->upload->display_errors();
$this->load->view('dashboard/pages/forms/addEvent', compact('upload_error'));
}
}
This is my model:
public function addNews($array)
{
$this->db->insert('news', $array);
return $this->db->insert_id();
}
My form validation are working perfectly but when user is uploading wrong file type or does not upload any file then file upload error message is not displaying and data is inserting into database the with both image field empty.

AJAX form not triggering POST

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.

How to create dependent drop down in Yii2 using Activeform?

i have form in which there are 2 dropdowns which are select batch from first dropdown and according to that batch, students will appear in second dropdown , Right now i am getting dropdown of batch but, not getting dropdown of students using batch
This is my view
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use common\models\TblBetch;
use common\models\TblCompany;
use common\models\TblCourse;
use common\models\TblStudent;
use common\models\TblStudentExpence;
use kartik\widgets\DepDrop;
use yii\db\Query;?><link rel="shortcut icon" href="<?php echo Yii::$app->params['global_theme_path'];?>images/logo1.png" type="image/png"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><script src="http://iamrohit.in/lab/js/location.js"></script><div class="pageheader"><h2><i class="fa fa-money"></i>Expense</h2>
<div class="breadcrumb-wrapper">
<span class="label">You are here:</span>
<ol class="breadcrumb">
<li>Westline Shipping</li>
<li class="active">Add Expense</li>
</ol></div></div><div class="contentpanel">
<?php
$session = Yii::$app->session;
if($session->hasFlash('success'))
{
echo $session->getFlash('success');
}
if($session->hasFlash('error'))
{
echo $session->getFlash('error');
}
$site = '';
?>
<div class="row">
<div class="col-md-12">
<div class="container-fluid-50">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-btns">
−
</div>
<h4 class="panel-title">Add Expense</h4>
</div>
<form class="panel-body" action="dashboard" method="post">
<div class="row">
<?php $form = ActiveForm::begin(); ?>
<div class="col-sm-9">
<div class="form-group">
<label class="control-label"></label>
<?php
//$batch = new TblBetch();
//$quer = new Query();
//$getid = [''];
//$getid = $quer->select('b_sn')->from($batch->tableName())->all();
//echo $form->field($model,'se_b_id')->dropDownList($getid,['id'=>'se_b_id']);
?>
<?=
$form->field($model,'se_s_id')->dropDownList(
ArrayHelper::map(TblBetch::find()->all(),'b_id','b_sn'),
[
'prompt'=>'Select Batch',
'name'=>'s_b_id',
'id'=>'s_b_id',
'onchange'=>'$.post("student?id="+$(this).val(),function(data){$("select#tblstudentexpence-se_s_id").html(data);});'
]);
?>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label"></label>
<?=
$form->field($model,'se_s_id')->dropDownList(
ArrayHelper::map(TblStudent::find()->all(),'s_id','s_fname'),
[
'prompt'=>'Select Student',
'name'=>'se_s_id',
'id'=>'se_s_id'
]);
?>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Payment Mode</label>
<div class="radio"><label><input type="radio" name="num" value="cash" required> Cash </label></div>
<div class="radio"><label><input type="radio" name="num" value="check" required> Check </label></div>
<div class="radio"><label><input type="radio" name="num" value="dd" required> DD </label></div>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Check / DD No</label>
<input type="text" name="num" id="num" class="form-control" placeholder="Enter your check or DD number" required/>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Amount</label>
<input type="text" name="lastname" class="form-control" placeholder="Enter your Amount" required/>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group"> <br><br>
<input type="submit" value="Submit" class="btn btn-primary">
<input type="reset" value="Cancel" class="btn btn-primary">
</div></div>
</div><!-- row -->
</form>
</div><!-- panel-body -->
</div>
</div>
</div> <div class="col-md-12">
</div>
</div></div></div> <script>
$(function() {
window.invalidate_input = function() {
if ($('input[name=num]:checked').val() == "check" || $('input[name=num]:checked').val() == "dd")
$('#num').removeAttr('disabled');
else
$('#num').attr('disabled', 'disabled');
};
$("input[name=num]").change(invalidate_input);
invalidate_input();
});
This is my controller (sitecontroller.php)
public function actionStudent($id)
{
//p($id);
$student = new TblStudent();
$queryobj = new Query();
$data = [];
$data = $queryobj->select('*')->from($student->tableName())->where(['s_betch'=>$id])->count();
//p($data);
$queryobj1 = new Query();
$getid = [];
$getid = $queryobj->select('*')->from($student->tableName())->where(['s_betch'=>$id])->all();
//p($getid);
if($data > 0)
{
foreach($getid as $gid)
{
echo "<option value='".$gid->s_id."'>".$gid->s_fname."</option>";
}
}
else
{
echo "<option> - </option>";
}
}
I am getting this response in firebug after ajax call
You can implement dependent drop-down much simpler in yii 2 because there is a widget available which you can use.
use kartik widget here -> kartik dependent drop down

Unable to access an error message - Callback validation in CodeIgniter

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

Unable to retrieve ID to update in database

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

Categories