如何将php smarty下拉列表转换为按钮?

时间:2011-05-31 08:58:48

标签: php smarty

我需要帮助将php smarty下拉到按钮。

我正在尝试为订阅和取消订阅时事通讯创建两个按钮。

请帮助。

Php smarty code:

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 个答案:

答案 0 :(得分: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']))
    {
        /* ... */
    }
}

只会点击其中一个,因此只有其中一个会与表单一起发送。