Reputation: 346
I am new to Laravel and am trying to find my way around.
I have created an php file which has several functions one generates a random number and one displays date and so on...
I have a view page where onclick of a radio button i generate a random number,so my question is where to include my php file in application and how can i call a specific function from view page so that only that part of the file executes.
My code goes here...
view.blade.php
<script type="text/javascript">
function CreateRandomNumber()
{
$('#wdv').onclick("<?php CreateRandomNumber(); ?>");
}
</script>
<div class="row form-wrapper">
<form class="form-horizontal" id="form" method="post" action="" autocomplete="off">
<!-- CSRF Token -->
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<div class="form-group {{ $errors->has('depreciation_type') ? ' has-error' : '' }}">
<label for="depreciation_type" class="col-md-3 control-label depreciationlabel">@lang('admin/assetdetails/form.depreciation')</label>
<div class="controls col-md-7">
{{ Form::radio('depreciation_type', 'wdv', Input::old('depreciation_type', $assetdetail->depreciation_type == 'wdv' ), array('id'=>'wdv', 'class'=>'wdvbutton','onclick' => 'random()')) }}
<label for="wdv" class="col-md-2 control-label wdvlabel">@lang('admin/assetdetails/form.wdv')</label>
{{ Form::radio('depreciation_type', 'slm', Session::hasOldInput() ? array_key_exists('depreciation_type', Input::old()) : ($assetdetail->exists ? $assetdetail->depreciation_type == 'slm' : true), array('id'=>'slm', 'class'=>'slmbutton')) }}
<label for="slm" class="col-md-2 control-label slmlabel">@lang('admin/assetdetails/form.slm')</label></br>
{{ $errors->first('depreciation_type', '<span class="alert-msg"><i class="icon-remove-sign"></i> :message</span>') }}
</div>
</div>
</form>
</div>
My php page as of now i have included the php page inside app folder and included it in start.php file i am not sure whether this is the right place to insert the files in my application.
randomnumber.php
<?php
function CreateRandomNumber()
{
//stuff to create random number here
}
function ShowDate()
{
//stuff to find and show date here
}
?>
I know calling a php function inside java script is a bad practice,is there any other approach to call a specific php function inside view page on a button click.If it is ajax how to include php function name inside ajax.
More over is it right to add files inside app folder and include it in start.php.
Thanks in advance....
Upvotes: 2
Views: 2586
Reputation: 124
You basically want to create a helper function. This post should guide you in the right direction. I would suggest going the library route which would be much cleaner and you have the advantage of it only being loaded when used. Laravel 4 helpers or basic functions
Upvotes: 1