【问题标题】:Call php function when click a button单击按钮时调用php函数
【发布时间】:2016-01-22 09:52:53
【问题描述】:

我有一个代码:

<?php
getPriceListHeader();

 function getPriceListDetail($PriceListCode)
{
    $resource = Mage::getSingleton('core/resource');
    $readConnection = $resource->getConnection('core_read');

    $query = "SELECT * FROM pricelisdetail where pricelist_code='".$PriceListCode."'";
    $results = $readConnection->fetchAll($query);
    echo "<table id='tbdata'>
                    <thead>
                        <tr>
                            <th>Price List Code</th>
                            <th>Price List Name</th>
                            <th>Effective From</th>
                            <th>Effective To</th>
                        </tr>
                    </thead>
        <tbody> ";
    foreach ($results as $row)
    {
        echo "<tr>";
        echo "<td> ".$row[entity_id];
        echo "<td> ".$row[sku];
        echo "<td> ".$row[sku];
        echo "<td> ".$row[sku];
    };
    echo "   </tbody>
    </table> ";
}


 function getPriceListHeader()
{
    $resource = Mage::getSingleton('core/resource');
    $readConnection = $resource->getConnection('core_read');

  $query = 'SELECT * FROM pricelistheader';
    $results = $readConnection->fetchAll($query);
    echo "
            <h2>Price List</h2>
            <div>
            <h3>Please select Price List</h3>
            <div>
            <select class='element select large' id='pricelist' name='element_2'>";
    foreach ($results as $row)
    {
            echo '<option value="' . $row[entity_id]. '">' . $row[sku] . '</option>';
    }
      echo "</select>
            </div>
            <input type='button' class='button' name='insert' value='Get Data' onclick='getPriceListDetail(pricelist.value)'/>

            ";
    getPriceListDetail('');
}
?>

我有一个下拉列表、一个按钮、一个表格 当我从下拉列表中选择一个值时,然后单击按钮,表格将再次填充数据。有两种方法,getPriceListHeader(): 加载页面时加载数据头,getPriceListDetail :单击按钮时加载数据详细信息。我尝试将事件 getPriceListDetail(value) 放到按钮上,但是当我单击时,什么也没有发生
请帮助我如何做到这一点。

【问题讨论】:

  • if (isset(yourSubmitButton)) { function call here. }。您不能将 PHP 函数附加到内联 JavaScript 事件。如果你真的想的话,你也可以使用AJAXPHP 运行服务器端,JavaScript 运行客户端。

标签: php html magento events button


【解决方案1】:

是的,你可以像这样通过ajax请求调用php到服务器(非常简单):

注意以下代码使用jQuery

jQuery.ajax({
type: "POST",
url: 'my_php_function.php',
dataType: 'name_the_data_type',
success: function (data) {
         // here you will get the response your function 
      }
});

和 my_php_function.php 这样的:

<?php

  // here is your php code or function

?>

来源How can I call PHP functions by JavaScript?

【讨论】:

    【解决方案2】:

    您不能将 PHP 函数附加到 HTML。 PHP是服务器端语言,所以在用户浏览器中显示后就不能再引用PHP了,除非你重新加载页面。

    我认为您可以选择 3 个选项:

    1. 更改选择页面并使用 GET 准备新数据后重新加载页面。
    2. 将所有数据发送到用户浏览器,然后仅显示与所选选项相关的部分数据。
    3. 使用 AJAX 并在后台向服务器请求新数据。

    【讨论】:

    • 你能解释一下关于选项2的更多信息吗,我想过但我不知道如何编写代码。谢谢
    • 好吧,如果您不愿意编写自己的 javascript 代码来涵盖该主题,请尝试考虑使用例如数据表 (datatables.net/examples/data_sources/js_array.html)。它们非常简单,非常适合用于显示可排序的表数据。
    【解决方案3】:

    最后,我找到了方法。它对我有用。现在,我不明白代码,我正在努力阅读并清楚地理解它。 谢谢大家的cmets。

        $to="";
        $from="";
        $show_order_statuses = 0;
        $orserstatus = "";
    
        $result_order = 0;
    
    //var_dump($results);
    
    //return;
        if(!empty($_REQUEST['filter_type']))
        {
    
            $orders_row = array();
            $filter_type = $_REQUEST['filter_type'];
    
            $resource = Mage::getSingleton('core/resource');
            $readConnection = $resource->getConnection('core_read');
            $query = " SELECT *  FROM pricelistitem where pricelist_code='".$filter_type."' ";
    
    
    // $query = 'SELECT * FROM pricelistheader1';
            $results = $readConnection->fetchAll($query);
            foreach ($results as $rowid)
            {
                $result_order=10;
              $orders_row[]=array($rowid['pricelist_code'],$rowid['product_code'],number_format( $rowid['unitprice'],2),$rowid['UOM']);
              // $orders_row[]=array(1,1,1,1,1);
            }
    
        }
    
    ?>
    
    <div id="anchor-content" class="middle">
        <div id="page:main-container">
            <div class="content-header">
                <table cellspacing="0">
                    <tbody>
                    <tr>
                        <td style="width:50%;"><h3 class="icon-head head-report-sales-sales"><?php echo $this->__("Price List");?></h3></td>
                        <td class="form-buttons"><button style="" onclick="filterFormSubmit.submit()" class="scalable " type="button" id="id_<?php echo Mage::getSingleton('core/session')->getFormKey() ?>"><span>Show Report</span></button></td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div>
                <div class="entry-edit">
                    <form method="get" action="<?php echo Mage::helper('core/url')->getCurrentUrl();?>" id="filter_form">
                        <?php /*?><input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" /><?php */?>
                        <div class="entry-edit-head">
                            <h4 class="icon-head head-edit-form fieldset-legend">Filter</h4>
                            <div class="form-buttons"></div>
                        </div>
                        <div id="sales_report_base_fieldset" class="fieldset">
                            <div class="hor-scroll">
                                <table cellspacing="0" class="form-list">
                                    <tbody>
                                    <tr>
                                        <td class="label"><label for="sales_report_filter_type">Filter By <span class="required">*</span></label></td>
                                        <td class="value">
                                            <select class="required-entry select" name="filter_type" id="sales_report_filter_type" onchange="myFunction();">
                                                <?php
                                                $resource = Mage::getSingleton('core/resource');
                                                $readConnection = $resource->getConnection('core_read');
                                                $query = " SELECT * FROM pricelistheader ";
    
                                                $results = $readConnection->fetchAll($query);
                                                $so=1;
                                                foreach ($results as $row)
                                                {
                                                    $selected='';
                                                    if ($filter_type==$row[pricelist_code])
                                                        $selected= ' selected=selected';
                                                    else
                                                        $selected='';
    
                                                    echo '<option value="' . $row[pricelist_code]. '" '.$selected.' >' . $row[pricelist_name].'  '. $row[pricelist_code] . '</option>';
    
                                                }
                                                ?>
                                                                    </select>
                                    </tr>
                                    <tr>
                                        <td class="label"><label for="effect_from">Effect From </label></td>
                                        <td class="value">
                                            <?php
                                            foreach ($results as $row)
                                            {
    
                                                if ($filter_type==$row[pricelist_code])
                                                  echo  $row[pricelist_fromdate];
    
                                            }
                                            ?>
                                        </td>
                                    </tr>
    
                                    <tr>
                                        <td class="label"><label for="effect_from">Effect To </label></td>
                                        <td class="value">
                                            <?php
                                            foreach ($results as $row)
                                            {
    
                                                if ($filter_type==$row[pricelist_code])
                                                    echo  $row[pricelist_todate];
    
                                            }
                                            ?>
                                        </td>
                                    </tr>
                                    </tbody>
                                    <script>
                                        function myFunction() {
                                            // document.getElementById("tbdata").deleteRow(1);
                                            var rowCount = tbdata.rows.length;
                                            for (var i = rowCount - 1; i > 0; i--) {
                                                tbdata.deleteRow(i);
                                            }
                                            srt.value=pricelist.options[pricelist.selectedIndex].value;
                                            ";
                                            //$('#tbdata').empty();
                                        }
                                    </script>
                                </table>
                            </div>
                        </div>
                    </form>
                </div>
                <script type="text/javascript">
                    //<![CDATA[
                    var filterFormSubmit  = new varienForm('filter_form');
    
                    //]]>
                </script>
                <script type="text/javascript"> new FormElementDependenceController({"sales_report_order_statuses":{"sales_report_show_order_statuses":"1"}}); </script>
                <style type="text/css">
                    .no-display{display:none;}
                </style>
            </div>
    
    
    
            <div>
                <?php if($result_order>0){?>
                    <table cellspacing="0" class="actions">
                        <tbody>
                        <tr>
                            <td class="pager">&nbsp;</td>
                            <td class="export a-right">
                                <form method="post" action="<?php echo $this->getUrl('*/*/exportCsv')?>" id="csv_form_customer">
                                    <input name="form_key_customer" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />
    
    
                                </form>
                                <script type="text/javascript">
                                    //<![CDATA[
                                    var csvFormSubmitcustomer  = new varienForm('csv_form_customer');
                                    //]]>
                                </script>
                            </td>
                            <td class="filter-actions a-right">
                                <img class="v-middle" alt="" src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);?>skin/adminhtml/default/default/images/icon_export.gif">&nbsp; Export to:
                                <select style="width:8em;" id="sales_order_grid_export_customer" name="sales_order_grid_export_customer">
                                    <option value="<?php echo $this->getUrl('*/*/exportCsv')?>">CSV</option>
                                </select>
                                <button onclick="csvFormSubmitcustomer.submit()" class="scalable task" type="button"><span>Export</span></button>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                <?php } ?>
                <div id="id_customer<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" class="print_customer<?php echo Mage::getSingleton('core/session')->getFormKey() ?>">
                    <div class="grid">
                        <div class="hor-scroll">
                            <table cellspacing="0" id="id_customer<?php echo Mage::getSingleton('core/session')->getFormKey() ?>_table" class="data">
                                <colgroup>
                                    <col>
                                    <col>
                                </colgroup>
                                <thead>
                                <tr class="headings">
                                    <th class=" no-link"><span class="nobr">Price List Code</span></th>
                                    <th class=" no-link"><span class="nobr">Cust Code</span></th>
                                    <th class=" no-link"><span class="nobr">Cust Name</span></th>
                                </tr>
                                </thead>
                                <tbody id="">
                                <?php
    
                                $resource = Mage::getSingleton('core/resource');
                                $readConnection = $resource->getConnection('core_read');
                                $query = " SELECT * FROM " . $resource->getTableName('catalog/product');;
                                $customercount=0;
                                $customerresults = $readConnection->fetchAll($query);
    
                                $customerresults = Mage::getModel('customer/customer')
                                    ->getCollection()
                                    ->addAttributeToSelect('*')
                                    ->addAttributeToFilter('erp_pricelistcode_1', $filter_type)
                                  //  ->addFieldToSelect (array('created_at','customer_id','increment_id','updated_at','status','entity_id','state'))
                                ;
    
                                   // ->addAttributeToFilter('erp_pricelistcode_1','00')->load();
                                $so=1;
                                foreach ($customerresults as $row) {
    
                                    $customercount++;
                                    }
                               // var_dump(@$customerresults);
                                if($customercount>0){
                                    foreach($customerresults as $singlerows){
                                        $cot=0;
                                        {
                                            echo "<tr>";
                                            {
                                                $cot++;
                                                ?>
                                                 <td>
                                                     <?php
                                                    echo $singlerows->getData('erp_pricelistcode_1');
                                                     ?>
                                                </td>
                                                <td>
                                                    <?php
                                                    echo $singlerows->getFirstname();
                                                    ?>
                                                </td>
                                                <td>
                                                    <?php
                                                    echo $singlerows->getName();
                                                    ?>
                                                </td>
                                                <?php
                                            }
                                            echo "</tr>";
                                        }
                                    }
                                }else{
                                    ?>
                                    <tr class="even">
                                        <td colspan="13" class="empty-text a-center">No records found.</td>
                                    </tr>
                                <?php } ?>
                                </tbody>
    
                            </table>
    
        </div>
    </div>
    
            <div>
                <?php if($result_order>0){?>
                    <table cellspacing="0" class="actions">
                        <tbody>
                        <tr>
                            <td class="pager">&nbsp;</td>
                            <td class="export a-right">
                                <form method="post" action="<?php echo $this->getUrl('*/*/exportCsv')?>" id="csv_form">
                                    <input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />
    
    
                                </form>
                                <script type="text/javascript">
                                    //<![CDATA[
                                    var csvFormSubmit  = new varienForm('csv_form');
                                    //]]>
                                </script>
                            </td>
                            <td class="filter-actions a-right">
                                <img class="v-middle" alt="" src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);?>skin/adminhtml/default/default/images/icon_export.gif">&nbsp; Export to:
                                <select style="width:8em;" id="sales_order_grid_export" name="sales_order_grid_export">
                                    <option value="<?php echo $this->getUrl('*/*/exportCsv')?>">CSV</option>
                                </select>
                                <button onclick="csvFormSubmit.submit()" class="scalable task" type="button"><span>Export</span></button>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                <?php } ?>
                <div id="id_<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" class="print_<?php echo Mage::getSingleton('core/session')->getFormKey() ?>">
                    <div class="grid">
                        <div class="hor-scroll">
                            <table cellspacing="0" id="id_<?php echo Mage::getSingleton('core/session')->getFormKey() ?>_table" class="data">
                                <colgroup>
                                    <col>
                                    <col>
                                    <col>
                                    <col>
    
                                </colgroup>
                                <thead>
                                <tr class="headings">
                                    <th class=" no-link"><span class="nobr">Price List Code</span></th>
                                    <th class=" no-link"><span class="nobr">Product Code</span></th>
                                    <th class=" no-link"><span class="nobr">Unit Price</span></th>
                                    <th class=" no-link"><span class="nobr">UOM</span></th>
    
                                </tr>
                                </thead>
                                <tbody id="">
                                <?php
                                $cot=0;
                                if(count($orders_row)>0){
                                    foreach($orders_row as $singlerows){
                                        $cot=0;
                                        if(!empty($singlerows)){
                                            echo "<tr>";
                                            foreach($singlerows as $value){
                                                $cot++;
                                                ?>
                                                <td>
                                                    <?php
                                                   if ($cot==3 )
                                                     echo "<div style='float:right;width:30%;'>";
                                                        echo $value;
                                                echo "</div>";
                                                    ?>
                                                </td>
                                                <?php
                                            }
                                            echo "</tr>";
                                        }
                                    }
                                }else{
                                    ?>
                                    <tr class="even">
                                        <td colspan="13" class="empty-text a-center">No records found.</td>
                                    </tr>
                                <?php } ?>
                                </tbody>
    
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 2014-11-01
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多