Jump to content

Problem : Call PHP function with TPL


Recommended Posts

Hello everyone,

 

I have a problem while calling my php functon in my tpl. In my php function I have sql request to update my db.

I want the function to be executed only when the page loads.

The problem is that I have a checkbox which should updates the same db every time I click on it, and beceause I put my php function the changes are not made when I click on the checkbox.

Before I added my php function, it used to work but know it doesn't.

I think it's because in my tpl I wrote the function like this :

{myFunction}

It cancels out my checkbox.

I think I need to use a jquery ready function but i don't know how it works.

 

Can someone help me please ?

Link to comment
Share on other sites

Here are a couple links on it. Let me preface by saying that it is not advised to put php in a template file.

http://www.smarty.net/docsv2/en/language.function.php.tpl

 

To turn php on in the template this will help

http://www.smarty.net/forums/viewtopic.php?p=72216

 

you add the setting to the config/smarty.config.inc.php file, around the top declarations.

Link to comment
Share on other sites

I will expose my problem more properly with all my files and what I want to do.

 

I have my tpl file :

 

{initialisation}

 

 

<script type="text/javascript">

function cliquer(){

var xhr_object = null;

if(window.XMLHttpRequest)

{ // Firefox

xhr_object = new XMLHttpRequest();

}

else if(window.ActiveXObject)

{ // Internet Explorer

xhr_object = new ActiveXObject('Microsoft.XMLHTTP');

}

var method = 'POST';

var filename = './modules/plusquinze/plusquinze_page.php';

xhr_object.open(method, filename, true);

xhr_object.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

xhr_object.onreadystatechange = function()

{

if(xhr_object.readyState == 4)

{

if (xhr_object.status == 200)

{

alert(xhr_object.responseText);

}

}

}

var case_check=document.getElementById('PlusQuinze');

if(case_check.checked)

{

var data="valeur_checkbox=1";

 

alert('1');

}

else

{

var data="valeur_checkbox=0";

alert('0');

}

xhr_object.send(data);"

}

</script>

 

<p class="checkbox">

<input id="PlusQuinze" type="checkbox" name="PlusQuinze" value='1'

onclick="cliquer()" checked />

{l s='I would like to take the insurance.' mod='plusquinze'}

</p>

 

This file calls up the function in my php :

 

 

<?php

if ( !defined( '_PS_VERSION_' ) )

exit;

class PlusQuinze extends Module

{

public function __construct()

{

$this->name = 'plusquinze';

$this->tab = 'Test';

$this->version = 1.0;

$this->author = 'DMW';

$this->need_instance = 0;

parent::__construct();

$this->displayName = $this->l( 'Plus Quinze' );

$this->description = $this->l( 'Ce module ajoute 15euros à la commande si la case est cochée.' );

}

public function install()

{

if ( parent::install() == false OR !$this->registerHook( 'extraCarrier' ) )

if ( parent::install() == false OR !$this->registerHook( 'header' ) )

return false;

return true;

}

public function hookExtraCarrier( $params )

{

global $smarty;

$callback = array(&$this,'initialisation'); //To use initialisation

$type = smartyRegisterFunction($smarty, 'function', 'initialisation', $callback); //To use initialisation

return $this->display( __FILE__, 'plusquinze.tpl');

}

public function hookHeader( $params )

{

global $smarty;

return $this->display( __FILE__, 'tplheader.tpl');

}

public function initialisation($params)

{

// SQL REQUESTS

}

}

?>

 

And on the tpl there is also a php file which is loaded with ajax each time I the checkbox is checked or unchecked.

 

<?PHP

//SQL Requests

?>

 

The problem is before I added my function initialisation, I managed to update the db every time someone checked or unchecked the box.

Now that the function is here to initialize my sql table, the checkbox doesn't work anymore.

And I think it is because my initialisation function cancels out my cliquer function in the checkbox. But I'm not sure.

Link to comment
Share on other sites

You really should be handling that in your php file and calling the smarty variables with your template file. A templating system is not really for calling php in your template files. It is a security risk.

Link to comment
Share on other sites

×
×
  • Create New...