How to attach an element to a form - php

I'm writing a dynamic form that appends available text-fields depending on value that was selected by the user. How do I make it work? For context, it's an edit button, so the text-field will be pre-filled with STRING from the database.
The functions of getting the data from the database works, and the modal also shows, but the problem is just that the select-form doesn't work.
I tried calling "#btn-edit-account" but it doesn't work.
Code for the modal edit-account
<div class="modal fade" id="edit-account-modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Edit Account Details</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="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-add" role="tabpanel" aria-labelledby="nav-home-tab">
<form id="form-edit-account">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<div class="container my-2">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Role</label>
<select type="select" id="select-role" class="form-control edit_role select-role" name="role_id" required>
</select>
</div>
<div class="form-group student-number-container hidden">
<label>Student Number</label>
<input type="text" class="form-control" id="edit_student_number" name="student_number" required>
</div>
<div class="organization-container hidden">
</div>
<div class="form-row">
<div class="col-md-6">
<label>First name</label>
<input type="text" class="form-control" id="edit_first_name" name="first_name" required>
</div>
<input type="hidden" id="token" name="_token" value="{{csrf_token()}}">
<div class="col-md-6">
<label>Last Name</label>
<input type="text" class="form-control" id="edit_last_name" name="last_name" required>
</div>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" id="edit_email" name="email">
</div>
<div class="form-row">
<label class="control-label">New Password <span class="required">*</span></label>
<input type="password" id="new_password" name="new_password" minlength="6" class="form-control password" required>
</div>
<div class="form-row">
<label class="control-label">Confirm New Password <span class="required">*</span></label>
<input type="password" id="confirm_password" name="confirm_password" minlength="6" class="form-control password" required>
</div>
<div class="form-group text-right mt-4">
<button type="button" class="btn btn-dark" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success btn-confirm-edit-account">Update</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
Javascript for btn-edit-account
$(document).on('click', '.btn-edit-account', function() {
$('#edit-account-modal').modal('show');
var id = $(this).attr('data-id');
$.ajax({
url: "users/get-specific-user-info",
type: "POST",
data: {
id: id,
_token: "{{csrf_token()}}"
},
success: function(data) {
// $('#edit_role').val(data.role_id);
$('#edit_first_name').val(data.first_name);
$('#edit_last_name').val(data.last_name);
$('#edit_email').val(data.email);
console.log(data);
}
});
});
});
Javascript for select-role
$(document).on('change', '#select-role', function() {
var val = $(this).val();
if(val != 3){
$('.student-number-container').show();
}
else {
$('.student-number-container').hide();
}
if(val != 1){
$('.organization-container').html("");
}
else {
appendOrganization();
$('.organization-container').show();
}
});
function for appendOrganization
function appendOrganization() {
var html = "";
html += '<div class="form-group"> <label>Organization Name</label> <input type="text" class="form-control" name="organization_name" required> </div>';
html += '<div class="form-group"> <label>Organization Type</label> <select type="select" id="select-org-type" class="form-control" name="organization_type" required>'+
'<option value="" selected disabled>Select Organization Type</option>'+
'<option value="TYPE A">TYPE A</option>'+
'<option value="TYPE B">TYPE B</option>'+
'</select></div>';
html += '<div class="form-group"> <label>College</label> <select type="select" id="select-org-type" class="form-control" name="organization_college" required> <option value="" selected disabled>Select Organization Type</option> <option value="COLLEGE ONE">COLLEGE ONE</option> <option value="COLLEGE TWO">COLLEGE TWO</option> </select> </div>';
$('.organization-container').html(html);
}
The select in the modal should show the available choices in the given select, then will dynamically change the form depending on the selected choice.

Related

Post option value to mysql via ajax wont work

i have been working on some project and getting some experience on php and ajax, jquery. So i have checked 20+ question on stackoverflow but couldnt find the solution even if its already answered the same question.
I have tryed 10 diffrent solution but i guess im skipping something.
I have simple select box and im using ajax to send mysql but only select box doesnt work for some reason.
HTML Code:
<section class="resume-section p-3 p-lg-5 d-flex flex-column" id="rezervasyon">
<div class="my-auto">
<h2 class="mb-5">Online Rezervasyon</h2>
<form id="loginForm" method="" action="" novalidate="novalidate">
<div class="form-group row">
<label for="rez_ad" class="col-sm-2 col-form-label">İsim Soyisim</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="rez_ad" id="rez_ad" placeholder="İsim Soyisim">
</div>
</div>
<div class="form-group row">
<label for="rez_saat" class="col-sm-2 col-form-label">Rezervasyon Saati</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="rez_saat" id="rez_saat" placeholder="Rezervasyon saati">
</div>
</div>
<div class="form-group row">
<label for="rez_gsm" class="col-sm-2 col-form-label">Cep Numaranız</label>
<div class="col-sm-10">
<input type="number" class="form-control" name="rez_gsm" id="rez_gsm" placeholder="İletişim numaranız.">
</div>
</div>
<div class="form-group row">
<label for="rez_tarih" class="col-sm-2 col-form-label">Tarih</label>
<div class="col-sm-10">
<input type="date" class="form-control" name="rez_tarih" id="rez_tarih" placeholder="Tarih belirtiniz.">
</div>
</div>
<div class="form-group row">
<label for="rez_email" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" name="rez_email" id="rez_email" placeholder="Email">
</div>
</div>
<div class="form-group row">
<label for="rez_tip" class="col-sm-2 col-form-label" name="rez_tip" id="rez_tip">Rezervasyon Tipi</label>
<div class="col-sm-10">
<select id="rez_tip" class="custom-select">
<option value="" selected>Rezervasyon Tipi Seçiniz</option>
<option value="1">Cuma Fix Menü</option>
<option value="2">Cumartesi Fix Menü</option>
<option value="3">Haftaiçi ALKOLSÜZ</option>
<option value="4">Haftaiçi ALKOLLÜ</option>
<option value="5">Kutlama / Doğum Günü</option>
<option value="6">Diğer</option>
</select>
</div>
</div>
<div class="form-group row">
<label for="rez_sayi" class="col-sm-2 col-form-label" id="rez_sayi" name="rez_sayi">Kişi Sayısı</label>
<div class="col-sm-10">
<select id="rez_sayi" class="custom-select">
<option value="" selected>Kişi Sayısı Belirtiniz</option>
<option value="1">2 Kişilik Rezervasyon</option>
<option value="2">4 Kişilik Rezervasyon</option>
<option value="3">4+ Kişilik Rezervasyon</option>
<option value="4">6+ Kişilik Rezervasyon</option>
<option value="5">8+ Kişilik Rezervasyon</option>
<option value="6">20+ Kişilik Rezervasyon</option>
</select>
</div>
</div>
<textarea name="rez_aciklama" id="rez_aciklama" class="form-control" rows="6" cols="21" required="required"
placeholder="Ek açıklama girebilirsiniz."></textarea>
<form class="was-validated">
<!-- Button trigger modal -->
<!-- Modal -->
<div class="modal fade" id="myModal" 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 text-center" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="divider"></div>
<div class="right-side">
<button type="button" class="btn btn-danger btn-link">Delete</button>
</div>
</div>
</div>
</div>
</div>
<div class="custom-control custom-checkbox mb-3">
<input type="checkbox" class="custom-control-input" id="customControlValidation1" required>
<label class="custom-control-label" for="customControlValidation1">Rezervasyon Şartlarını Kabul Ediyorum!</label>
<div class="invalid-feedback">Rezervasyon şartlarını okuyunuz!</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="button" class="btn btn-success" name="insert-data" id="insert-data" onclick="insertData()">Rezervasyonu Tamamla</button>
</div>
</div>
<br>
<p id="message"></p>
</form>
Here is my php file ;
<?php
include('db.php');
$rez_ad=$_POST['rez_ad'];
$rez_saat=$_POST['rez_saat'];
$rez_gsm=$_POST['rez_gsm'];
$rez_tarih=$_POST['rez_tarih'];
$rez_tip=$_POST['rez_tip'];
$rez_sayi=$_POST['rez_sayi'];
$rez_aciklama=$_POST['rez_aciklama'];
$stmt = $DBcon->prepare("INSERT INTO student(rez_ad,rez_saat,rez_gsm,rez_tarih,rez_tip,rez_sayi,rez_aciklama) VALUES(:rez_ad, :rez_saat,:rez_gsm,:rez_tarih,:rez_tip,:rez_sayi,:rez_aciklama)");
$stmt->bindparam(':rez_ad', $rez_ad);
$stmt->bindparam(':rez_saat', $rez_saat);
$stmt->bindparam(':rez_gsm', $rez_gsm);
$stmt->bindparam(':rez_tarih', $rez_tarih);
$stmt->bindparam(':rez_tip', $rez_tip);
$stmt->bindparam(':rez_sayi', $rez_sayi);
$stmt->bindparam(':rez_aciklama', $rez_aciklama);
if($stmt->execute())
{
$res="Data Inserted Successfully:";
echo json_encode($res);
}
else {
$error="Not Inserted,Some Probelm occur.";
echo json_encode($error);
}
?>
Here is my jquery code;
function insertData() {
var rez_ad=$("#rez_ad").val();
var rez_saat=$("#rez_saat").val();
var rez_gsm=$("#rez_gsm").val();
var rez_tarih=$("#rez_tarih").val();
var rez_tip=$("#rez_tip").val();
var rez_sayi=$("#rez_sayi").val();
var rez_aciklama=$("#rez_aciklama").val();
// AJAX code to send data to php file.
$.ajax({
type: "POST",
url: "rez/insert-data.php",
data: {rez_ad:rez_ad,rez_saat:rez_saat,rez_gsm:rez_gsm,rez_tarih:rez_tarih,rez_tip:rez_tip,rez_sayi:rez_sayi,rez_aciklama:rez_aciklama},
dataType: "JSON",
success: function(data) {
$("#message").html(data);
$("p").addClass("alert alert-success");
},
error: function(err) {
alert(err);
}
});
}
Thanks for help !
You should then pass the rez_tip option in ajax call also.
<script type="text/javascript">
function insertData() {
//var rez_tip=$("#rez_tip option:selected").text();
//var rez_sayi=$("#rez_sayi option:selected").text();
var rez_tip=$("select#rez_tip").val();
var rez_sayi=$("select#rez_sayi").val();
// AJAX code to send data to php file.
$.ajax({
type: "POST",
url: "rez/insert-data.php",
data: {rez_sayi:rez_sayi,rez_tip:rez_tip},
dataType: "JSON",
success: function(data) {
$("#message").html(data);
$("p").addClass("alert alert-success");
},
error: function(err) {
alert(err);
}
});
}
</script>

How can i show a pop up modal after executing function in laravel?

Hey guys so i have done a simple contact form to send out emails. How can i show a modal or a notification when i press the send button that the email has been sent? but it has to execute the email sending function first then only show the modal.
Blade:
<form role="form" enctype="multipart/form-data" action="{{action('AltHr\Portal\PortalController#sendemail')}}" method="post" class="m-t-15">
{{csrf_field()}}
<div class="form-group form-group-default">
<label>Full Name<span style="color:red">*</span></label>
<input type="text" name="name" placeholder="As per IC" class="form-control" required>
</div>
<div class="form-group form-group-default">
<label>Company<span style="color:red">*</span></label>
<input type="text" name="company" placeholder="Company name" class="form-control" required>
</div>
<div class="form-group form-group-default">
<label>Email<span style="color:red">*</span></label>
<input type="email" name="email" placeholder="Company email preferred" class="form-control" required>
</div>
<div class="form-group form-group-default">
<label>Phone Number</label>
<input type="number" name="number" placeholder="Phone number optional" class="form-control">
</div>
<div class="form-group form-group-default">
<label>Category</label>
<select name="category" class="full-width form-control">
<option value="General">General</option>
<option value="Sign Up">Sign Up</option>
<option value="Onboarding">Onboarding</option>
<option value="Expenses">Expenses</option>
<option value="Travels">Travels</option>
<option value="Leaves">Leaves</option>
<option value="Suggestions">Suggestions</option>
</select>
</div>
<div class="form-group form-group-default">
<label>Message<span style="color:red">*</span></label>
<textarea name="text" placeholder="Please type your message here" style="height:100px" class="form-control" required></textarea>
</div>
<div class="form-group form-group-default">
<label>Attachment</label>
<input type="file" name="files[]" accept="file_extension|image/*|media_type" multiple>
</div>
<div class="sm-p-t-10 clearfix">
<i class="pull-left small hint-text m-t-5 font-arial"><span style="color:red">*</span>indicates required field</i>
<button class="btn btn-primary font-montserrat all-caps fs-12 pull-right xs-pull-left">Submit</button>
</div>
<div class="clearfix"></div>
</form>
Route:
Route::post('send-email','PortalController#sendemail');
Controller:
public function sendemail(Request $request)
{
$data = array(
'name'=> $request->name,
'email'=> $request->email,
'text'=> $request->text,
'category'=> $request->category,
'company'=> $request->company,
'number'=> $request->number
);
$files = $request->file('files');
\Mail::send('AltHr/Portal/supportemail', $data, function ($message) use($data, $files){
$message->from($data['email'], $data['name']);
$message->to('ra7veer#gmail.com')->subject($data['company'] . ' - ' .$data['category']);
if(count($files > 0)) {
foreach($files as $file) {
$message->attach($file->getRealPath(), array(
'as' => $file->getClientOriginalName(), // If you want you can chnage original name to custom name
'mime' => $file->getMimeType())
);
}
}
});
\Mail::send('AltHr/Portal/supportresponse', $data, function ($message) use($data){
$message->from('ra7veer#gmail.com', 'alt.hr Support');
$message->to($data['email'])->subject('Greetings from alt.hr');
});
return view('AltHr.Portal.support');
}
The code is working fine now i just need to how a modal to say that the email has been sent out. how can i do that?
Modal:
<div id="emailSentModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
You can use a session variable. If this session variable has value then the modal will show. something like this.
Controller
public function sendemail(Request $request)
.....
...
return view('AltHr.Portal.support')->with('modal','true');
Blade
<script>
#if(session()->has('modal'))
$("#emailSentModal").modal("toggle");
#endif
</script>
You could do it by using Ajax, sending the POST request with it, and showing the modal in Javascript after getting the response.
Something similar to:
$.ajax({
type: "POST",
url: {{action('AltHr\Portal\PortalController#sendemail')}},
data: {
// Your data here
},
success: function(msg)
{
$("#emailSentModal").modal("toggle");
}
});

Clear data in modal when clicking close in Laravel

I have a modal (Bootstrap) for creating a new product. When I click the "close" button, which creates a new product, the old data still does not clear from the modal.
My code:
<div class="modal fade" id="product" role="dialog" aria-hidden="true" data-target="#myModal" data-backdrop="static" data-keyboard="false" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Create new product</h4>
</div>
<div class="modal-body">
<form role="form" action="{{ route('admin.product.addProduct')}}" method="post" id="frmProduct">
{{ csrf_field() }}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="">Product Name</label>
<input type="text" class="form-control" id="" name="product_name">
</div>
<div class="form-group">
<label for="">Product Type</label>
<input type="text" class="form-control" id="" name="product_type_id">
</div>
<div class="form-group">
<label for="">Price</label>
<input type="text" class="form-control" id="" name="price">
</div>
<div class="form-group">
<label for="">Status</label>
<select class="form-control input-sm m-bot15" id="" name="status">
<option value="0">Inactive</option>
<option value="1">Active</option>
</select>
</div>
<div class="form-group">
<label for="img">Image</label>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-default btn-file">
Choose <input type="file" id="imgInp">
</span>
</span>
<input type="text" class="form-control" name="product_image" readonly>
</div>
<img id='img-upload' class="image_responsive" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="">Description</label>
<textarea class="form-control" id="" name="description"></textarea>
</div>
<div class="form-group">
<label for="">Note</label>
<textarea class="form-control" id="" name="addition_information"></textarea>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<input type="submit" value="Save" class="btn btn-success">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
How can I clear the data when closing the modal?
You can add event listener to your close button by adding an id to your close button for example id=close-btn:
document.getElementById("close-btn").addEventListener("click", function(){
document.getElementById("frmProduct").reset();
});
You can use hidden.bs.modal event:
This event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete).
The snippet:
$('#product').on('hidden.bs.modal', function(e) {
$(this).find('form').trigger('reset');
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#product">
Launch modal
</button>
<div class="modal fade" id="product" role="dialog" aria-hidden="true" data-target="#myModal" data-backdrop="static" data-keyboard="false" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Create new product</h4>
</div>
<div class="modal-body">
<form role="form" action="{{ route('admin.product.addProduct')}}" method="post" id="frmProduct">
{{ csrf_field() }}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="">Product Name</label>
<input type="text" class="form-control" id="" name="product_name">
</div>
<div class="form-group">
<label for="">Product Type</label>
<input type="text" class="form-control" id="" name="product_type_id">
</div>
<div class="form-group">
<label for="">Price</label>
<input type="text" class="form-control" id="" name="price">
</div>
<div class="form-group">
<label for="">Status</label>
<select class="form-control input-sm m-bot15" id="" name="status">
<option value="0">Inactive</option>
<option value="1">Active</option>
</select>
</div>
<div class="form-group">
<label for="img">Image</label>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-default btn-file">
Choose <input type="file" id="imgInp">
</span>
</span>
<input type="text" class="form-control" name="product_image" readonly>
</div>
<img id='img-upload' class="image_responsive" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="">Description</label>
<textarea class="form-control" id="" name="description"></textarea>
</div>
<div class="form-group">
<label for="">Note</label>
<textarea class="form-control" id="" name="addition_information"></textarea>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<input type="submit" value="Save" class="btn btn-success">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>

google re captcha not showing up

I have implemented google re captcha in a modal popup. I have also included the api ,i.e recaptcha/api.js to enable the recpatcha. But it is not showing up when I invoke the popup.
When I right click on the popup and inspect, the div tag which includes the google recaptcha is being seen in the elements section .
I have included the modal popup in my footer page which is called by every page in my website.
My code:
<script src="https://www.google.com/recaptcha/api.js"></script>
<!-- Request A Demo POPUP(Start) -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<?php echo form_open(base_url( 'employer-request-demo/request-demo/submit' ), array( 'id' => 'idfrmdemo', 'name' =>'frmdemo', 'method'=>'post'));?>
<!-- input type="text" name="<?php //echo $this->security->get_csrf_token_name();?>" value="<?php //echo $this->security->get_csrf_hash();?>"-->
<div class="modal-content">
<div class="modal-header" align="center">
<button type="button" id="frmdemo-close" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><strong>Request a Demo</strong></h4>
</div>
<div class="modal-body" id="idmodalcontent">
<div class="row">
<div class="form-group col-md-6">
<label for="txtfirstname">FIRST NAME</label>
<input name="txtfirstname" type="text" class="form-control required" placeholder="Please enter your first name" id="idfirstname" >
</div>
<div class="form-group col-md-6">
<label for="txtlastname">LAST NAME</label>
<input name="txtlastname" type="text" class="form-control required" placeholder="Please enter your last name" id="idlastname">
</div>
</div>
<div class="form-group">
<label for="Email">CORPORATE EMAIL</label>
<input type="email" name="txtemail" class="form-control" placeholder="Please enter your email id" id="idemail">
<div class="divstyle"></div>
</div>
<div class="form-group">
<label for="idcompany">COMPANY NAME</label>
<input type="text" name="txtcompany" placeholder="Please enter the name of your company" class="form-control" id="idcompany" >
</div>
<div class="form-group">
<label for="Phone">PHONE</label>
<input type="tel" name="txtphone" placeholder="Please enter your phone number" class="form-control" id="idphone">
</div>
<div class="form-group">
<label for="">MODE OF CONTACT</label>
<select name="selmode" id="idmode" class="form-control">
<option value="" disabled selected>Select mode of contact</option>
<option value="email">Email</option>
<option value="phone">Phone</option>
<option value="any">Any</option>
</select>
</div>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="6LexRREUAAAAAELhZaiO5FxRbgnyWEwIxFTWeHhE"></div>
<input type="hidden" class="hiddenRecaptcha required" name="cphiddenRecaptcha" id="idcphiddenRecaptcha">
</div>
</div>
<div class="modal-footer">
<div id="btn-smb">
<input type="submit" id="idsubmit" name="submit" value="Submit" class="btn btn-warning" />
</div>
<div id="btn-close" style="display:none;">
<input type="button" name="close" value="Close" class="btn btn-warning" id="idclose"/>
</div>
</div>
</div>
<input type="hidden" name="context" id="frm_context" value="Request A Demo" />
<?php echo form_close();?>
</div>
</div>
<!-- Request A Demo POPUP(End) -->
I am getting the following error in the console:
XMLHttpRequest cannot load javascript:void(0);. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

Uploading image with another form text input in codeigniter?

i'm have confusing problem about uploading image in multiple input text like registration page. i found solution but the image is not save to database or folder.
please help me all...
my admin.php view
<script>
//upload img
$('#form').ajaxForm({
//uploadimg is my form id
dataType: 'json',
success: processJson
});
function processJson(data) {
if(data.msg=="success"){
alert('Upload sukses.');
}
else{
alert('Upload gagal.');
}
}
function add_edulibs()
{
save_method = 'add';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
$('#modal_form').modal('show'); // show bootstrap modal
$('.modal-title').text('Tambah Data'); // Set Title to Bootstrap modal title
}
function edit_edulibs(id)
{
save_method = 'update';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
//Ajax Load data from ajax
$.ajax({
url : "<?php echo site_url('edulibs/ajax_edit/')?>/" + id,
type: "GET",
dataType: "JSON",
success: function(data)
{
$('[name="id"]').val(data.id);
$('[name="nama"]').val(data.nama);
$('[name="nim"]').val(data.nim);
$('[name="pembimbing"]').val(data.pembimbing);
$('[name="subyek"]').val(data.subyek);
$('[name="judul"]').val(data.judul);
$('[name="tanggal"]').datepicker('update',data.tanggal);
$('[name="alamat"]').val(data.alamat);
$('[name="kontak"]').val(data.kontak);
$('[name="email"]').val(data.email);
$('[name="penerbit"]').val(data.penerbit);
$('[name="file_upload"]').val(data.gambar);
$('#modal_form').modal('show'); // show bootstrap modal when complete loaded
$('.modal-title').text('Edit Data'); // Set title to Bootstrap modal title
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error mendapatkan data');
}
});
}
</script>
<div class="modal fade" id="modal_form" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h3 class="modal-title">Kelola Data</h3>
</div>
<div class="modal-body form">
<form action="<?php echo base_url('edulibs/do_upload/')?>" method="post" enctype="multipart/form-data" id="form" class="form-horizontal">
<input type="hidden" value="" name="id"/>
<div class="form-body">
<div class="form-group">
<label class="control-label col-md-3">Nama</label>
<div class="col-md-9">
<input name="nama" placeholder="Nama Lengkap" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">NIM</label>
<div class="col-md-9">
<input name="nim" placeholder="Nomor Induk Mahasiswa" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Pembimbing</label>
<div class="col-md-9">
<input name="pembimbing" placeholder="Nama Pembimbing" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Subyek</label>
<div class="col-md-9">
<input name="subyek" placeholder="Subyek" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Judul</label>
<div class="col-md-9">
<textarea name="judul" placeholder="Judul Karya Tulis" class="form-control"></textarea>
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Tanggal Terbit</label>
<div class="col-md-9">
<input name="tanggal" placeholder="Tahun-Bulan-Hari" class="form-control datepicker" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Alamat</label>
<div class="col-md-9">
<textarea name="alamat" placeholder="Alamat Lengkap" class="form-control"></textarea>
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Kontak</label>
<div class="col-md-9">
<input name="kontak" placeholder="Nomor Kontak" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Email</label>
<div class="col-md-9">
<input name="email" placeholder="Alamat e-mail" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Penerbit</label>
<div class="col-md-9">
<input name="penerbit" placeholder="Nama Penerbit" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Foto</label>
<div class="col-md-9">
<input type="file" name="file_upload" />
<span class="help-block"></span>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Simpan</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Batalkan</button>
</div>
My edulibs.php controller
public function admin()
{
$this->load->view('admin');
}
function do_upload(){
$config['upload_path'] = "./uploads/foto/";
$config['allowed_types'] = 'gif|jpg|png|JPEG';
$config['file_name'] = url_title($this->input->post('file_upload'));
$this->upload->initialize($config);
if(!$this->upload->do_upload('file_upload'))
{
echo $this->upload->display_errors();
}
else{
$data = array(
'gambar'=>$this->upload->file_name
);
$this->edulib_model->insert($data,'edulib');
}
}
My edulib_model.php
public function save($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
public function insert($data,$table){
$this->db->insert($data,$table);
}

Categories