[英]Where to include the php file in laravel and how to call a specific function in the php file using javascript
我是Laravel的新手,正在尝试寻找解决方法。
我创建了一个php文件,该文件具有多个功能,一个功能生成一个随机数,一个显示日期,等等。
我有一个视图页面,单选按钮的onclick会生成一个随机数,所以我的问题是在应用程序中包含我的php文件,以及如何从视图页面调用特定函数,以便仅执行文件的那一部分。
我的代码在这里...
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>
截至目前,我的php页面已将php页面包含在app文件夹中,并将其包含在start.php文件中,我不确定这是否是在我的应用程序中插入文件的正确位置。
randomnumber.php
<?php
function CreateRandomNumber()
{
//stuff to create random number here
}
function ShowDate()
{
//stuff to find and show date here
}
?>
我知道在Java脚本中调用php函数是一种不好的做法,是否有其他方法可以在单击按钮时在视图页面内调用特定的php函数。如果是ajax,如何在ajax中包含php函数名称。
此外,在app文件夹中添加文件并将其包含在start.php中是正确的。
提前致谢....
您基本上想创建一个辅助函数。 这篇文章应该指导您正确的方向。 我建议您走图书馆路线,这样会更清洁,您的优势是仅在使用时才加载它。 Laravel 4助手或基本功能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.