I have facing an weird problem in my laravel application. In my view file there are a select input field where values are fetched from database table. I have checked that data successfully passed to the view and tried with below code which is not working
<div class="row form-group">
<div class="col col-md-3">
<label for="text-input" class=" form-control-label">Course</label>
</div>
<div class="col-12 col-md-9">
<select class="form-control" name="course_id" id="course" required>
<option value="">Please select</option>
#if ($courses)
#foreach ($courses as $course)
<option value="{{$course->id}}">{{$course->name}}</option>
#endforeach
#endif
</select>
</div>
</div>
But when I have paste the same code again then the second input field is working properly. I didn't get any error.
Edited:
I have some JavaScript which given below
<script>
$(document).ready(function(){
// Add click
$(".addBtn").click(function(){
var id = this.id;
// Get data from field
var sem = $("#"+id).data('sem');
var slotId = $("#"+id).data('slot');
var timeSlot = $("#"+id).data('timeslot');
var dayId = $("#day").data('dayid');
var dayValue = $("#day").data('dayvalue');
// Add data to model
$('#dayInput').text(dayValue);
$('#semesterInput').text(sem);
$('#timeInput').text(timeSlot);
// Hidden input value for db
$('#dayInputValue').val(dayId);
$('#semesterInputValue').val(sem);
$('#timeInputValue').val(slotId);
// Get assign teacher
$.ajax({
url: "{{URL::to('admin/teacher-assign/sem')}}/"+ sem,
type: 'GET',
success: function(data){
$('#course').html("<option value=\"\">Please select</option>");
$.each(data, function(key, value) {
$('#course')
.append($("<option></option>")
.attr("value",value.course.id +'-'+ value.teacher.id)
.text(value.course.name +'-'+ value.teacher.name));
});
}
});
});
// Edit click
$(".editBtn").click(function(){
var id = this.id;
// Get data from field
var sem = $("#"+id).data('sem');
var slotId = $("#"+id).data('slot');
var timeSlot = $("#"+id).data('timeslot');
var dayId = $("#day").data('dayid');
var dayValue = $("#day").data('dayvalue');
// Edit data
var course = $("#"+id).data('course');
// var teacher = $("#"+id).data('teacher');
var roomNo = $("#"+id).data('room_no');
var editNote = $("#"+id).data('note');
var routineId = $("#"+id).data('routine_id');
// Add data to model
$('#editDayInput').text(dayValue);
$('#editSemesterInput').text(sem);
$('#editTimeInput').text(timeSlot);
$('#editCourseInput').text(course);
// $('#editTeacherInput').text(teacher);
$('#editRoomNoInput').text(roomNo);
// Hidden input value for db
$('#editDayInputValue').val(dayId);
$('#editSemesterInputValue').val(sem);
$('#editTimeInputValue').val(slotId);
// Assign ID
$('#editRoutineIdValue').val(routineId);
$('#editNoteInputValue').val(editNote);
// Get assign teacher
$.ajax({
url: "{{URL::to('admin/teacher-assign/sem')}}/"+ sem,
type: 'GET',
success: function(data){
$('#editCourse').html("<option value=\"\">Please select for edit</option>");
$.each(data, function(key, value) {
$('#editCourse')
.append($("<option></option>")
.attr("value",value.course.id +'-'+ value.teacher.id)
.text(value.course.name +'-'+ value.teacher.name));
});
}
});
});
// Add button
$('td').mouseenter(function () {
$(this).find('.addBtn').show();
}).mouseleave(function () {
$(this).find('.addBtn').hide();
});
// Edit button
$('td').mouseenter(function () {
$(this).find('.editBtn').show();
}).mouseleave(function () {
$(this).find('.editBtn').hide();
});
// Delete button
$('td').mouseenter(function () {
$(this).find('.deleteBtn').show();
}).mouseleave(function () {
$(this).find('.deleteBtn').hide();
});
});
</script>
change #if ($courses) this into #if (count($courses) > 0) this and try again.
Related
I am trying to add products with AJAX into the cart from the products page with the quantity field. the addition of the first item is working fine with desired quantity but when I try to add a second product with a different quantity it keeps sending the first product quantity into the database.
here is my code
HTML
<div class="pro-quantity">
<div class="quantity">
<button class="decrement-btn">-</button>
<input type="text" id="sqty" maxlength="2" max="10" value="1" class="qty-input">
<button class="increment-btn">+</button>
</div>
</div>
<div class="pro-id-2">
<div class="tw-flex tw-justify-between tw-items-center">
<button class="pro-id-link" value="<? echo $id; ?>" id="add-deposit">Add to Cart</button>
<input type="hidden" name="" id="cus_id" value="<? echo $_SESSION['u_id']; ?>">
</div>
</div>
JS (for increment and decrement quantity)
$(document).ready(function () {
$('.increment-btn').click(function (e) {
e.preventDefault();
var incre_value = $(this).parents('.quantity').find('.qty-input').val();
var value = parseInt(incre_value, 10);
value = isNaN(value) ? 0 : value;
if (value < 10) {
value++;
$(this).parents('.quantity').find('.qty-input').val(value);
}
});
$('.decrement-btn').click(function (e) {
e.preventDefault();
var decre_value = $(this).parents('.quantity').find('.qty-input').val();
var value = parseInt(decre_value, 10);
value = isNaN(value) ? 0 : value;
if (value > 1) {
value--;
$(this).parents('.quantity').find('.qty-input').val(value);
}
});
});
JS (AJAX code)
$(document).ready(function () {
$(document).on("click", "#add-deposit", function (e) {
e.preventDefault();
var element = $(this);
var id = $(this).val();
var cus_id = $('#cus_id').val();
var sqty = $('#sqty').val();
$.ajax({
url: "add_deposit_script.php",
type: "POST",
data: {
id: id,
cus_id: cus_id,
sqty: sqty
},
success: function (data) {
if (data === 'success') {
Swal.fire({
icon: 'success',
title: 'Success.',
text: 'Success! This product has been added into Deposit.'
})
}
if (data === 'error') {
Swal.fire({
icon: 'error',
title: 'Error.',
text: 'Error! This Product is already in your deposit.'
})
}
element.prop('disabled', true);
element.html('Added');
getcartnumber();
}
});
});
});
i am trying to build an crud operation with php and js(ajax) and i have some filters which user can add more filters to a category works fine on adding operation, but on edit i did with ajax and on click is retriving from databse and fill the inputs but on select2 i retrive data like this:
var main_id = $(this).attr("id");
$.ajax({
url:"<?php echo $site_url;?>/actions/get_filters.php",
method:"POST",
data:{main_id:main_id},
dataType:"json",
success:function(data){
var fl = [];
var nr = [];
$.each(data, function(i, field){
fl.push(field.fil_opt+","+field.txt_fil);
nr.push(field.id);
});
for (i = 0; i < nr.length; ++i) {
$('#filters_cat_update > option').each(function() {
$(this).attr('data-id', nr[i++]);
});
}
$('#filters_cat_update').val(fl);
$('#filters_cat_update').trigger('change'); // Notify any JS components that the value changed
}
});
I detect when i remove an option like this:
//remove from edit options
$('#filters_cat_update').on('select2:unselecting', function (e) {
console.log(e.params.args.data.id);
});
My html looks like this:
<div class="col-lg-6 col-md-12 col-sm-12">
<select class="js-example-basic-multiple custom-select mr-sm-2" name="filters_update[]" multiple="multiple" id="filters_cat_update">
<?php
$sqlf = mysqli_query($conn, "SELECT * FROM filtre WHERE vizibil=1 ORDER BY pozitie");
while ($rf = mysqli_fetch_assoc($sqlf)) {?>
<option data-id="" value="<?=$rf['id'];?>,<?=$rf['nume'];?>"><?=$rf['nume'];?></option>
<?php } ?>
</select>
</div>
What i want to do is when i click one option for example "color" to go with ajax and delete from table mysql where i have filters saved for each category and remove it.
As you see in my option i have concatenaded id of filter and name of filter coming from another table where user can create filters.
Thank you in advance.
You can get data-id using $("#filters_cat_update option:selected") which will give you selected option using this you can use .attr and .val() to get required values .
Demo Code :
$('#filters_cat_update').select2();
$('#filters_cat_update').on('select2:unselecting', function(e) {
var code = $("#filters_cat_update option:selected").attr('data-id'); //getting id
var values = $("#filters_cat_update option:selected").val(); //get values
var datas = values.split(',');
var id = datas[0]; //id
var color = datas[1]; //color
console.log("id - " + id + " color - " + color + " data-id -" + code)
//ajax call
$.ajax({
url: "your url",
method: "POST",
data: {
id: id,
color: color,
code :code
}, //send data
success: function(data) {
console.log("done")
}
});
});
<link href="https://cdn.jsdelivr.net/npm/select2#4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2#4.1.0-beta.1/dist/js/select2.min.js"></script>
<select class="js-example-basic-multiple custom-select mr-sm-2" name="filters_update[]" multiple="multiple" id="filters_cat_update">
<option data-id="2" value="1,Color">Color</option>
<option data-id="7" value="2,Color2">Color2</option>
<option data-id="77" value="3,Color3">Color3</option>
</select>
i have a ScanField. In this field i'm scanning a barcode. After that i'm cutting the last four character of the bardcode-string and give it to the hiddenField. And from that hidden field when i click on the search button i pass it to the php site via ajax.
for example: i'm scanning a barcode: TWRG000000009102 in the hiddenfield shows 9102 and after that im passing the value to ajax.
Everthing works fine but i would like to do it automatically. When the hidden inputField was filled it should fire an event and pass the value to ajax.
my code:
<label for="myType">
<select id="myType" name="myType" size="5">
<option value="01">B&C</option>
<option value="02">James Nicholson</option>
<option value="F.O.L">Fruit Of The Loom</option>
<option value="TeeJays">TeeJays</option>
</select>
</label>
<br>
<label for="hiddenField">hiddenField:</label>
<input type="text" name="hiddenField" style="width:300px"><br>
<label for="myScan">Scanfield:</label>
<input type="text" name="myScan" style="width:300px" autofocus>
<button type="submit">Search</button>
<div id="result"></div>
<script>
$(document).ready(function(){
$('input[name=myScan]').on('keypress', function(e){
if(e.which == 13) {
var scanField = $(this).val();
console.log(scanField);
var lastFour = scanField.substr(scanField.length - 4);
$('input[name=hiddenField]').val(lastFour);
}
});
$('button').on('click', function(){
var postForm = {
'myType' : $('#myType').val(),
'myScan' : $('input[name=hiddenField]').val()
};
$.ajax({
url: "index.php",
type: "POST",
data: postForm,
dataType: "text",
success: function(data){
$('#result').html(data);
}
});
});
});
here the html example: https://jsfiddle.net/froouf9f/
Once you save value to hidden field, you can trigger button click event using:
$("button").trigger("click");
Try this:
$('input[name=myScan]').on('keypress', function(e){
if(e.which == 13) {
var scanField = $(this).val();
console.log(scanField);
var lastFour = scanField.substr(scanField.length - 4);
$('input[name=hiddenField]').val(lastFour);
$("button").trigger("click");
}
});
I have scenario form like this
if option selected it will come out a new field i named "hah" i call it using ajax script like this
$('#bahan').change(function() {
var id = $('#bahan').val()
var datana = 'id=' + id
var urlna = "<?=base_url()?>controller/food/getdetailstok";
$.ajax({
type: 'POST',
url: urlna,
data: datana,
success: function(data) {
$('#hah').html(data);
}
})
return false;
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<select class="form-control select2" name="bahan" id="bahan">
<option value="">-- Choose One --</option>
<option value="1"> 1 </option>
<option value="2"> 2 </option>
</select>
</div>
<div id="hah"></div>
How to reset "hah" if i click button save. hah in reset because the value field options again become null
$('#hah').empty() will do the trick.
.empty() will remove every element from the DOM. In your case every element in your #hah div.
$('#bahan').change(function() {
$('#hah').html("");
var id = $('#bahan').val()
var datana = 'id=' + id
var urlna = "<?=base_url()?>controller/food/getdetailstok";
$.ajax({
type: 'POST',
url: urlna,
data: datana,
success: function(data) {
$('#hah').html(data);
}
})
return false;
})
I am using the code below using jQuery and AJAX, to update my database.
Deletion works perfectly, but the edit function is not working. How is it possible to get the value from each input field?
<ol class="update">
<?php
$ss = mysql_query ("SELECT * FROM users_channels WHERE u_id='$uid'");
while ($chann = mysql_fetch_assoc($ss)) {
echo'
<li>
<input name="channelName" type="text" id="channelName" style="float:left; width:300px; border:1px solid #CCC;" value="'.$chann['channel_name'].'" />
<span id="rcolor">Delete</span>
<span id="gcolor">Save</span>
</li>';
}
?>
</ol>
Javascript code:
$(document).ready(function() {
$(function() {
$(".save_button").click(function() {
var id = $(this).attr("id");
var cvalue = $(this).attr("cvalue");
var dataStringe = 'id=' + id + '&cvalue=' + cvalue;
var parent = $(this).parent();
alert(cvalue);
$.ajax({
type: "POST",
url: "update_channel.php",
data: dataStringe,
cache: false,
beforeSend: function() {
parent.animate({'backgroundColor':'#fb6c6c'}, 300).animate({ opacity: 0.35 }, "slow");
},
success: function() {
//parent.slideUp('slow', function() {$(this).remove();});
}
});
return false;
});
});
});
Add this in your java script function
$(document).ready(function() {
$(function() {
$(".save_button").click(function() {
var inputVal=$("#channelName").val();
//rest of your code
return false;
});
});
});
you'll get the value of an input field(as per your question) in inputVal variable.
In order to get the values of all input fields,give them a common class name instead of giving style there like this
<input class="myInput" type="text" value="red" id="one"/>
<input class="myInput" type="text" value="France" id="two" />
and then in your javascript add this
var inputValues= {};
$(".myInput").each(function() {
inputValues[$(this).attr("id")] = $(this).val();
});
alert(inputValues.one); // "red"
you'll get the value of all input fields in inputValues variable
can u be more explicit? What do you need to get? Value of all input elements? if yes .. use $('input[name=example]').each(function(){})
this will get all values of all input with specified name
I guess what you're trying to do is submit the value of your input field as cvalue? To do this, the best approach would be:
$(".save_button").click(function() {
var id = $(this).attr("id");
var parent = $(this).parent();
var cvalue = parent.find('input').val(); // this gets the value of the <input> field
var dataStringe = 'id='+ id + '&cvalue='+ cvalue;
// the rest of your code...
});