Bootstrap dropdown with Ajax - ajax

I am using Bootstrap 3 and I am trying to obtain an action following the click on a dropdown.
My current issue is that I got the entire page reloaded instead of updating the content of a specific .
This is my current code:
<div class="dropdown pull-right">
<button type="button" class="btn btn-default" data-toggle="dropdown">Mybutton!</button>
<ul class="dropdown-menu test" role="menu">
<li role="presentation" class="dropdown-header">Select your action</li>
<li>Action 1</li>
<li>Action 2</li>
<li>Action 3</li>
</ul>
</div>
Then I have a div that where I would like to show the result:
<div id="result">
<?php
$test = $_POST['sentData'];
echo $test;
echo 'something else to write here';
?>
</div>
This is the ajax call:
$(document).ready(function() {
$('.test li a').on('click', function () {
var sentData = $(this).html();
$.ajax({
type: "POST",
url: "design7.php",
data: "sentData=" + sentData,
dataType: "html",
success: function(data)
{
$('#result').html(data);
}
});
});
});
I have tried to find a solution but my skills are limited. I will appreciate any help.

Related

CodeIgniter Ajax does not populate content in modal form

The modal form called does not load the content using Ajax. So I have a modal dialog that is supposed to come up with a dropdown and Cancel & OK buttons. The dropdown is loaded from database, however it comes up empty.
I've tried to look this up but examples are either close or very far but none offered something similar.
I've also tried different approaches but still I end up with the same issue.
View:
foreach($aRecords as $record)
...
<a href="#"
class="btn btn-primary btn-xs change_parent_class"
data-toggle="modal"
data-target="#confirmChangeParent"
id ="<?php echo $record->id; ?>"
><i class="fa fa-arrow-up"></i> Change Parent</a>
The modal
<div class="modal fade" id="confirmChangeParent" role="dialog" aria-labelledby="confirmChangeParent" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Change Parent</h4>
</div>
<div class="modal-body">
<p>Please select one of the following options</p>
<div id="areaChangeParent" name="areaChangeParent"></div>
</div>
<div class="modal-footer">
<a href="#" class="btn btn-info btn-xs default" data-dismiss="modal">
<i class="fa fa-reply"></i> Cancel
</a>
<a href="#" class="btn btn-success btn-xs default" id ="confirmSubmitDelete">
<i class="fa fa-arrow-up"></i> Move
</a>
</div>
</div>
</div>
</div>
JavaScript
$(document).ready(function(){
//function change_parent(id)
$(".change_parent_class").click(function()
{
id = $(this).attr('id');
alert("p-aci:" + id);
e.preventDefault();
$.ajax({
url: "<?php echo base_url() ?>project/project_issue/get_parents/",
method: "POST",
data: {mid:id},
success: function(data)
{
$("#areaChangeParent").html(data);
}
});
});
});
Controller function to fetch data is in a dummy state just for testing:
function get_parents()
{
echo "bla";
return "bla bla";
}
No matter what the prompt is displayed but empty.
You are using e.preventDefault(); without function(e)

Ajax call route laravel not found

i want to make delete confirmation laravel with Modal,
i call route laravel in ajax, but why the route is not found .
this is my route in laravel
Route::delete('delete-product/{productId}', 'StoreController#hapus')->name('product.delete');
this is my ajax delete..
$(document).on('click', '.delete-modal', function() {
$('.modal-title').text('Delete');
$('#id_delete').val($(this).data('productId'));
$('#deleteModal').modal('show');
productId = $('#id_delete').val();
});
$('.modal-footer').on('click', '.hapus', function() {
$.ajax({
type: 'DELETE',
url: 'delete-product/' + productId,
data: {
'_token': $('input[name=_token]').val(),
},
when i click icon trash,, modal is show, but in modal when i click delete button then inspect element in browser my route is not found
this is inspect element
Request URL: http://localhost:8000/delete-product/
Request Method: DELETE
Status Code: 404 Not Found
Remote Address: 127.0.0.1:8000
Referrer Policy: no-referrer-when-downgrade
why the URL just delete/product .... not include ID ,,
even though my url in ajax called like this
......
url: 'delete-product/' + productId,
.....
this is my blade code
#if(sizeof($resp->content) > 0)
<ul class="the-product">
#foreach($resp->content as $item)
<li>
#if(isset($store) && $store->storeId == $item->store->storeId)
<i class="icofont-ui-love"></i>
#else
<a class="btn btn-danger delete-modal"><i class="fa fa-trash" data-id="{{$item->productId}}"></i></a>
<a class="btn btn-danger" onclick="return myFunction();" href="{{url('edit.product', $item->productId)}}"><i class="icofont-ui-edit"></i></a>
{{-- <i class="icofont-ui-edit"></i> --}}
{{-- <i class="icofont-ui-delete"></i> --}}
#endif
{{-- onclick="ajaxContent(event, this);" --}}
<a href="productDetail/{{ $item->productId }}" class="the-item #if($item->discount > 0)on-promo #endif">
#if($item->discount > 0)
<div class="prod-rib">{{ $item->discount }}% Off</div>
#endif
<img data-src="#if($item->imageId != null){{ config('constant.showImage').$item->imageId }}#else{{ asset('images/no-image-available.png') }}#endif"
src="#if($item->imageId != null){{ config('constant.showImage').$item->imageId }}#else{{ asset('images/no-image-available.png') }}#endif"
alt="{{ $item->productNm }}">
<div class="prod-name">{{ $item->productNm }}</div>
<div class="prod-rev">
#for($i=0; $i<5;$i++)
#if($i<$item->reviewRate)
<img src="{{asset('img/kopi-on.svg')}}" />
#else
<img src="{{asset('img/kopi.svg')}}" />
#endif
#endfor
<span>{{ $item->reviewCount }} ulasan</span>
</div>
<div class="prod-price">
#if($item->discount > 0)
<span>Rp. {{ number_format($item->price,0,',','.') }}</span> Rp. <i>{{ APIHelper::discountPrice($item->discount, $item->price) }}</i>
#else
Rp. <i>{{ APIHelper::discountPrice($item->discount, $item->price) }}</i>
#endif
</div>
</a>
Add to cart <i class="icofont-cart"></i>
{{-- //onclick="ajaxContent(event, this);" --}}
<a href="{{url('/store-detail/'.$item->store->storeId)}}" class="the-store">
<img src="#if($item->store->storePic != null){{ config('constant.showImage').$item->store->storePic }}#else{{ asset('images/no-image-available.png') }}#endif" />
{{ $item->store->storeNm }}
<span>{{ $item->store->percentFeedback }}% ({{ $item->store->totalFeedback }}
feedback)</span>
</a>
</li>
#endforeach
</ul>
<ul class="pagination">
<li class="disabled">
<span><i class="icofont-rounded-double-left"></i></span>
</li><li class="disabled">
<span><i class="icofont-rounded-left"></i></span>
</li>
{{-- active --}}
#for ($i = 0; $i < $resp->totalPages && $i < 5; $i++)
<li class="" onclick="ajaxContent(event, this,null,{'key':'page','value':{{$i+1}} })">
<span>{{$i + 1}}</span>
</li>
#endfor
<li>
<i class="icofont-rounded-right"></i>
</li>
<li>
<i class="icofont-rounded-double-right"></i>
</li>
</ul>
#else
<div class="container clearfix">
<div class="style-msg errormsg mt-5">
<div class="sb-msg"><i class="icon-remove"></i><strong>Maaf</strong>, pencarian anda tidak cocok dengan
produk apapun. Silahkan coba lagi
</div>
</div>
</div>
#endif
<!-- Modal form to delete a form -->
<div id="deleteModal" class="modal fade" role="dialog">
<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"></h4>
</div>
<div class="modal-body">
<h3 class="text-center">Are you sure you want to delete the following post?</h3>
<br />
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="control-label col-sm-2" for="id">ID:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="id_delete" disabled>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="title">Title:</label>
<div class="col-sm-10">
<input type="name" class="form-control" id="title_delete" disabled>
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-danger hapus" data-dismiss="modal">
<span id="" class='glyphicon glyphicon-trash'></span> Delete
</button>
<button type="button" class="btn btn-warning" data-dismiss="modal">
<span class='glyphicon glyphicon-remove'></span> Close
</button>
</div>
</div>
</div>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).on('click', '.delete-modal', function() {
$('.modal-title').text('Delete');
$('#id_delete').val($(this).data('productId'));
$('#deleteModal').modal('show');
productId = $('#id_delete').val();
});
$('.modal-footer').on('click', '.hapus', function() {
$.ajax({
type: 'DELETE',
url: 'delete-product/{productId}',
data: {
'_token': $('input[name=_token]').val(),
},
success: function(data) {
toastr.success('Successfully deleted Post!', 'Success Alert', {timeOut: 5000});
$('.item' + data['id']).remove();
$('.col1').each(function (index) {
$(this).html(index+1);
});
}
});
});
</script>
how to write URL in ajax ?
plis HELP..
In your case, in the following code :
$(document).on('click', '.delete-modal', function() {
$('.modal-title').text('Delete');
$('#id_delete').val($(this).data('productId'));
$('#deleteModal').modal('show');
productId = $('#id_delete').val();
});
ProductId is a local variable having scope function scope. You can not refer it outside unless you declare it globally at the top.
When you try to refer it directly later, it will not be having any value(undefined).
Secondly, You need to change
url: 'delete-product/{productId}'
with
url: '/delete-product/' + productId,
So below is the code :
jQuery(document).ready(function($){
$(document).on('click', '.delete-modal', function() {
$('.modal-title').text('Delete');
$('#id_delete').val($(this).data('productId'));
$('#deleteModal').modal('show');
});
$('.modal-footer').on('click', '.hapus', function() {
var productId = $('#id_delete').val();
$.ajax({
type: 'DELETE',
url: '/delete-product/' + productId,
data: {
'_token': $('input[name=_token]').val(),
},
success: function(data) {
toastr.success('Successfully deleted Post!', 'Success Alert', {timeOut: 5000});
$('.item' + data['id']).remove();
$('.col1').each(function (index) {
$(this).html(index+1);
});
}
});
});
});

Modal loading dynamic content not working

I am developing an app which requires options like view,So for viewing details I am using bootstrap modal to show the dynamic details,but the modal is not firing up.
My button
<a href="#" class="btn btn-info btn-sm view" data-id="{{$value->id}}" data-title="{{$value->title}}" data-status="{{$value->status}}">
<i class="fa fa-eye"> Preview</i>
</a>
My modal
<div class="modal fade" id="viewM" tabindex="-1" role="dialog">
<div class="modal-dialog modal-dialog-centered" style="width:60%;">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Preview</h4>
</div>
<div class="modal-body" id="preview" style="padding: 40px;">
</div>
<div class="modal-footer">
<button class="btn btn-danger" type="button" data-dismiss="modal">
<span class="fa fa-times-circle"></span> Close
</button>
</div>
</div>
</div>
</div>
My jquery
$('a.view').click(function()
{
var id = $(this).attr('data-id');
var url = 'receiveReprt';
$.get(url + '/' + id, function (data)
{
console.log(data);
$("#preview").html(data.content);
$('#viewM').modal({show:true});
});
});
Url function
public function receiveReprt($id)
{
$reprt = shareReport::select('content')->where('id','=',$id)->first();
return $reprt;
}
Are you getting any errors in your browser log? My guess at this point is that you are dealing with a javascript error, possibly due to data.content (e.g. if it's not object), which kills the script execution.
If this is the case, and data is not an object as I suspect, you can convert to one (assuming it's JSON of course):
$.get(url + '/' + id, function (data)
{
console.log(data);
$("#preview").html(data.content);
$('#viewM').modal({show:true});
}, "json");
Or
$.ajax({
url: url + '/' + id,
method: "get",
success: function (data) {
var json = $.parseJSON(data);
$("#preview").html(json.content);
$('#viewM').modal({show:true});
}
});

Bootstrap 4 navbar with Angular - how to activate item

I am putting together B4 navbar with Angular router states. Here is my code:
HTML:
<ul class="nav" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" ui-sref="state1" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" ui-sref="state2" href="#">Link</a>
</li>
</ul>
<div ui-view></div>
Controller:
var myApp = angular.module("myApp",[ "ui.router", "AppCtrls"]);
myApp.config(function ($stateProvider, $urlRouterProvider){
$stateProvider.state("state1", {
url: "#",
template: "<p>State 1</p>",
controller: "Ctrl1"
}).state("state2", {
url: "#",
template: "<p>State 2</p>",
controller: "Ctrl2"
});
});
var ctrls = angular.module("AppCtrls", []);
ctrls.controller( "Ctrl1", function($scope) {
console.log("Ctrl1 loaded.");
});
ctrls.controller( "Ctrl2", function($scope) {
console.log("Ctrl2 loaded.");
});
What I want to achieve is to have a default active item with its content being shown. I am setting up the first link to active but it doesn't do anything.
Here is my
Fiddle
try this code for HTML :
<ul class="nav" id="myTab" role="tablist">
<li class="nav-item" ui-sref-active="active">
<a class="nav-link" ui-sref="name1">Active</a>
</li>
<li class="nav-item" ui-sref-active="active">
<a class="nav-link" ui-sref="name2">Link</a>
</li>
</ul>
<div ui-view></div>
And this Angular :
var myApp = angular.module("myApp",[ "ui.router"]);
myApp.config(function($stateProvider, $urlRouterProvide) {
$urlRouterProvider.otherwise("/");
$stateProvider
.state('name1',{
url: "/name1",
conontroller:'ctrl1',
templateUrl: "name1.html"
})
.state('name2',{
url: "/name2",
conontroller:'ctrl2',
templateUrl: "name2.html"
})
});
myApp.controller( "ctrl1", function($scope) {
console.log("Ctrl1 loaded.");
});
myApp.controller( "ctrl2", function($scope) {
console.log("Ctrl2 loaded.");
});

Ajax not triggering for Partial view

I am adding Partial view in to my main view , the partial view code snippet is:
<li class="item-#j deeper parent active">
<a class="" href="">
<span data-toggle="collapse" data-parent="#menu-group-1" href="#sub-item-#j" class="sign"><i class="icon-plus icon-white"></i></span>
<span class="lbl">Brands</span>
</a>
<ul class="children nav-child unstyled small collapse" id="sub-item-#j">
#foreach (var brand in Model.BrandList)
{
<li class="item-#j deeper parent active">
<div class="checkbox">
<label>
<input type="checkbox" value="#brand._id">
#*<a class="" href="#Url.Action("FilterData", "ShopWindow", new {shopKey=house._id,houseKey=house._id,houseName = house.House})">*#
<span class="lbl">#brand.Brand</span>
#*</a>*#
</label>
</div>
</li>
j++;
}
</ul>
</li>
in my main view I have a script to get all the check boxes that user have selected, the script snippet :
<script type="text/javascript">
var selectedBrand=[];
$("input[type='checkbox']").change(function () {
$('#brand input:checked').each(function () {
selectedBrand.push($(this).attr('value'));
alert("Test");
})
sendAjaxRequest();
});
When I select any check box from the partial view the script's change function is not getting triggered. This change function triggers if I check any checkbox in the main page but not for checkboxes belongs to partial view .
Any idea?
Thanks
I found the solution :
changed
$("input[type='checkbox']").change(function () {
to
$(document).on('change', '.checkbox', function() {

Resources