单击按钮时调用php函数

时间:2016-01-22 09:52:53

标签: php html magento events button

我有一个代码:

<?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('');
}
?>

我有一个下拉列表,一个按钮,一个表格 当我从下拉列表中选择一个值时,我点击按钮,表格将再次填充数据。加载页面时有2个方法,getPriceListHeader():加载数据头,getPriceListDetail:单击按钮时加载数据详细信息。我尝试将事件getPriceListDetail(value)设置为按钮,但是当我点击时,没有任何事情发生 请帮我解决这个问题。

enter image description here

3 个答案:

答案 0 :(得分: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?

答案 1 :(得分:0)

您无法将PHP函数附加到HTML。 PHP是服务器端语言,因此在用户浏览器中显示后,除非重新加载页面,否则无法再次引用PHP。

我认为你可以做3个选项:

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

答案 2 :(得分:0)

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

    $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>