简体   繁体   中英

Call php function when click a button

I have a code :

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

I have a dropdown list, a button, a table When I select a value from dropdown list, then I click button , table will be filled data again. There are 2 method, getPriceListHeader(): load data header when load the page, getPriceListDetail : load data detail when click a button. I try to put event getPriceListDetail(value) to button, but when I click, nothing happens
Please help me how to do this.

在此处输入图片说明

Yes, you can call php via ajax request to server like this (very simple):

Note that the following code uses 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 
      }
});

and my_php_function.php like this:

<?php

  // here is your php code or function

?>

from the source How can I call PHP functions by JavaScript?

You can't attach PHP function to HTML. PHP is server-side language, so after it's displayed in users browser you can't refer to PHP again, unless you reload page.

There are 3 options I think you can do:

  1. Reload page after changing select page and using GET prepare new data.
  2. Send all data to user browser and than show only part of it related to selected option.
  3. Use AJAX and ask server for new data in background.

Finally, I found out the way.It works for me. Now, I do not understand the code , I am trying to read and get it clearly. Thanks all for your comments.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM