如何在JS文件中添加php代码

时间:2014-10-12 05:40:12

标签: php jquery yii

我使用yii框架进行web开发,文本是从特定的php文件调用,以帮助我交换语言。一些我如何从JS文件添加一些消息,但我仍然需要yii php来调用文本。怎么做?如果我的编码吼叫

我想改变"英语:26个字符","中文:16个字符"和"其他:16个字符"。

因为在yii中调用来自其他php的文本是这样的:

在labels.php中是这样的:

(例如:' englishchar' =>'英语:26个字符')

所以调用文本是这样的:

eg:<?php echo Yii::t('labels','englishchar');?>


$(function () {
        $('#select_gametitle').change(function () {
            var k = $(this).val();

            if (k == "E") {
                $("#txtgametitle").attr("placeholder", "English: 26 characters").placeholder();
                $("#txtgametitle").attr('maxlength', '26');
            }
            else if (k == "C") {
                $("#txtgametitle").attr("placeholder", "Chinese: 16 characters").placeholder();
                $("#txtgametitle").attr('maxlength', '26');
            }
            else if (k == "O") {
                $("#txtgametitle").attr("placeholder", "Other: 16 characters").placeholder();
                $("#txtgametitle").attr('maxlength', '26');
            }
            });
                $('input[placeholder], textarea[placeholder]').placeholder();
        });

PHP代码:

                           <div class="inputWrapper">
                            <div class="usetitle">* <?php echo Yii::t('labels', 'gametitle'); ?> :</div>
                            <select id="select_gametitle" name="select_gametitle" class="selectInput" style="width:369px;">
                                <option value=""><?php echo Yii::t('labels', 'select_gametitle'); ?></option>
                                <option value="E"><?php echo Yii::t('labels', 'english'); ?></option>
                                <option value="C"><?php echo Yii::t('labels', 'chinese'); ?></option>
                                <option value="O"><?php echo Yii::t('labels', 'other'); ?></option>
                            </select>
                            <div id="err_select_gametitle" class="error"></div>
                                <input id="txtgametitle"  name="txtgametitle" type="text" class="textInput" style="width:352px;" placeholder=""  />

                        </div>

3 个答案:

答案 0 :(得分:0)

您可以将其添加为主布局文件中的全局var inside标签。

/view/layouts/main.php:

<?php 
    Yii::app()->getClientScript()->registerCoreScript('jquery.ui');
    Yii::app()->clientScript->registerCssFile(Yii::app()->clientScript->getCoreScriptUrl().'/jui/css/base/jquery-ui.css');
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="language" content="en" />

    <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/styles.css" />

    <title><?php echo CHtml::encode($this->pageTitle); ?></title>

    <?php Yii::app()->bootstrap->register(); ?>
    <link rel="stylesheet" href="<?php echo Yii::app()->baseUrl; ?>/css/font-awesome-4.0.3/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->baseUrl; ?>/css/style.css" />
    <script>
       eg:<?php echo Yii::t('labels','englishchar');?>
    </script>
</head>

<body>

答案 1 :(得分:0)

您可以在PHP之前添加脚本,例如:

<!--just add your script code-->
<script>
  $(function () {
    //your js code
    <?php echo "with text printed by php"); ?>
  });
</script>

<!--and now your html code, also with embedded PHP-->
<div class="inputWrapper">
    <div class="usetitle">* <?php echo Yii::t('labels', 'gametitle'); ?> :</div>
    <!--the rest of your div-->
</div>

答案 2 :(得分:0)

您可以将翻译回显为JSON。

    Yii::app()->clientScript->registerScript('test',
            'var lang =' . json_encode( array(
        'message1'=>Yii::t('app','Message 1'), 
        'message2'=>Yii::t('app','Message 2'),              
    )) . '; console.log(lang.word1)');

    $this->render('json');

从此扩展,你可以使用AJAX和JS模板

相关问题