onclick not working on button laravel 5.2 - javascript

I am working on Larvel framework. I am using login blade file of auth component. There is one button of register. whren i apply onclick on button to register page it is not redirect to register page.
<button class="btn btn-info sign-bttn" onclick="{{ url('/register') }}">Register Your Business</button>
Where i am doing wrong anyone help me

If you wanna redirect, should use a element of HTML
<a class="btn btn-info sign-bttn" href="{{ url('/register') }}">Register Your Business</a>
Btw, inside onclick you need to pass a javascript not a string ({{ url('/register') }} just pass a string)
Redriect in javascript use window.location.href
<button class="btn btn-info sign-bttn" onclick="window.location.href = '{{ url('/register') }}';">Register Your Business</button>

Related

"Could not parse reminder error" - django

When jsevent triggered , it forwards link parameter to data-url attribute of a button and when user click that button it redirects to django backend view but there is a problem with parsing line of JS
$('#btn_del').attr('data-url',{% url 'event_delete' + event.id + %});
is there any chance to combine that syntax ?
Thank you , have a nice days.
JS:
eventClick: function(event, jsEvent, view) {
$('#modalTitle').html(event.id);
$('#btn_del').attr('data-url',`{% url 'event_delete' ` + event.id + `%}`);
$('#calendarEditModal').modal();
console.log(event.id);
}
url.py
....
url(r'^event/(?P<pk>\d+)/delete/$', event_delete, name='event_delete'),
....
Html button that redirects to django
<button id="btn_del" type="button" class="btn btn-primary js-delete-events" data-url="">
<span class="glyphicon glyphicon-pencil"></span>
Edit
</button>
Result should be like this format event.pk is going to be our parameter numeric value , it's ok.
<button type="button" class="btn btn-primary js-delete-events" data-url="{% url 'event_delete' event.pk %}">
<span class="glyphicon glyphicon-plus"></span>
button
</button>
/event/1/update/ is the last result of seen from browser inspect. But I need to write inside jsEvent with django syntax to able to reach backend view which is {% url 'event_delete' event.pk %} something like that.
No, it cannot be done that way... Parsing of your templates (the {% url %} tag) is done on the server before even the browser receives anything. Browser doesn't know what template tags are you using.
On the other side, JavaScript is executed in the browser, so template language has also no knowledge about variables inside it.
Solution for that is to use a package like Django JS Reverse. The second one is to pass full URL to the JavaScript, just like it receives the ID of the item.

Laravel: using Javascript inside blade #if

I was wondering. It's not possible to put Javascript inside blade #if right? Is blade #if some form of php but using php code will be discouraged in view right? What should I do? Here's the logic of what I'm trying to achieve. Basically, what alternative ways are there to achieve what I'm trying to do? I'm trying to show button when the three conditions are met.
#if(isToday($one_parking_info->booking_data->m_bkl_end_date) && isBeforeElevenPM() && notEndInT($one_parking_info->booking_data->m_plots_address_city))
<button type="button" class="btn btn-primary btn-sm" onClick="cancelBooking('{{ $one_parking_info->booking_data->m_bkl_gov_id }}')">取消</button>
#endif
Blade is a form of php, so all work is done on the server. So you cannot call javascript functions inside #if expression. Instead you can run the functions in the controller and return a value to say if you should include the button or not or you can have a javascript to show or hide the button if you don't care if the users can find and show the button if they want.
Something like in your controller return view('your view')->with(['showButton' => $shouldShow) and in your view #if($shouldShow) and the rest of your code
Your js must be in public or a child, and would be something like this:
#if(isToday($one_parking_info->booking_data->m_bkl_end_date) && isBeforeElevenPM() && notEndInT($one_parking_info->booking_data->m_plots_address_city))
#include('script.js')
<button type="button" class="btn btn-primary btn-sm" onClick="cancelBooking('{{ $one_parking_info->booking_data->m_bkl_gov_id }}')">取消</button>
#endif
Another option could be <script src="{{ asset('js/scripts.js') }}"></script> instead of #include directive
i guess:
#if(isToday($one_parking_info->booking_data->m_bkl_end_date) && isBeforeElevenPM() && notEndInT($one_parking_info->booking_data->m_plots_address_city))
#include('script.js')
<button type="button" class="btn btn-primary btn-sm" data-id="{{ $one_parking_info->booking_data->m_bkl_gov_id }}">取消</button>
#endif
<script>
$('body').on('click','.btn-sm',function(){
var id = $(this).attr('data-id');
//
logic of cancelBooking
//
})
</script>

How to concatenate two params using href?

I have kendo grid where i am using anchor tag so using href i am trying to pass two params from client side but it always give syntax error, any help what is correct way to pass variables using href.
config.js
toolbar: [{template: '<a class="btn btn-default btn-sm pull-right"
href="app/challenge/rest/exportChallengeGridDataToExcel?key={{$rootScope.id}}
&challengeType={{$rootScope.challengeType}}">Export to Excel</a>'}]
You can do something like :
'<a class="btn btn-default btn-sm pull-right"
href="app/challenge/rest/exportChallengeGridDataToExcel?key={{$rootScope.id}}
&challengeType={{$rootScope.challengeType}}">Export to Excel</a>
Anyways I would encourage you to use $rootScope as less as possible, also to build your URL in your controller so your html looks more readable, but that's up to you

How to pass user id in javascript or in ajax to fetch record from database if using laravel framework?

I have fetched task title from database. Now I want if I click on task title it should show description of that particular task on the same page using toggle.
I have tried AJAX and JavaScript but found no result. Can anyone help by providing a small example related to this topic?
<button type="button" id="button" value="submit" class="btn btn-warning" onclick="javascript:fload($usermm->id);">{{ $usermm->TaskTitle }}</button>
This should work:
<button type="button" id="button" value="submit" class="btn btn-warning" onclick="javascript:fload({{{ $usermm->id }}});">{{{ $usermm->TaskTitle }}}</button>
Use {{{ $usermm->TaskTitle }}} (3 curly brackets instead of 2) to make sure you escape the data in the view.
UPDATE
I'm not going to write all code for you but I can give you a clue:
function fload(id)
{
[...]
var url='mytaskurl?id=' +id;
[...]
}
UPDATE #2
Here is an example of using Ajax with Laravel:
http://blog.igeek.info/2013/using-ajax-in-laravel/

check confirmation script and do link to route action

I have a question on laravel 5 and I think It's simple
I have "x" icon ; this icon is for delete item . there is a confirmation script which make the deletion of the item's div . but I want to implement the back-end action
the code now is fine for interface . it is :
<a class="btn btn-default btn-xs remove" href=" javascript: jQuery('.item-{{$i}}').remove()" data-confirm="Are you sure" data-confirm-button="yes" ><span class="glyphicon glyphicon-remove"></span></a>
and the link route I want to add is :
{!! link_to_route('item_delete','', $id) !!}
How can I combine the back-end to the interface ? I mean combine the script confirmation with route action?
I appreciate any help
You can do this by AJAX. Since you need $i generated from php loop and url generated from laravel, then we cannot put these variables in javascript.
HTML.
<a class="btn btn-default btn-xs remove" onclick="removeItem({{$i}},{!! link_to_route('item_delete','', $id) !!});" data-confirm="Are you sure" data-confirm-button="yes" ><span class="glyphicon glyphicon-remove"></span></a>
Javascript.
function removeItem(i,url){
$.post( url, { _token: '{{csrf_token()}}' }, function( data ) {
//check the data return from back-end and remove the x icon. assume return 0 is success.
if(data===0){
jQuery('.item-'+i).remove();
}
});
};
Please check syntax again, some maybe wrong :).

Categories