Jump to content

[SOLVED] Run module function from tpl


cyberdoc

Recommended Posts

hi, this may sound dumb but im sorry for the confusion.

i basically have a module for whose frontend ive written a module.tpl now it has a button on clicking which i want to run a function in the module.php.. i'm all confused in these files write now!!

shall i go via a js function for onclick??

  <Form name ="form1" Method ="Post" ACTION ="function()">
<b><label for="Installation">Installation to be done?</label>
<Input type = 'Radio' Name ="install" id ="radio1"  onclick = "MyAlert()">DO Installation
<Input type = 'Radio' Name ="install" id="radio2"  onclick = "MyAlert()">no
<button type="submit"  VALUE="Update Cart">
   </b> </Form>
Edited by vekia (see edit history)
Link to comment
Share on other sites

i think you are confusing the difference between "form posting" and "function invocation".

 

You have an HTML form that you are submitting, so you would just make the form action URL a php file provided with your module.  If using PS 1.5+, this php file would be a module controller that handles the form submission

Link to comment
Share on other sites

yes exactly ,

so here is what i do..

 <Form name ="form1" Method ="Post" ACTION ="update.php">
<b><label for="Installation">Installation to be done?</label>
<Input type = 'Radio' Name ="install" id ="radio1"  onclick = "MyAler()">DO Installation
<Input type = 'Radio' Name ="install" id="radio2"  onclick = "MyAler()">no
<Input type= 'submit' value="Update Cart" name="submit">
   </b> </Form>

in the update.php file(in same folder)

<? php

code..

?> 

but it searches for it in the root folder.. what next? also if i go this way it will get the user back after executing the function na??

Link to comment
Share on other sites

<Form name ="form1" Method ="Post" ACTION ="update.php">

instead of it use:

<Form name ="form1" Method ="Post" ACTION ="{$content_dir}/modules/YOURMODULE/update.php">

but im affraid that your customer will be redirected to the update.php file and this is not what you're looking for

 

why you use update.php file? why not to use function inside the module class? (main module .php file)

Link to comment
Share on other sites

     public function hookdisplayshoppingcartfooter($params) {

         $cookie = $this->context->cookie;
        $sub_total=0;
        $products = $params['cart']->getProducts(true);
 //       print_r($products);
    //    print_r($cookie);
  
foreach ($products as $product) {
  $result = Db::getInstance()->getRow('SELECT textarea FROM `'._DB_PREFIX_.'belvg_samplemodule` sample WHERE sample.`id_product` = '.$product['id_product']);
//    print_r($product['id_product']);
$sub_total = $sub_total + $result['textarea'];
   }
$result =DB::getInstance()->Execute('UPDATE `'. _DB_PREFIX_ .'product` SET `price` = ' .$sub_total. ' WHERE `id_product` = 9');
print_r($sub_total);

            echo "<form name='testForm' id='testForm'  method='POST' >
<Input type = 'Radio' Name ='install' id ='radio1'>DO Installation
<Input type = 'Radio' Name ='install' id='radio2'>no
     <input type='submit' name='btn' autofocus />
 </form>";
        if(isset($_POST['install'])) {
            $params['cart']->updateQty(1, 9);
            $params['cart']->update();
        }

here is my code so far.. what it basically does is shows user an installation option. also it calculates cost for it from the db(saved already via the backend. ) then it makes that cost the price of a product 'installation' ive predefined and adds it to the cart  :huh:

this way if the user chooses installation option the relevant  cost is added.. now a few doubts.. how to refresh the cart after update?? is there a way to use ajax-addcart here?? also how i get this form back down.. 

u can refer the below link for ref::

http://soccer-madness.com/presta/index.php?controller=order

Edited by cyberdoc (see edit history)
Link to comment
Share on other sites

don't use echo there!

 

use your .tpl file as you used it before.

 

and just add there (in this hook) code like:

if(isset($_POST['install'])) {
$params['cart']->updateQty(1, 9);
$params['cart']->update();
}

and don't use action="" param in your <form>

page will be reloaded - hook will be executed, and if condition will be executed too

Link to comment
Share on other sites

fixed it...

Here is the code!!


        
         if(isset($_POST['install'])=='on') {
         echo($_POST['install']);
          $params['cart']->updateQty(1, 9);
$params['cart']->update();
header('Location: '. $_SERVER['PHP_SELF']);  
exit();
       }
 return $this->display(__FILE__, 'views/frontend/sample.tpl');
  }

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...