将组合框的选定值作为参数传递给使用 AJAX 或 JavaScript 的特定 PHP 函数

pass the selected value of combo box as a parameter to specific php function using ajax or javascript

本文关键字:AJAX JavaScript 函数 PHP 参数传递 组合      更新时间:2023-09-26

嗨,首先,我将解释我正在尝试做什么,以便每个人都清楚地了解我正在尝试做什么。

正在使用laravel框架开发一个应用程序,其中我有一个编辑页面和一个php页面,即edit.blade.php和计算.php

编辑刀片.php

<script type="text/javascript">
    function CompanyDepreciationCalculation()
    {
        debugger;
        var endFinancialYear = $('#years').val();
        $('.display-table').html("<?php DepreciationCalculation2014(endFinancialYear); ?>");
    }
</script>
<div class="page-header">
    <h3>@lang('general.calculate_2014')</h3>
    <div class="pull-right">
    </div>
</div>
<div class="row form-wrapper">
    <form class="form-horizontal" method="post" action="calculation.php" autocomplete="off">
        <div class="form-group {{ $errors->has('years') ? ' has-error' : '' }}">
            <label for="shift" class="col-md-3 control-label calculate-label">@lang('admin/records/form.Calculate_dep')</label>
            <div class="controls col-md-7">
                <select name="years" class="controls assettext select2" id="years"  >
                    <option value=""> -- Select Year -- </option>
                    <?php
                    for ($i = 2014; $i <= 2101; $i++) {
                        echo "<option value=" . $i . ">" . $i . "</option>";
                    }
                    ?> 
                </select>
                <!--<a  class="btn btn-flat gray pull-right generatebtn">Next</a>-->
                <button type="button" class="btn btn-flat gray pull-right generatebtn" id="next" value="next" onclick="CompanyDepreciationCalculation()"><i class="icon-download-alt"></i>Generate  Depreciation Report</button>
            </div>
            {{ $errors->first('shift', '<span class="alert-msg"><i class="icon-remove-sign"></i> :message</span>') }}
        </div>
        <div  class="display-table" style="margin:70px 0px 0px -14px;">
        </div>
    </form>
</div>

在这里,我有一个带有 id 年份的下拉列表,我尝试从下拉列表中获取所选值,并将该值作为参数传递给 php 函数 DemonationComputing2014,并在单击按钮的事件中在表中显示计算值。计算和表格内容写在函数 DeliationComputing2014 中。

在我的计算中.php页面我有很多函数,其中一个函数名称是 DemonationComputing2014,我在编辑页面中将其指定为 php 函数,并且我已经将 php 文件包含在 edid 页面的表单标签中。首先,我知道这是否是像这样调用 php 函数的好方法。

计算.php

function DepreciationCalculation2014($endFinancialYear) 
{ 
    //calculation stuff goes here and i display the calculated values in a table
}

问题是我无法在这里获取变量,它抛出以下错误

Use of undefined constant selectedYear - assumed 'selectedYear' (View: C:'xampp'htdocs'AMS'app'views'backend'calculate2014'index.blade.php)

我尝试使用ajax但没有运气

$(document).ready(function() {   
    $('#years').change(function() {debugger;
        $.POST('depreciationcalculation.php', $(this).serialize(), function(data) {
            alert(data);
        });
    });
});

和在 PHP 页面中

function DepreciationCalculation2014($endFinancialYear = 2014) 
{ 
    $_POST['years']
}

请帮助我解决这个问题,我知道我做错了一些事情,但不太清楚。

这部分肯定很糟糕:

<script type="text/javascript">
function CompanyDepreciationCalculation() {   
    debugger;
    var endFinancialYear = $('#years').val();
    $('.display-table').html("<?php DepreciationCalculation2014(endFinancialYear); ?>");
  } 
</script>

你不能把这样的javascript变量传递给php

$('.display-table').html("<?php DepreciationCalculation2014(endFinancialYear); ?>");

请改用 Ajax。