How to include custom PHP Script into .PHTML file

Magento 1.9 - How to include custom PHP Script into .PHTML file

My magento tracking page:

Back-end magento code :

How can i add my PHP script into this tracking page.

Tracking page code - trackorder.phtml [https://i.stack.imgur.com/ySgMA.png]

?php
    if(Mage::getStoreConfig('trackorder/trackorder_general/enabled')):
    ?>
    <div class="page-title"><h1><?php echo $this->__('Track Your Order ') ?></h1></div>
    <div  class="form-list">
        <form name="track_order" id="track_order" action="" method="post" onsubmit="sendAjax('track_order','<?php  echo Mage::getUrl('*/*/track');?>'); return false;">
        <!--<form name="track_order" method="post" id="track_order" action="<?php echo Mage::getUrl('*/*/view');?>">-->
            <ul class="form-list">
                <li>
                    <label for="order_id" class="required"><em>*</em><?php echo $this->__('Order Id') ?></label>
                    <div class="input-box">
                        <input type="text" name="order_id" id="order_id" value="" title="" class="input-text required-entry" />
                    </div>    
                </li>
                <li>
                    <label for="email_address" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
                    <div class="input-box" >
                        <input type="text" name="email" id="email_address" value="" title="<?php echo $this->__('Email Address') ?>" class="input-text validate-email required-entry" />
                    </div>    
                </li>
            </ul>
            <div class="buttons-set">
               <button type="submit" class="button" title="<?php echo $this->__('Track Order') ?>" name="track" id="track">
                    <span><span><?php echo $this->__('Track Order') ?></span></span>
                </button>
            </div>

        </form>
        <div id="loading-details" class="loading-details" style="display:none">
            <div id="loading-mask" >
                <p class="loader" id="loading_mask_loader"><img src="<?php echo $this->getSkinUrl('trackorder/images/ajax-loader-tr.gif') ?>" alt="<?php echo Mage::helper('adminhtml')->__('Loading...') ?>"/><br/><?php echo $this->__('Please wait...') ?></p>
            </div>
        </div>
    </div> 

    <div id="oderinfo" class="order-info-message"></div>

    <script type="text/javascript">
        var validateForm = new VarienForm('track_order', true);
    </script>           
    <script type="text/javascript">

        function sendAjax(frmId,url){
            if (!validateForm.validator.validate()) {
                return;
            }
            var data = $(frmId).serialize(this);
            $("loading-details").show();


        new Ajax.Updater(
                {
                    success:"oderinfo"
                },

                url,
                {
                    asynchronous:true,
                    evalScripts:false,
                    onComplete:function(request, json){
                        $("loading-details").hide();
                        return false;
                    }, 
                    onLoading:function(request, json){},
                    parameters:data
                }
            ); 
            return false;
        }

    </script>
    <?php else: ?>
    <?php
        $url = Mage::getBaseUrl();
        Mage::app()->getFrontController()->getResponse()->setRedirect($url);
    ?>
    <?php endif; ?>

From above code how can i add my own PHP script :

My PHP script :

<!DOCTYPE HTML>
<html>

<body>

    <form action="#" method="POST">
        Select Courier :
        <select name="courier">
            <option disabled='disabled' selected>-- Choose an option --</option>
            <option value="professional_courier">Professional Courier</option>
            <option value="shree_maruti_courier">Shree Maruti Courier</option>
            <option value="india_post_courier">India Post Courier</option>
            <option value="dhl_courier">DHL Courier</option>
            <option value="fedex_courier">Fedex Courier</option>
            <option value="ups_courier">UPS Courier</option>
        </select>

        Trackingid: <input type="text" name="trackingid">
        <input type="submit">

    </form>

    <?php
    if (isset($_POST['courier'])) {
        // Professional Courier
        if ('professional_courier' === $_POST['courier']) {
            header("Location: https://www.tpcindia.com/Tracking2014.aspx?id=" . $_POST["trackingid"] . "&type=0&service=0");
        }
        // Shree Maruti Courier
        else if ('shree_maruti_courier' === $_POST['courier']) {
            header("Location: https://www.shreemaruticourier.com/track-your-shipment/#track-your", "_blank");
        }

        // india_post_courier
        else if ('india_post_courier' === $_POST['courier']) {
            header("Location: https://www.indiapost.gov.in/vas/Pages/IndiaPostHome.aspx/#main-content", "_blank");
        }

        // DHL Courier
        else if ('dhl_courier' === $_POST['courier']) {
            header("Location: https://www.dhl.com/en/express/tracking.html?AWB=" . $_POST["trackingid"] . "&brand=DHL", "_blank");
        }

        // Fedex Courier
        else if ('fedex_courier' === $_POST['courier']) {
            header("Location: https://www.fedex.com/apps/fedextrack/?action=track&trackingnumber=" . $_POST["trackingid"] . "&cntry_code=in&locale=en_IN", "_blank");
        }

        // ups_courier
        else if ('ups_courier' === $_POST['courier']) {
            header("Location: https://www.ups.com/track?loc=en_US&tracknum=" . $_POST["trackingid"] . "&requester=WT/trackdetails", "_blank");
        }
    }
    ?>
</body>

</html>

My Workout in trackorder.phtml getting error : [start & end header added]

<?php
    if(Mage::getStoreConfig('trackorder/trackorder_general/enabled')):
    ?>
    <div class="page-title"><h1><?php echo $this->__('Track Your Order ') ?></h1></div>
    <div  class="form-list" style="float: left;">
        <form name="track_order" id="track_order" action="" method="post" onsubmit="sendAjax('track_order','<?php  echo Mage::getUrl('*/*/track');?>'); return false;">
        <!--<form name="track_order" method="post" id="track_order" action="<?php echo Mage::getUrl('*/*/view');?>">-->
            <ul class="form-list">
                <li>
                    <label for="order_id" class="required"><em>*</em><?php echo $this->__('Order Id') ?></label>
                    <div class="input-box">
                        <input type="text" name="order_id" id="order_id" value="" title="" class="input-text required-entry" />
                    </div>    
                </li>
                <li>
                    <label for="email_address" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
                    <div class="input-box" >
                        <input type="text" name="email" id="email_address" value="" title="<?php echo $this->__('Email Address') ?>" class="input-text validate-email required-entry" />
                    </div>    
                </li>
            </ul>
            <div class="buttons-set">
               <button type="submit" class="button" title="<?php echo $this->__('Track Order') ?>" name="track" id="track">
                    <span><span><?php echo $this->__('Track Order') ?></span></span>
                </button>
            </div>

        </form>
        <div id="loading-details" class="loading-details" style="display:none">
            <div id="loading-mask" >
                <p class="loader" id="loading_mask_loader"><img src="<?php echo $this->getSkinUrl('trackorder/images/ajax-loader-tr.gif') ?>" alt="<?php echo Mage::helper('adminhtml')->__('Loading...') ?>"/><br/><?php echo $this->__('Please wait...') ?></p>
            </div>
        </div>
    </div> 

    <!-- Start couier tracking -->

      <div style="float: left;">
        <form action="#" method="POST" style="padding: 28px 15px 21px 196px;">
            Select Courier :
            <select name="courier">
            <option disabled='disabled' selected>-- Choose an option --</option>
            <option value="professional_courier">Professional Courier</option>
            <option value="shree_maruti_courier">Shree Maruti Courier</option>
            <option value="india_post_courier">India Post Courier</option>
            <option value="dhl_courier">DHL Courier</option>
            <option value="fedex_courier">Fedex Courier</option>
            <option value="ups_courier">UPS Courier</option>
        </select>

        Trackingid: <input type="text" name="trackingid">
        <input type="submit">

    </form>

    <?php
    if (isset($_POST['courier'])) {
        // Professional Courier
        if ('professional_courier' === $_POST['courier']) {
            header("Location: https://www.tpcindia.com/Tracking2014.aspx?id=" . $_POST["trackingid"] . "&type=0&service=0");
        }
        // Shree Maruti Courier
        else if ('shree_maruti_courier' === $_POST['courier']) {
            header("Location: https://www.shreemaruticourier.com/track-your-shipment/#track-your", "_blank");
        }

        // india_post_courier
        else if ('india_post_courier' === $_POST['courier']) {
            header("Location: https://www.indiapost.gov.in/vas/Pages/IndiaPostHome.aspx/#main-content", "_blank");
        }

        // DHL Courier
        else if ('dhl_courier' === $_POST['courier']) {
            header("Location: https://www.dhl.com/en/express/tracking.html?AWB=" . $_POST["trackingid"] . "&brand=DHL", "_blank");
        }

        // Fedex Courier
        else if ('fedex_courier' === $_POST['courier']) {
            header("Location: https://www.fedex.com/apps/fedextrack/?action=track&trackingnumber=" . $_POST["trackingid"] . "&cntry_code=in&locale=en_IN", "_blank");
        }

        // ups_courier
        else if ('ups_courier' === $_POST['courier']) {
            header("Location: https://www.ups.com/track?loc=en_US&tracknum=" . $_POST["trackingid"] . "&requester=WT/trackdetails", "_blank");
        }
    }
    ?>

    </div>

    <!-- End couier tracking -->

    <div id="oderinfo" class="order-info-message"></div>

    <script type="text/javascript">
        var validateForm = new VarienForm('track_order', true);
    </script>           
    <script type="text/javascript">

        function sendAjax(frmId,url){
            if (!validateForm.validator.validate()) {
                return;
            }
            var data = $(frmId).serialize(this);
            $("loading-details").show();


        new Ajax.Updater(
                {
                    success:"oderinfo"
                },

                url,
                {
                    asynchronous:true,
                    evalScripts:false,
                    onComplete:function(request, json){
                        $("loading-details").hide();
                        return false;
                    }, 
                    onLoading:function(request, json){},
                    parameters:data
                }
            ); 
            return false;
        }

    </script>
    <?php else: ?>
    <?php
        $url = Mage::getBaseUrl();
        Mage::app()->getFrontController()->getResponse()->setRedirect($url);
    ?>
    <?php endif; ?>

You need to have the form-processing PHP code before the form. As you are trying to do header redirects, this in particular must be before any output to the browser, otherwise you’ll get something like a “headers already sent” error when you redirect.

I just learning stage in PHP can i get more help?

I can’t think of a way to explain it differently. In the section of code in your post above headed “My PHP script:”, you have the PHP form-processing code after you’ve sent the form to the browser, and you need to put it before. All the PHP code starting from the <?php opening tag needs to be before the <!DOCTYPE line.

I’m also not sure whether or not the PHP will work correctly if your files are called whatever.phtml as I’m not familiar with your configuration.

I am not familiar with hacking Magento code or templates. But I know PHP can be picky about headers being already sent.

PHP can do code server-side and output HTML. While “inside of” PHP, i.e. … <?php echo "Flynn's in"; ?> … one needs to echo or otherwise tell PHP to “send this in the response”. While “outside of” PHP, text is treated as text, not PHP code. It is automatically treated as output without explicit instructions to do so.

Perhaps what is missing is a better understanding of HTTP, response headers in particular. Once PHP sends any HTML - including even a single whitespace - HTTP headers are sent something like “text/html is on the way”. So later on in the code you will have trouble sending something like “no text/html is on the way, go here instead”.

IMHO, the best approach is to put the header() before any HTML is output

In other words, if you start the file with “<?php” and put the conditional if header redirects before the first “?>” (when going outside of PHP and into HTML) there won’t be an “already sent” problem.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.