I have a problem when updating the password of student, I am using the modal of bootstrap: -> here is the picture updatepassword
here is my modal and the action is studentpasswordupdate/updatepasswordstudent, I retrieve the value using echo $Password in session
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title text-center">Change Password</h4>
</div>
<div class="modal-body"><br>
<form action="<?php echo site_url("studentpasswordupdate/updatepasswordstudent") ?>" method="POST">
<div class="l">
<label class=" input-lg" style="font-size: 28px;">Password:</label> <input class="k" type="text" name="Password" value="<?php echo $Password ?>" >
</div>
<br> <br><br>
<div class="modal-footer">
<button type="submit" name="Submit" class="btn btn-info m" >Save</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
</div>
and here is my controller/method:
class Studentpasswordupdate extends CI_Controller {
//put your code here
function updatepasswordstudent(){
$id = $this->input->post('Id');
$data= array(
'Password' => $this->input->post('Password')
);
$this->db->where('Id', $id);
$this->db->update('studentinformation',$data);
$hasError = $this->db->_error_message();
echo $hasError;
redirect('studentform/index', 'refresh');
}
}
Try this
class Studentpasswordupdate extends CI_Controller {
function updatepasswordstudent()
{
$id = $this->input->post('Id');
$data = array(
'Password' => $this->input->post('Password')
);
$this->db->where('Id', $id);
if (!$this->db->update('studentinformation',$data))
{
$hasError = $this->db->_error_message();
echo $hasError;
}
else{
redirect('studentform/index', 'refresh');
}
}
}
In View
Set name field foe this
<input type="hidden" name="Id" value="<?php echo $Id ?>">
Related
I want to validate all field. But It is validating only first field. Append field is not validating
multiple fields validation using livewire? name.0.required is working but name.*.required is not working.
<?php
namespace App\Http\Livewire;
use Livewire\Component;
class Test extends Component
{
public $name;
public $inputs = [];
public $i = 1;
public function add($i)
{
$i = $i + 1;
$this->i = $i;
array_push($this->inputs ,$i);
}
public function remove($i)
{
unset($this->inputs[$i]);
}
public function store()
{
$validatedDate = $this->validate([
'name.0' => 'required',
'name.*' => 'required',
],
[
'name.0.required' => 'name field is required',
'name.*.required' => 'name field is required',
]
);
session()->flash('message', 'Name Has Been Created Successfully.');
}
public function render()
{
return view('livewire.test');
}
}
<div>
<form class="offset-md-3">
#if (session()->has('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
#endif
<div class="add-input">
<div class="row">
<div class="col-md-5">
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter Name" wire:model="name.0">
#error('name.0') <span class="text-danger error">{{ $message }}</span>#enderror
</div>
</div>
<div class="col-md-2">
<button class="btn text-white btn-info btn-sm" wire:click.prevent="add({{$i}})">Add</button>
</div>
</div>
</div>
#foreach($inputs as $key => $value)
<div class=" add-input">
<div class="row">
<div class="col-md-5">
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter Name" wire:model="name.{{ $value }}">
#error('name.'.$value) <span class="text-danger error">{{ $message }}</span>#enderror
</div>
</div>
<div class="col-md-2">
<button class="btn btn-danger btn-sm" wire:click.prevent="remove({{$key}})">remove</button>
</div>
</div>
</div>
#endforeach
<div class="row">
<div class="col-md-12">
<button type="button" wire:click.prevent="store()" class="btn btn-success btn-sm">Save</button>
</div>
</div>
</form>
</div>
This is my full code use to create multi field using livewire. I am not able to validate appended field so I need help to solve this problem. This validate first field name.0 other append field name.* does not validate.
You should use $key instead $value.
Like this:
wire:model="name.{{ $key }}"
hello i am using modal form and if login successful , the successful message is shown in alertity message box. Why my message is not shown? If i am successful login stay on same page and show alertity message in message box & if failed then show failed message and show modal again? code is working and redirect on same page after login and failed in login.
Controller:
public function submitUser(Request $req)
{
$req->validate([
'email' => 'required|email',
'password' => 'required',
]);
$email_address=$req->input('email');
$password=$req->input('password');
$query_select=DB::table('table_users')->where('email','=',$email_address)->where('password','=',$password)->first();
if($query_select)
{
if($email_address==$query_select->email && $password ==$query_select->password)
{
$remember=$req->input('remember');
$req->session()->put('name',$query_select->name);
$req->session()->put('user_id',$query_select->id);
$req->session()->put('user_email',$query_select->email);
if(!empty($remember))
{
setcookie ("email",$query_select->email,time()+ (10 * 365 * 24 * 60 * 60));
setcookie ("password",$query_select->password,time()+ (10 * 365 * 24 * 60 * 60));
return redirect()->back()->with('status_of_login','You log in successful');
}
else
{
if(isset($_COOKIE["email"]))
{
setcookie ("email","");
}
if(isset($_COOKIE["password"]))
{
setcookie ("password","");
}
return redirect()->back()->with('status_of_login','You log in successful');
}
}
else
{
// $req->session()->flash('error','You have entered wrong credentials');
return redirect()->back()->with('status_of_login_wrong','Your credentials are wrong');
}
}
else
{
return redirect()->back()->with('status_of_login_wrong','Your credentials are wrong');
}
}
html:
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Login safely</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="card">
<center>
<div class="social-media">
<i class="fa fa-facebook-f fa_fb_new"></i>
<i class="fa fa-google fa_go_new"></i>
</div>
</center>
<form class="ps-product__review" action="{{ route('submit_user')}}" method="post" enctype="multipart/form-data">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
<div class="form-group">
<label for="inputEmail">Email</label>
<input type="email" class="form-control" id="" placeholder="Enter Email" name="email" value="<?php if(isset($_COOKIE["email"])) { echo $_COOKIE["email"]; } ?>" required>
<span class="text-danger">{{ $errors->first('email') }}</span>
</div>
<div class="form-group">
<label for="inputPassword">Password</label>
<input type="password" class="form-control" id="" placeholder="Enter Password" name="password" value="<?php if(isset($_COOKIE["password"])) { echo $_COOKIE["password"]; } ?>" required>
<span class="text-danger">{{ $errors->first('password') }}</span>
</div>
<div class="form-group">
<label class="form-check-label"><input type="checkbox" name="remember" <?php if(isset($_COOKIE["email"])) { ?> checked <?php } ?>> Remember me</label>
</div>
<button type="submit" class="ps-btn-login">Log In</button>
Sign Up
</form>
</div>
</div>
</div>
</div>
</div>
in footer:
<script type="text/javascript" src="{{ asset('js/alertify.min.js')}}"></script>
<script type="text/javascript">
#if(session('status_of_login'))
alertify.set('notifier','position', 'top-center');
alertify.success("{{session('status_of_login')}}");
#endif
</script>
change your footer code like
<script type="text/javascript" src="{{ asset('js/alertify.min.js')}}"></script>
#if(session('status_of_login'))
<script type="text/javascript">
$(function(){
alertify.set('notifier','position', 'top-center');
alertify.success("{{session('status_of_login')}}");
alertify.error("{{session('status_of_login_wrong')}}");
})
</script>
#endif
here it check have any session is there or not once it got session then it trigger onload function of jQuery
I am working on Code Igniter and stuck in a place where I need to change the password of a signed-in user. I need help in picking up the user id of the signed-in user and through it I want to update the password of that user.
The following are the images of controller and models created of the project respectively.
Create html link:
<i class="fa fa-circle-o"></i>Change password
Create user controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller
{
public function __construct()
{
parent::__construct();
// Load form helper library
$this->load->helper('form');
// Load form validation library
$this->load->library('form_validation');
// Load session library
$this->load->library('session');
$this->load->model('Data_model');
}
public function changePassword()
{
$web = array();
$web['title'] = 'Change password';
$web['content'] = 'web/password';
$this->form_validation->set_rules('old_password', 'Old password', 'required|callback_check_password');
$this->form_validation->set_rules('new_password', 'New password', 'required');
$this->form_validation->set_rules('confirm_password', 'Confirm password', 'required|matches[new_password]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('web_template',$web);
} else {
$id = $this->user_id;
$data = array(
'user_password' => $this->input->post('new_password'),
);
$this->Common_model->Data_model('user_login', $data, 'id', $id);
$this->session->set_flashdata('msg', 'Password changed Successfully');
redirect('user/changePassword');
}
}
function check_password($password) {
if($this->user_id)
$id = $this->user_id;
else
$id = '';
$result = $this->Data_model->check_user_password($id, $password);
if($result > 0)
$response = true;
else {
$this->form_validation->set_message('check_password', 'Old password is wrong');
$response = false;
}
return $response;
}
}
Change password html page in view:
<section class="content">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title"><?php echo $title; ?></h3>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip" title="Collapse">
<i class="fa fa-minus"></i></button>
</div>
</div>
<?php
$attributes = array('class' => 'form-horizontal', 'id' => 'changePassword');
echo form_open('user/changePassword', $attributes);
?>
<div class="box-body">
<?php if (!empty($this->session->flashdata('msg'))) : ?>
<div class="alert alert-success alert-dismissable alertDiv"> <?php echo $this->session->flashdata('msg'); ?> </div>
<?php endif; ?>
<div class="row">
<div class="col-md-offset-2 col-md-8">
<div class="form-group">
<label for="inputEmail3" class="col-md-2 control-label">Old password</label>
<div class="col-md-8">
<input type="password" name="old_password" value="<?php echo (isset($form_data) ? $form_data->old_password : set_value('old_password')); ?>" class="form-control" id="old_password" placeholder="Old password">
<?php echo form_error('old_password', '<div class="error">', '</div>'); ?>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-offset-2 col-md-8">
<div class="form-group">
<label for="inputEmail3" class="col-md-2 control-label">New password</label>
<div class="col-md-8">
<input type="password" name="new_password" value="<?php echo (isset($form_data) ? $form_data->new_password : set_value('new_password')); ?>" class="form-control" id="new_password" placeholder="New password" autocomplete="off">
<?php echo form_error('new_password', '<div class="error">', '</div>'); ?>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-offset-2 col-md-8">
<div class="form-group">
<label for="inputEmail3" class="col-md-2 control-label">Confirm Password</label>
<div class="col-md-8">
<input type="password" name="confirm_password" value="<?php echo (isset($form_data) ? $form_data->confirm_password : set_value('confirm_password')); ?>" class="form-control" id="confirm_password" placeholder="Confirm Password" autocomplete="off">
<?php echo form_error('confirm_password', '<div class="error">', '</div>'); ?>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-offset-5" style="margin-top: 10px;">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
<?php form_close(); ?>
</div>
<!-- /.box -->
</section>
Create Data_model in model add this function:
// Update data to table
public function update($table, $data, $primaryfield, $id)
{
$this->db->where($primaryfield, $id);
$q = $this->db->update($table, $data);
return $q;
}
//Check the old password:
function check_user_password($id = '', $password) {
$this->db->where('user_password', $password);
$this->db->where('id', $id);
return $this->db->get('user_login')->num_rows();
}
i was creating vue file for inpute Excel or csv file laravel 5.7
i was using Maatwebsite/Laravel-Excel
dont know how to route there things it if my first time to make app like laravel with vue JS
Don't know what to doo
there is Vue file
<template>
<div class="container">
<div class="row mt-5">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">Students</h3>
<div class="card-tools">
<button class="btn btn-success" data-toggle="modal" data-target="#exampleModal">Add New <i class="fas fa-user-plus"></i></button>
<button class="btn btn-primary" data-toggle="modal" data-target="#importModel">Import <i class="fas fa-file-excel"></i></button>
<button class="btn btn-warning" data-toggle="modal" data-target="#export">Export <i class="fas fa-file-excel"></i></button>
</div>
</div>
</div>
<!-- /.card -->
</div>
</div><!-- /.row -->
<!-- Modal -->
<div class="modal fade" id="importModel" tabindex="-1" role="dialog" aria-labelledby="importModelLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="importModelLabel">Import Excel</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form method="POST" enctype="multipart/form-data">
<div class="modal-body">
<div class="form-group">
<label>Import</label>
<input type="file" name="excel_file"
class="form-control" :class="{ 'is-invalid': form.errors.has('excel_file') }">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save Student</button>
</div>
</form>
</div>
</div>
</div>
<!--Exit Modal-->
</div>
</template>
<script>
export default {
data(){
return{
form: new Form({
first_name:'',
last_name:'',
email:'',
password:'',
designation:''
})
}
},
mounted() {
console.log('Component mounted.')
}
}
</script>
set a post route to import data in web.php file
that will route the things as pr laravelExcel
Route::post('importExcel', 'API/StudentMasterController#import');
in a controller file
public function import()
{
Excel::import(new StudentImport, request()->file('excel_file'));
//return redirect('/')->with('success', 'All good!');
}
and in a APP/import/studentmaster.php file
public function model(array $row)
{
return new Student_master([
'EnrollmentNo' => $row[22],
]);
}
this is vue views and js my code :
<form method="POST" action="#" class="form-horizontal" #submit.prevent="createUpload" enctype="multipart/form-data">
<div class="box-body">
<div class="form-group row"><label for="name" class="col-sm-2 form-control-label">Parameter</label>
<div class="col-sm-10">
<input type="file" class="form-control" id="result_file" ref="myFiles" placeholder="Name Parameter" required="required" #change="previewFiles($event)">
<input type="hidden" name="result_file2" class="form-control" id="title" v-model="datafileupload.result_file3" placeholder="Name Parameter" required="required">
<input type="hidden" name="hasilcek" class="form-control" id="hasilcek" v-model="datafileupload.hasilcek3" placeholder="Name Parameter" required="required">
<input type="hidden" class="form-control" v-model="datapush.filename">
<input type="hidden" class="form-control" v-model="datapush.size">
</div><br><br><br>
<div v-if="this.hasilcekexcel > 0" class="alert alert-warning">File {{ namafile }} exists , do you want to replace it ?</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" #click="doSomethingOnHidden" >Close</button>
<input type="submit" class="btn btn-primary" id="submit_upload2" v-if="this.hasilcekexcel > 0" value="Replace">
<input type="submit" class="btn btn-primary" id="submit_upload" v-if="this.hasilcekexcel == 0" value="Upload">
</div>
</form>
js upload :
createUpload () {
var data = new FormData();
var file = this.$refs.myFiles.files[0];
var size = this.$refs.myFiles.files[0].size;
data.append('filenya', file);
data.append('namasifile', this.datafileupload.result_file3);
data.append('angkacek', this.datafileupload.hasilcek3);
axios.post('excelupload/upload', data)
.then(response => {
this.pesertas.push(response.data.data);
this.showModal =false;
});
},
controller :
$excelupload = new Exceluploads;
$hasilcek = $request->angkacek;
$requestfile = $request->namasifile;
if ($request->filenya != '') {
$input = Input::all();
$size = $request->file('filenya')->getClientSize();
$file = array_get($input,'result_file');
$userID = Auth::user()->id;
$uploaded_by = Auth::user()->name;
$destinationPath = 'excel';
// GET THE FILE EXTENSION
$extension = 'xls';
$fileName = $requestfile.'.'.$extension;
if ($hasilcek > 0) {
File::delete($destinationPath.'/'.$fileName);
$upload_success = $request->filenya->move($destinationPath, $fileName);
return response()->json(['data' => Exceluploads::find($excelupload->id)]);
}else if ($hasilcek == 0) {
$upload_success = $request->filenya->move($destinationPath, $fileName);
$excelupload->filename = $fileName;
$excelupload->directory = $destinationPath.'/'.$fileName;
$excelupload->size = $size;
//1 = not custome
$excelupload->type = 1;
$excelupload->uploaded_by = $uploaded_by;
$excelupload->save();
return response()->json(['data' => Exceluploads::find($excelupload->id)]);
}
}
There is this codeigniter tutorial from weblesson youtube page About working with Datatables and Codeigniter.
But i'm having some difficulties inserting data into the database.
Controller:
function user_action(){
if($_POST["action"] == "Add")
{
$insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post("last_name"),
'image' => $this->upload_image()
);
$this->load->model('crud_model');
$this->crud_model->insert_crud($insert_data);
echo 'Data Inserted';
}
}
function upload_image()
{
if(isset($_FILES["user_image"]))
{
$extension = explode('.', $_FILES['user_image']['name']);
$new_name = rand() . '.' . $extension[1];
$destination = './upload/' . $new_name;
move_uploaded_file($_FILES['user_image']['tmp_name'], $destination);
return $new_name;
}
}
View Class:
<div id="userModal" class="modal fade">
<div class="modal-dialog">
<form method="post" id="user_form">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add User</h4>
</div>
<div class="modal-body">
<label>Enter First Name</label>
<input type="text" name="first_name" id="first_name" class="form-control" />
<br />
<label>Enter Last Name</label>
<input type="text" name="last_name" id="last_name" class="form-control" />
<br />
<label>Select User Image</label>
<input type="file" name="user_image" id="user_image" />
</div>
<div class="modal-footer">
<input type="submit" name="action" class="btn btn-success" value="Add" />
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
When i ran this code, i got the following errors:
Error message
And when i did
if(isset($_POST["action"]) && $_POST["action"] == "Add")
i got an empty alert box
In controller,at your constructor load url helper. and save file as Main.php with code::
function __construct()
{
$this->load->helper('url');
}
function user_action(){
if($_POST["action"] == "Add")
{
$insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post("last_name"),
'image' => $this->upload_image()
);
$this->load->model('crud_model');
$this->crud_model->insert_crud($insert_data);
echo 'Data Inserted';
}
}
function upload_image()
{
if(isset($_FILES["user_image"]))
{
$extension = explode('.', $_FILES['user_image']['name']);
$new_name = rand() . '.' . $extension[1];
$destination = './upload/' . $new_name;
move_uploaded_file($_FILES['user_image']['tmp_name'], $destination);
return $new_name;
}
}
at your view define a input of type hidden with name action and value add.And define action of your form.As follows..
<div id="userModal" class="modal fade">
<div class="modal-dialog">
<form method="post" id="user_form" action="<?php echo base_url('main/user_action');?>">
<input type="hidden" name="action" value="add">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add User</h4>
</div>
<div class="modal-body">
<label>Enter First Name</label>
<input type="text" name="first_name" id="first_name" class="form-control" />
<br />
<label>Enter Last Name</label>
<input type="text" name="last_name" id="last_name" class="form-control" />
<br />
<label>Select User Image</label>
<input type="file" name="user_image" id="user_image" />
</div>
<div class="modal-footer">
<input type="submit" name="action" class="btn btn-success" value="Add" />
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
Hope it will works..
I did fix the problem but don't know if it's the best way to do it
what i did was change this two codes:
controller:
public function user_action(){
if(isset($_POST["action"]) && $_POST["action"] == "Add")
{
$insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post("last_name"),
'image' => $this->upload_image()
);
$this->load->model('crud_model');
$this->crud_model->insert_crud($insert_data);
echo 'Data Inserted';
}
and Ajax:
$(document).on('submit', '#user_form', function(event){
event.preventDefault();
var firstName = $('#first_name').val();
var lastName = $('#last_name').val();
var extension = $('#user_image').val().split('.').pop().toLowerCase();
if(jQuery.inArray(extension, ['gif','png','jpg','jpeg']) == -1)
{
alert("Invalid Image File");
$('#user_image').val('');
return false;
}
if(firstName != '' && lastName != '')
{
$.ajax({
url:"<?php echo base_url() . 'main/user_action'?>",
method:'POST',
data:new FormData(this),
contentType:false,
processData:false,
success:function(data)
{
alert(data);
$('#user_form')[0].reset();
$('#userModal').modal('hide');
dataTable.ajax.reload();
}
});
}
else
{
alert("Bother Fields are Required");
}
});
for controller:
where it say:
if(isset($_POST["action"]) && $_POST["action"] == "Add")
remove it
In in the Ajax Code:
create a link variable to get the action attribute of the form like so:
var link = $('#user_form').attr('action');
and parse it to to the ajax url.
And that's it!
I did fix the problem but don't know if it's the best way to do it
<div class="modal-footer">
<input type="hidden" name="action" class="btn btn-success" value="Add">
<input type="submit" name="action" class="btn btn-success" value="Add" />
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
This is a hidden field that we have used to describe database operations like insert, update, delete