Using php inside javascript

时间:2014-03-12 作者:user3274745

我在名为custom的javascript(jquery)文件中获得了以下代码。js公司:

     var gapinvite = new Date();


    gapinvite = new Date(2014, 06 - 1, 2);
    $(\'.days\').countdown({
        until: gapinvite,
        layout: \'{dn} {dl}\',

        /* Set your timezone */
        timezone: +7
    });
1。现在我希望用户能够更改上述日期。我创建了一个名为“主题选项”的主题选项页面。php

2、我正在使用<?php require_once(\'theme-options.php\'); ?> 在函数中。php链接到主题选项。php。

3、这是主题选项。php:

<?php
add_action(\'admin_menu\', \'director_create_menu\');
function director_create_menu() {
add_submenu_page( \'themes.php\', \' Theme Options\',
\'Theme Options\', \'administrator\', __FILE__,
\'director_settings_page\');
add_action( \'admin_init\', \'director_register_settings\' );
}
function director_register_settings() {
register_setting( \'director-settings-group\', \'director_date\' );
}

div class="wrap">
  <h2>Theme Settings</h2>
  <form id="landingOptions" method="post" action="options.php">
    <?php settings_fields( \'director-settings-group\' ); ?>
    <table class="form-table">
<tr valign="top">
          <th scope="row">Date:</th>
          <td>
            <input type="text" placeholder="2014, 06 - 1, 2" name="director_date"
            value="<?php print get_option(\'director_date\');
            ?>" />
          </td>
        </tr>
      <tr valign="top">
      <th scope="row">Time Zone:</th>
      <td>
        <input type="text" placeholder="+5" name="director_timezone"
        value="<?php print get_option(\'director_timezone\');
        ?>" />
      </td>
    </tr>
     <p class="submit">
        <input type="submit" class="button-primary" value="<?php _e(\'Save Changes\') ?>"   />
        </p>
      </form>
    </div>
    <?php } ?>
基本上是有一个主题选项页面。用户在其中给出日期。现在我想在javascript文件中使用该日期。如果我必须在索引中使用它。php应该是

<?php $date = get_option(\'director_date\'); ?>
<?php if( $date ) : ?> <?php echo $date; ?><?php endif; ?>);
以及

<?php $timezone = get_option(\'director_timezone\'); ?>
<?php if( $timezone ) : ?> <?php echo $timezone; ?><?php endif; ?>);
。然而,这是javascript。我如何在这里实施这样的行动?

2 个回复
最合适的回答,由SO网友:Adam 整理而成

如果您只想在更改和保存日期后重新实例化倒计时计时器,则应执行以下操作:

//In your custom.js file....
//make sure your localized data is a dependency of your custom.js file so you have proper scope to local data
jQuery(document).ready(function($){

    var date, gapinvite;

    date = $(\'input[name="director_date"]\');

    if ( date.val().length ) {

         gapinvite = new Date(Data.date); //if localizing variable

         /* OR */

        //gapinvite = new Date(date.val()); //if accessing variable directly from value attribute

    } else {

         /* if gapinvite should always start from 2014, 06 - 1, 2 as default do... */
         gapinvite = new Date(2014, 06 - 1, 2);

         /* if gapinvite should always start from today as default do... */
         //gapinvite = new Date(); //uncomment if required

         /* 
          * if gapinvite should always come from your default placeholder value do...
          * how you generate that value is up to you.
          */
         //gapinvite = new Date(date.attr(\'placeholder\'));  //uncomment if required
    }

    $(\'.days\').countdown({
        until: gapinvite,
        layout: \'{dn} {dl}\',

        /* Set your timezone */
        timezone: gapinvite.getTimezoneOffset()
    });

});
如果director\\u date value属性为空,这将获取默认占位符日期,否则它将获取用户提供的值并相应地实例化倒计时计时器。

将来,与JavaScript和jQuery严格相关的问题应在Stackoverflow

编辑:删除不必要的时区变量,直接在对象属性值内计算偏移量。

SO网友:Steven Jones

您可以使用wp localize script() 以便将数据发送到Javascript文件。

排队后自定义。js然后可以像这样向其传递数据:

wp_enqueue_script(\'custom_script\' );
wp_localize_script(\'custom_script\', \'Data\', array(\'date\' => get_option(\'director_date\')));
然后,您可以访问javascript文件中的日期,如下所示:

console.log(Data.date);

结束