2011-05-31 51 views
0

我需要幫助將php smarty下拉到按鈕。如何將一個PHP的smarty下拉到一個按鈕?

我正在嘗試爲訂閱和取消訂閱通訊創建兩個按鈕。

請幫忙。

腓智者代碼:

private function isNewsletterRegistered($customerEmail) 
{ 
    if (Db::getInstance()->getRow('SELECT `email` FROM '._DB_PREFIX_.'newsletter WHERE `email` = \''.pSQL($customerEmail).'\'')) 
     return 1; 
    if (!$registered = Db::getInstance()->getRow('SELECT `newsletter` FROM '._DB_PREFIX_.'customer WHERE `email` = \''.pSQL($customerEmail).'\'')) 
     return -1; 
    if ($registered['newsletter'] == '1') 
     return 2; 
    return 0; 
} 

private function newsletterRegistration() 
{ 
    if (empty($_POST['email']) OR !Validate::isEmail(pSQL($_POST['email']))) 
     return $this->error = $this->l('Invalid e-mail address'); 
    /* Unsubscription */ 
    elseif ($_POST['action'] == '1') 
    { 
     $registerStatus = $this->isNewsletterRegistered(pSQL($_POST['email'])); 
     if ($registerStatus < 1) 
      return $this->error = $this->l('E-mail address not registered'); 
     /* If the user ins't a customer */ 
     elseif ($registerStatus == 1) 
     { 
      if (!Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'newsletter WHERE `email` = \''.pSQL($_POST['email']).'\'')) 
       return $this->error = $this->l('Error during unsubscription'); 
      return $this->valid = $this->l('Unsubscription successful'); 
     } 
     /* If the user is a customer */ 
     elseif ($registerStatus == 2) 
     { 
      if (!Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'customer SET `newsletter` = 0 WHERE `email` = \''.pSQL($_POST['email']).'\'')) 
       return $this->error = $this->l('Error during unsubscription'); 
      return $this->valid = $this->l('Unsubscription successful'); 
     } 
    } 
    /* Subscription */ 
    elseif ($_POST['action'] == '0') 
    { 
     $registerStatus = $this->isNewsletterRegistered(pSQL($_POST['email'])); 
     if ($registerStatus > 0) 
      return $this->error = $this->l('E-mail address already registered'); 
     /* If the user ins't a customer */ 
     elseif ($registerStatus == -1) 
     { 
      global $cookie; 

      if (!Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'newsletter (email, newsletter_date_add, ip_registration_newsletter, http_referer) VALUES (\''.pSQL($_POST['email']).'\', NOW(), \''.pSQL(Tools::getRemoteAddr()).'\', 
       (SELECT c.http_referer FROM '._DB_PREFIX_.'connections c WHERE c.id_guest = '.(int)($cookie->id_guest).' ORDER BY c.date_add DESC LIMIT 1))')) 
       return $this->error = $this->l('Error during subscription'); 
      $this->sendVoucher(pSQL($_POST['email'])); 

      return $this->valid = $this->l('Subscription successful'); 
     } 
     /* If the user is a customer */ 
     elseif ($registerStatus == 0) 
     { 
      if (!Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'customer SET `newsletter` = 1, newsletter_date_add = NOW(), `ip_registration_newsletter` = \''.pSQL(Tools::getRemoteAddr()).'\' WHERE `email` = \''.pSQL($_POST['email']).'\'')) 
       return $this->error = $this->l('Error during subscription'); 
      $this->sendVoucher(pSQL($_POST['email'])); 

      return $this->valid = $this->l('Subscription successful'); 
     } 
    } 
} 

private function sendVoucher($email) 
{ 
    global $cookie; 

    if ($discount = Configuration::get('NW_VOUCHER_CODE')) 
     return Mail::Send((int)($cookie->id_lang), 'newsletter_voucher', Mail::l('Newsletter voucher'), array('{discount}' => $discount), $email, NULL, NULL, NULL, NULL, NULL, dirname(__FILE__).'/mails/'); 
    return false; 
} 

function hookfooter($params) 
{ 
    global $smarty; 

    if (Tools::isSubmit('submitNewsletter')) 
    { 
     $this->newsletterRegistration(); 
     if ($this->error) 
     { 
      $smarty->assign(array('color' => 'red', 
            'msg' => $this->error, 
            'nw_value' => isset($_POST['email']) ? pSQL($_POST['email']) : false, 
            'nw_error' => true, 
            'action' => $_POST['action'])); 
     } 
     elseif ($this->valid) 
     { 
      if (Configuration::get('NW_CONFIRMATION_EMAIL') AND isset($_POST['action']) AND (int)($_POST['action']) == 0) 
       Mail::Send((int)($params['cookie']->id_lang), 'newsletter_conf', Mail::l('Newsletter confirmation'), array(), pSQL($_POST['email']), NULL, NULL, NULL, NULL, NULL, dirname(__FILE__).'/mails/'); 
      $smarty->assign(array('color' => 'green', 
            'msg' => $this->valid, 
            'nw_error' => false)); 
     } 
    } 
    $smarty->assign('this_path', $this->_path); 
    return $this->display(__FILE__, 'blocknewsletter.tpl'); 
} 

TPL文件代碼:

<div id="newsletter_block_left" class="block"> 
<h4>{l s='Newsletter' mod='blocknewsletter'}</h4> 
<div class="block_content"> 
{if isset($msg) && $msg} 
    <p class="{if $nw_error}warning_inline{else}success_inline{/if}">{$msg}</p> 
{/if} 
    <form action="{$link->getPageLink('index.php')}" method="post"> 
     <p><input type="text" name="email" size="18" value="{if isset($value) && $value}{$value}{else}{l s='your e-mail' mod='blocknewsletter'}{/if}" onfocus="javascript:if(this.value=='{l s='your e-mail' mod='blocknewsletter'}')this.value='';" onblur="javascript:if(this.value=='')this.value='{l s='your e-mail' mod='blocknewsletter'}';" /></p> 
     <p> 
      <select name="action"> 
       <option value="0"{if isset($action) && $action == 0} selected="selected"{/if}>{l s='Subscribe' mod='blocknewsletter'}</option> 
       <option value="1"{if isset($action) && $action == 1} selected="selected"{/if}>{l s='Unsubscribe' mod='blocknewsletter'}</option> 
      </select> 
      <input type="submit" value="ok" class="button_mini" name="submitNewsletter" /> 
     </p> 
    </form> 
</div> 

回答

1

編輯:我所做的代碼片段更具體一點,我希望它能幫助

使用2提交按鈕

<form action="{$link->getPageLink('index.php')}" method="post"> 
    <p><input type="text" name="email" size="18" value="{if isset($value) && $value}{$value}{else}{l s='your e-mail' mod='blocknewsletter'}{/if}" onfocus="javascript:if(this.value=='{l s='your e-mail' mod='blocknewsletter'}')this.value='';" onblur="javascript:if(this.value=='')this.value='{l s='your e-mail' mod='blocknewsletter'}';" /></p> 
    <p> 
     <input type="submit" value="unsubscribe" class="button_mini" name="submitNewsletterUnsubscribe" /> 
     <input type="submit" value="subscribe" class="button_mini" name="submitNewsletterSubscribe" /> 
    </p> 
</form> 

在PHP,測試,如果他們中的一個設置:

private function newsletterRegistration() 
{ 
    if (empty($_POST['email']) OR !Validate::isEmail(pSQL($_POST['email']))) 
     return $this->error = $this->l('Invalid e-mail address'); 
    /* Unsubscription */ 
    elseif (isset($_POST['submitNewsletterSubscribe'])) 
    { 
     /* ... */ 
    } 
    /* Subscription */ 
    elseif (isset($_POST['submitNewsletterUnsubscribe'])) 
    { 
     /* ... */ 
    } 
} 

其中只有一個會被點擊,只有其中一人將與形式發送。

+0

它確實,nt工作 – user700117 2011-06-02 09:37:00

+0

我使代碼片段更具體到你自己的代碼,我真的認爲這應該工作。 – aorcsik 2011-06-02 11:45:16

相關問題