簡體   English   中英

哪里在laravel中包括php文件,以及如何使用JavaScript在php文件中調用特定函數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM