I can't access image from file field in codeigniter view.In my project form helper is auto-loaded.When i check by form_validation it is found that image from file field is not reached at controller.
I got the message image not selected when i checked with form_validation and die function.
This is my view
<div id="content" class="clearfix">
<div class="contentwrapper"><!--Content wrapper-->
<div class="heading">
<h3>Manage images</h3>
</div><!-- End .heading-->
<!-- Build page from here: Usual with <div class="row-fluid"></div> -->
<div class="row-fluid">
<div class="span6" style="width:70%; margin-left:15%;">
<div class="box">
<div class="title">
<h4>
<span>Update Image</span>
</h4>
</div>
<?php echo form_open_multipart('productadmin/update_image');?>
<div class="form-row row-fluid">
<div class="span12">
<div class="row-fluid">
<label class="form-label span4" for="textarea">image<b style="color:#F00; font-size:11px;">*</b></label>
<input type="file" name="image">
<span class="error">
</span>
</div>
</div>
</div>
<div class="form-row row-fluid">
<div class="span12">
<div class="row-fluid">
<img src="<?php echo base_url();?>uploads/products/<?php echo $name;?>" width="50%" height="%">
</div>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-info" style="float:right;">Submit</button>
</div>
<?php echo form_close();?>
</div>
</div><!-- End .box -->
</div><!-- End .span6 --><!-- End .span6 --><!-- End .span6 -->
</div><!-- End .row-fluid --><!-- End .row-fluid --><!-- End .row-fluid --><!-- End .row-fluid -->
</div><!-- End contentwrapper -->
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js" type="text/javascript"></script>
<!-- End #content -->
<!-- End #wrapper -->
It is my controller
class Productadmin extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('products_model');
$this->load->helper('security');
$this->load->helper('url');
if($this->session->userdata('activedata')!=true)
{
$this->session->set_flashdata('message','Please do login');
redirect('admin');
}
error_reporting(0);
}
public function update_image()
{
$id=$this->input->post('ids');
$newname=$this->input->post('image');
$this->form_validation->set_rules('image','Image','required');
$this->form_validation->set_error_delimiters('','');
if($this->form_validation->run()==false)
{
echo "image not selected";
die();
$this->template->load('adminhome','edit_image','');
}
else
{
echo "image selected";
die();
$config=array(
'upload_path'=>'./uploads/products/',
'allowed_types'=>'jpg|png',
);
$this->load->library('upload',$config);
$this->upload->do_upload('images');
$data=$this->upload->data();
$newimage=$data['file_name'];
echo $newimage;
die();
$this->products_model->update_single_image($id,$newimage);
redirect('productadmin/viewimages/'.$id);
}
}
}
pls help me
Change this rule :
$this->form_validation->set_rules('image','Image','required');
to this :
if (empty($_FILES['image']['name']))
{
$this->form_validation->set_rules('image', 'Image', 'required');
}
so when the image is not empty, the validation rule for that image will be skipped.
Related
I am quite new to Lighting Web Component. However, I have some experience with other component base frameworks.
I'm creating a button for each item and when the button is clicked for each item. It applies to all items. In other words, when one clicks 'Extend' it is meant to show a date input for that specific item but it shows too all of them. This is how my code looks like:
<template>
<template if:true={subscriptionData}>
<template for:each={subscriptionData} for:item="subscriptionItem">
<div key={subscriptionItem.Id}>
<div>
<div class="slds-grid slds-gutters slds-grid_vertical-align-center">
<div class="slds-col custom-card-title-container">
<h2 class="custom-title">{subscriptionItem.SBQQ__ProductName__c}</h2>
</div>
<div class="slds-col button-custom">
<lightning-button label="Extend" title="Non-primary action" onclick={handleShowEditEndDate} class="slds-m-right_none custom-margin"></lightning-button>
</div>
</div>
<div class="slds-grid slds-m-top_xx-small">
<div class="slds-col">
<div class="slds-grid slds-grid_vertical">
<div class="slds-col slds-m-bottom_xx-small">
<span>Start Date</span>
</div>
<div class="slds-col">
<span>{subscriptionItem.SBQQ__StartDate__c}</span>
</div>
</div>
</div>
<div class="slds-col">
<div class="slds-grid slds-grid_vertical">
<div class="slds-col slds-m-bottom_xx-small">
<span>End Date</span>
</div>
<div class="slds-col">
<span>{subscriptionItem.SBQQ__EndDate__c}</span>
</div>
</div>
</div>
<div class="slds-col">
<div class="slds-grid slds-grid_vertical">
<div class="slds-col slds-m-bottom_xx-small">
<span>Quantity</span>
</div>
<div class="slds-col">
<span>{subscriptionItem.SBQQ__Quantity__c}</span>
</div>
</div>
</div>
</div>
<template if:true={showEditField}>
<lightning-layout multiple-rows class="slds-m-top_xx-small">
<lightning-layout-item size="8">
<lightning-input
type="date"
name="endDateInput"
label="End Date"
variant="label-inline"
>
</lightning-input>
</lightning-layout-item>
</lightning-layout>
</template>
<hr class = "custom-line">
</div>
</div>
</template>
</template>
export default class ExtendCPQSubscriptionLWC extends LightningElement {
#api recordId;
#track subscriptionData;
#track columns = columns;
showEditField = false
#wire(getSubscriptionsLWC, { recordId: '$recordId'})
loadFoundSubscriptions({error, data}) {
if (data) {
console.log('OK');
console.log(data);
this.subscriptionData = data;
} else if (error) {
console.log('ERR ' + JSON.stringify(error));
}
}
I would appreciate any input thanks in advance.
You have common showEditField used to control visibility/addition of date input. My recommendation is to move that information inside subscriptionData and update the track variable. This would trigger re-render of UI and only show that field for item, for which Extend button was clicked
I was following a youtube tutorial on making a codeigniter college management system
but i am stuck at a point.. when i try to submit the form on wamp server
select form fields not showing requied error on form submit
i have checked the name fields of the form inputs matching the database table but still when the form is submitted without any fiilling any fields
the role and gender select field errors doesnt show
here is my code
controller
welcome.php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see https://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
//$this->load->helper('url');
$this->load->view('home');
}
public function adminRegister()
{
$this->load->model('queries');
$roles = $this->queries->getRoles();
// print_r($roles);
// exit();
$this->load->view('register',['roles'=>$roles]);
}
public function adminLogin()
{
echo 'Login';
}
public function adminSignup()
{
//echo 'Registered succesfully';
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('gender','Gender','required');
$this->form_validation->set_rules('role_id','Role','required');
$this->form_validation->set_rules('password','Password','required');
$this->form_validation->set_rules('confpwd','Password Again','required');
$this->form_validation->set_error_delimiters('<div class="text-danger">','</div>');
if($this->form_validation->run()){
echo 'validation passed';
}else{
//echo 'validation error';
echo validation_errors();
}
}
}
and views
register.php
<?php include('inc/header.php');?>
<div class="container mt-2">
<?php echo form_open('welcome/adminSignup',['class'=>'form-hoizontal']);?>
<h3 class="text-center display-4">ADMIN REGISTER</h3>
<hr/>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div class="row">
<div class="col-md-3">
<label for="username" class="mt-2">User name:</label>
</div>
<div class="col-md-9">
<!-- <input type="text" class="form-control" placeholder="User name" id="username"> -->
<?php
$data = array(
'type' => 'text',
'name' => 'username',
'placeholder' => 'Enter Username',
'class' => 'form-control'
);
echo form_input($data); ?>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<?php echo form_error('username','<div class="text-danger">','</div>');?>
</div>
</div><!--row-->
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div class="row mt-3">
<div class="col-md-3">
<label for="email" class="mt-2">Email address:</label>
</div>
<div class="col-md-9">
<!-- <input type="email" class="form-control" placeholder="Enter email" id="email"> -->
<?php
$data = array(
'type' => 'email',
'name' => 'email',
'placeholder' => 'Enter Email',
'class' => 'form-control'
);
echo form_input($data); ?>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<?php echo form_error('email','<div class="text-danger">','</div>');?>
</div>
</div><!--row-->
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div class="row mt-3">
<div class="col-md-3">
<label for="gender" class="mt-2">Gender:</label>
</div>
<div class="col-md-9">
<!-- <input type="email" class="form-control" placeholder="Enter email" id="email"> -->
<select class="form-control" name="gender">
<option>Select</option>
<option>Male</option>
<option>Female</option>
</select>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<?php echo form_error('gender','<div class="text-danger">','</div>');?>
</div>
</div><!--row-->
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div class="row mt-3">
<div class="col-md-3">
<label for="email" class="mt-2">Role:</label>
</div>
<div class="col-md-9">
<select class="form-control" name="role_id">
<option>Select</option>
<?php if(count($roles)) { ?>
<?php foreach ($roles as $role){?>
<option><?php echo $role->rolename;?></option>
<?php }
} ?>
</select>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<?php echo form_error('role_id','<div class="text-danger">','</div>');?>
</div>
</div><!--row-->
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div class="row mt-3">
<div class="col-md-3">
<label for="password" class="mt-2">Password:</label>
</div>
<div class="col-md-9">
<!-- <input type="password" class="form-control" placeholder="Enter password" id="password"> -->
<?php
$data = array(
'type' => 'password',
'name' => 'password',
'placeholder' => 'Enter Password',
'class' => 'form-control'
);
echo form_input($data); ?>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<?php echo form_error('password','<div class="text-danger">','</div>');?>
</div>
</div><!--row-->
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div class="row mt-3">
<div class="col-md-3">
<label for="password" class="mt-2">Password Again:</label>
</div>
<div class="col-md-9">
<!-- <input type="password" class="form-control" placeholder="Enter password" id="password"> -->
<?php
$data = array(
'type' => 'password',
'name' => 'confpwd',
'placeholder' => 'Enter Password Again',
'class' => 'form-control'
);
echo form_input($data); ?>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<?php echo form_error('confpwd','<div class="text-danger">','</div>');?>
</div>
</div><!--row-->
<div class="row">
<div class="col-md-6">
<!-- <button type="submit" class="btn btn-dark float-right">Register</button> -->
<div class="float-right">
<?php echo form_submit('Register', 'Register',"class='btn btn-dark'"); ?>
<?php echo anchor('welcome','GO BACK',['class'=>'btn btn-warning']);?>
</div>
</div>
</div>
<?php echo form_close(); ?>
</div>
<?php include('inc/footer.php');?>
here is screenshot
The errors are not showing because your select boxes are always submitting a value. When you select an option in the selectbox and no value attribute is specified, the value after the <option> tag is being sent to the server.
To trigger the required rule, you need to send an empty string.
To do this, you can either use an empty <option> tag for the placeholder:
<select class="form-control" name="gender">
<option></option>
<option>Male</option>
<option>Female</option>
</select>
Or set the value attribute of the placeholder to an empty string:
<select class="form-control" name="gender">
<option value="">Select</option>
<option>Male</option>
<option>Female</option>
</select>
Try below Code:
<select class="form-control" name="gender">
<option></option>
<option value="male">Male</option>
<option valie="female">Female</option>
</select>
I have created a simple form page for people to submit change requests and am struggling to get the invalid validation working for the input field where type="file".
I have written a function to run the object through custom validation (similar to how I handle Email validation), but I never receive the red outline and error message like I do for email. I am logging my "Not valid" message successfully when I try to upload more than 5 files, but still receive a green outline and the valid message on the input.
I think I've tried so much at this point I'm just confusing myself. I was able to get both valid and invalid messages at one point, but nothing beyond that.
I am trying to avoid validating every input individually if possible.
Thanks for any help in advance!
<!DOCTYPE html>
<html lang="en" class="vh-100">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="" />
<meta name="author" content="AWright" />
<link rel="icon" href="##URL_FAV_ICON##" />
<title>Change Request Form</title>
<!-- Bootstrap 5 CSS -->
<link
href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous"
/>
<!-- /Bootstrap 5 CSS-->
<!-- Axios Library -->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<!-- /Axios Library -->
<style></style>
<script src="https://cdn.jsdelivr.net/npm/vue#3.0.11"></script>
</head>
<body>
<div id="awApp" class="d-flex flex-column vh-100">
<!-- FIXED NAVBAR AND/OR HEADER -->
<header>
<nav
class="navbar navbar-expand-md navbar-light fixed-top bg-light"
></nav>
</header>
<!-- / HEADER/NAVBAR -->
<!-- MAIN CONTENT -->
<main role="main" class="flex-shrink-0">
<div class="container">
<div class="row mt-5 pt-5">
<div class="col-md-8 offset-md-2">
<h2 class="mt-5 text-center" style="font-size: 3rem">
Program Change Request Form
</h2>
<p class="text-center">
Please complete the below form to submit your program change
request:
</p>
</div>
</div>
<div class="row">
<div class="col-md-6 offset-md-3">
<div class="card shadow p-3 my-5">
<div class="card-body">
<form
novalidate
class="needs-validation"
name="mainForm"
#submit="submitForm"
method="post"
ref="requestForm"
>
<div class="row mb-2">
<div class="col-md-6 mb-3">
<label for="firstname" class="form-label mb-0"
>First Name*</label
>
<input
id="firstname"
type="text"
name="firstname"
class="form-control"
v-model="contact.firstName"
required
/>
<div class="valid-feedback">Looks good!</div>
<div class="invalid-feedback">
Please enter a first name.
</div>
</div>
<div class="col-md-6 mb-3">
<label for="lastname" class="form-label mb-0"
>Last Name*</label
>
<input
id="lastname"
type="text"
name="lastname"
class="form-control"
v-model="contact.lastName"
required
/>
<div class="valid-feedback">Looks good!</div>
<div class="invalid-feedback">
Please enter a last name.
</div>
</div>
</div>
<div class="row mb-3">
<div class="col">
<label for="email" class="form-label mb-0"
>Email Address*</label
>
<input
id="email"
type="text"
name="email"
class="form-control"
v-model="contact.email"
pattern="\b[a-zA-Z0-9._%-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,100}\b"
required
/>
<div class="valid-feedback">Looks good!</div>
<div class="invalid-feedback">
Please enter a valid email.
</div>
</div>
</div>
<div class="row mb-3">
<div class="col">
<label for="name" class="form-label mb-0"
>Name of Request*</label
>
<input
id="name"
type="text"
name="RequestName"
class="form-control"
v-model="contact.name"
required
/>
<div class="valid-feedback">Looks good!</div>
<div class="invalid-feedback">
Please enter a name for the change request being made.
</div>
</div>
</div>
<div class="row mb-3">
<div class="col">
<label for="description" class="form-label mb-0"
>Description of Request*</label
>
<textarea
id="description"
type="text"
name="RequestDetails"
class="form-control"
v-model="contact.description"
required
></textarea>
<div class="valid-feedback">Looks good!</div>
<div class="invalid-feedback">
Please enter a description for the request being made.
</div>
</div>
</div>
<div class="row mb-3">
<div class="col">
<label for="due_date" class="form-label mb-0"
>Estimated Due Date*</label
><br />
<em
><small
>Please note that this subject to change.</small
></em
>
<input
id="due_date"
type="text"
name="CompletionDate"
class="form-control"
v-model="contact.dueDate"
v-maska="dateMask"
required
/>
<div class="valid-feedback">Looks good!</div>
<div class="invalid-feedback">
Please enter a desired due date for the change request
in MM/dd/yyyy format.
</div>
</div>
</div>
<div class="row mb-3">
<div class="col">
<label for="files" class="form-label mb-0"
>Please attach any assets (images, font files, etc)
here to upload*:</label
><br />
<small
><em
>*Maximum of 5 files can be uploaded at a time</em
></small
>
<input
type="file"
class="form-control"
id="files"
name="files"
#change="handleFilesUpload( $event )"
multiple
/>
<div class="valid-feedback">Looks good!</div>
<div class="invalid-feedback">
Please do not upload more than 5 files at a time.
</div>
</div>
</div>
<button
type="submit"
class="btn btn-primary px-4 py-2"
value="Submit"
>
Submit
</button>
<div>
<small class="form-text text-muted">
<em>* Denotes a required field.</em>
</small>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</main>
<!-- /MAIN CONTENT -->
</div>
<!--Bootstrap 5 JS-->
<script
src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"
></script>
<!--/Bootstrap 5 JS-->
<!-- Maska JS -->
<script src="https://cdn.jsdelivr.net/npm/maska#latest/dist/maska.js"></script>
<!-- /Maska JS -->
<!-- VUE APP CODE -->
<script>
const app = Vue.createApp({
data() {
return {
currentYear: new Date().getFullYear(),
now: new Date().toISOString(),
contact: {
firstName: "##firstname##",
lastName: "##lastname##",
email: "##email##",
name: "",
description: "",
dueDate: "",
},
files: "",
};
},
methods: {
submitForm(e) {
const isValid =
this.contact.firstName &&
this.contact.lastName &&
this.contact.email &&
this.contact.name &&
this.contact.description &&
this.contact.dueDate &&
this.validEmail(this.contact.email) &&
this.files.length <= 5 &&
this.multipleFilesValidation(this.files);
e.target.classList.add("was-validated");
if (!isValid) {
e.preventDefault();
console.log("Not valid");
} else {
e.preventDefault();
this.postToZapier();
console.log("Success");
}
},
multipleFilesValidation: function (files) {
if (this.files.length > 5) {
console.log(this.files);
return false;
} else {
return true;
}
},
handleFilesUpload(event) {
this.files = event.target.files;
},
postToZapier() {
console.log(this.files);
var formData = new FormData();
for (var i = 0; i < this.files.length; i++) {
let file = this.files[i];
formData.append("files[" + i + "]", file);
}
console.log(formData);
var url = "https://hooks.zapier.com/hooks/catch/159749/b9h6kww/";
var requestForm = this.$refs.requestForm;
console.log(requestForm);
axios
.post(url, formData, {
headers: {
"Content-Type": "multipart/form-data",
},
})
.then(function () {
console.log("Success!");
setTimeout(function () {
requestForm.submit();
}, 0);
})
.catch(function () {
console.log("Fail.");
});
},
validEmail: function (email) {
const re = /\b[a-zA-Z0-9._%-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,100}\b/;
return re.test(email);
},
},
computed: {
dateMask: function () {
return "##/##/####";
},
},
});
app.directive("maska", Maska.maska);
app.mount("#awApp");
</script>
<!-- /VUE APP CODE-->
</body>
</html>
Here is my html form.
<?php echo form_open(base_url('admin/subeler/add'), 'class="form-horizontal"'); ?>
<div class="row clearfix">
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-5 form-control-label">
<label for="grup">Grup Adı</label>
</div>
<div class="col-lg-8 col-md-10 col-sm-8 col-xs-7">
<div class="form-group">
<div class="form-line">
<input type="text" id="grup" required name="grup" value="<?php echo set_value('grup'); ?>" class="form-control">
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-5 form-control-label">
<label for="name">Şube Adı</label>
</div>
<div class="col-lg-8 col-md-10 col-sm-8 col-xs-7">
<div class="form-group">
<div class="form-line">
<input type="text" id="name" required name="name" value="<?php echo set_value('name'); ?>"class="form-control">
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-5 form-control-label">
<label for="ip">İp Adresi</label>
</div>
<div class="col-lg-8 col-md-10 col-sm-8 col-xs-7">
<div class="form-group">
<div class="form-line">
<input type="text" id="ip" required name="ip" value="<?php echo set_value("ip"); ?>"class="form-control">
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-5 form-control-label">
<label for="path">Database Adı</label>
</div>
<div class="col-lg-8 col-md-10 col-sm-8 col-xs-7">
<div class="form-group">
<div class="form-line">
<input type="text" id="path" required name="path" value="<?php echo set_value('path'); ?>"class="form-control">
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-5 form-control-label">
<label for="lisans">Lisans Tarihi</label>
</div>
<div class="col-lg-8 col-md-10 col-sm-8 col-xs-7">
<div class="form-group">
<div class="form-line">
<input type="text" id="lisans" required name="lisans" value="<?php echo set_value('lisans'); ?>"class="form-control">
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-5 form-control-label">
<label for="il">İl</label>
</div>
<div class="col-lg-8 col-md-10 col-sm-8 col-xs-7">
<div class="form-group">
<div class="form-line">
<input type="text" id="il" name="il" value="<?php echo set_value('il'); ?>"class="form-control">
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-5 form-control-label">
<label for="ilce">İlçe</label>
</div>
<div class="col-lg-8 col-md-10 col-sm-8 col-xs-7">
<div class="form-group">
<div class="form-line">
<input type="text" id="ilce" name="ilce" value="<?php echo set_value('ilce'); ?>"class="form-control">
</div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-lg-offset-2 col-md-offset-2 col-sm-offset-4 col-xs-offset-5">
<input type="submit" name="submit" value="EKLE" class="btn btn-success m-t-15 waves-effect">
</div>
</div>
<?php echo form_close();?>
When I enter an ip adres to the input field like (http://xxx.xxx.xx.xx:xx/) redirect me to the 403 Forbidden page but when I put " (like "http://xxx.xxx.xx.xx:xx") to the ip adres it work fine. I tried to disable xss-filter disable in config files but result is same. Can anyone give some advice. How to submit an ip adres correctly with Codeigniter?
Server-Side
public function add()
{
if ($this->input->post("submit")) {
$this->form_validation->set_rules('grup', 'Grup Adı', 'trim|required');
$this->form_validation->set_rules('name', 'Şube Adı', 'trim|required');
$this->form_validation->set_rules('ip', 'İp Adresi', 'trim|required');
$this->form_validation->set_rules('path', 'Database Yolu', 'trim|required');
$this->form_validation->set_rules('lisans', 'Lisans Tarihi', 'trim|required');
if ($this->form_validation->run() == true) {
$data = array(
'name' => $this->input->post('name'),
'marka' => $this->input->post('grup'),
'ip' => $this->input->post('ip'),
'il' => $this->input->post('il'),
'ilce' => $this->input->post('ilce'),
'databasePath' => $this->input->post('path'),
'lisanse_date'=>$this->input->post('lisans')
);
$data = $this->security->xss_clean($data);
$result = $this->sube_model->add_sube($data);
if ($result) {
$this->session->set_flashdata('msg', 'Şube başarılı bir şekilde eklendi!');
redirect(base_url('admin/subeler'));
}
} else {
$data['view'] = 'admin/subeler/sube_ekle';
$this->load->view('layout', $data);
}
} else {
$data['view'] = 'admin/subeler/sube_ekle';
$this->load->view('layout', $data);
}
}
Unfortunately I could not test the code exactly as provided as I don't make use of CodeIgniter 3 anymore, so I translated it into CodeIgniter 4.
Also, I could not test what $this->sube_model->add_sube does.
This, plus the form view you provided is working when setting http://xx.xx.xx.xx:xx on every form field.
You should trace when the 403 forbidden is triggered.
If you make use of XDebug i'd recommend you to trace step by step where the execution fails.
Otherwise I'd just start removing code to find out at which point of the execution the exception is occurring.
If you are making use of a shared hosting, there could be any rule in the http server that is blocking the request, if that's the case, run it locally using a WAMP/LAMP bundle.
// CodeIgniter 4 approach
public function add() {
if ($this->request->getMethod() == 'post') {
$validation = \Config\Services::validation();
$validation->setRule('grup', 'Grup Adı', 'trim|required');
$validation->setRule('name', 'Şube Adı', 'trim|required');
$validation->setRule('ip', 'İp Adresi', 'trim|required');
$validation->setRule('path', 'Database Yolu', 'trim|required');
$validation->setRule('lisans', 'Lisans Tarihi', 'trim|required');
if ($validation->withRequest($this->request)->run() == true) {
$data = array(
'name' => $this->request->getPost('name'),
'marka' => $this->request->getPost('grup'),
'ip' => $this->request->getPost('ip'),
'il' => $this->request->getPost('il'),
'ilce' => $this->request->getPost('ilce'),
'databasePath' => $this->request->getPost('path'),
'lisanse_date'=>$this->request->getPost('lisans')
);
// $data = $this->security->xss_clean($data);
$result = $this->sube_model->add_sube($data);
$result = 1;
if ($result) {
session()->setFlashdata('msg', 'Şube başarılı bir şekilde eklendi!');
return redirect(base_url('admin/subeler'));
}
} else {
$data['view'] = 'admin/subeler/sube_ekle';
return view('layout', $data);
}
} else {
$data['view'] = 'admin/subeler/sube_ekle';
return view('layout', $data);
}
}
i have a bootstrap modal dialog which use laravel form to register a user.
Here's the code:
<div id="addPenggunaModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="ModalLabel">Tambah Pengguna Baru</h3>
</div>
<div class="modal-body">
{{ Form::open(array('url'=>'users/addpengguna','class'=>'form-horizontal', 'method'=> 'POST')) }}
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
<div class="control-group">
<label for="firstname" class="control-label">First Name:</label>
<div class="controls">
{{ Form::text('firstname', null, array('class'=>'span3', 'placeholder'=>'First Name')) }}
</div>
</div> <!-- /field -->
<div class="control-group">
<label for="lastname" class="control-label">Last Name: </label>
<div class="controls">
{{ Form::text('lastname', null, array('class'=>'span3', 'placeholder'=>'Last Name')) }}
</div>
</div> <!-- /field -->
<div class="control-group">
<label for="email" class="control-label">Email Address: </label>
<div class="controls">
{{ Form::text('email', null, array('class'=>'span3', 'placeholder'=>'Email Address')) }}
</div>
</div> <!-- /field -->
<div class="control-group">
<label for="password" class="control-label">Password:</label>
<div class="controls">
{{ Form::password('password', array('class'=>'span3', 'placeholder'=>'Password')) }}
</div>
</div> <!-- /field -->
<div class="control-group">
<label for="confirm_password" class="control-label">Confirm Password:</label>
<div class="controls">
{{ Form::password('password_confirmation', array('class'=>'span3', 'placeholder'=>'Confirm Password')) }}
</div>
</div> <!-- /field -->
<div class="control-group">
<label for="type_user" class="control-label">Tipe Pengguna:</label>
<div class="controls">
{{ Form::radio('level', '1'); }} Supervisor
{{ Form::radio('level', '0'); }} Sales
</div>
</div> <!-- /field -->
</form>
</div>
<div class="modal-footer">
{{ Form::submit('Simpan', array('class'=>'button btn btn-primary','id'=>'mdl_save_change'))}}
<button class="btn" data-dismiss="modal" aria-hidden="true">Batal</button>
</div>
{{ Form::close() }}
</div>
then i use the controller to save the details:
public function postAddpengguna(){
/* function to add user in data pengguna */
$validator = Validator::make(Input::all(), User::$rules);
if($validator -> passes()){
$user = new User;
$user->firstname = Input::get('firstname');
$user->lastname = Input::get('lastname');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->level = Input::get('level');
/* save the following details */
$user->save();
return Redirect::to('pengguna');
} else {
return Redirect::to('index');
}
}
but the form doesn't save any data to database. I have another page called registration and it works.
my questions:
how to trace POST from laravel form submit, is there any browser extension?
how to trace error log in laravel
any ideas what's going on in my problem?
thank you in advance.
UPDATE
Here's the screenshot that describe how this works.
bootstrap modal:
when i press the submit button (blue button in modal) i want it to save the data to db. The function php is shown above.
PS. i don't use any AJAX to call the value from the FORM. But when i use the AJAX, it always error because TOKEN is missing.
First of all check the action and _token field of form . To add token field in your form you should include the following line in your form:
<input type="hidden" name="_token" value="{{csrf_token()}}">
To re-use bootstrap modal in your project you can check this Github link
In the latest versions of laravel 5 you can use a shortcut to get the token field.
<form ... >
{!! csrf_field() !!}
</form>
In this case you'll get something like
<input type="hidden" name="_token" value="hpyL7cUbCMFBGRfCi2dpzE5XHGj8WuyY2jqloKRx">
You can in any case get the token string calling csrf_token(), anyway I honestly prefer the csrf_field() alternative.
You may use this code with your ajax code:
$(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': {!! json_encode(csrf_token()) !!}
}
});
});