jquery - How to add php code in JS file

988

Im using yii framework to do web development , text are call from specific php file in order to help me exchange the language. Some how i have to add some message from JS file, but i still need the yii php to call the text . How to do that? bellow if my coding

I would like to change "English: 26 character","Chinese: 16 character" and "Other: 16 character".

Because in yii calling text from other php is something like this:

In the labels.php is like this:

(eg: 'englishchar'=>'English: 26 character')

So to call the text is like this:

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 code:

              <div >
              <div >* <?php echo Yii::t('labels', 'gametitle'); ?> :</div>
              <select id="select_gametitle" name="select_gametitle" >
                <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" ></div>
                <input id="txtgametitle" name="txtgametitle" type="text"  placeholder="" />

            </div>
362

Answer

Solution:

You can add it as a global var inside tags in your main layout file.

/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>
39

Answer

Solution:

You could add the script before the div, from PHP, for example:

<!--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 >
  <div >* <?php echo Yii::t('labels', 'gametitle'); ?> :</div>
  <!--the rest of your div-->
</div>
118

Answer

Solution:

You could echo out the translate into a 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');

Extending from this, you could use AJAX and JS template

People are also looking for solutions to the problem: error in accessing mysql OUT variable in php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.