【问题标题】:Warning: Illegal string offset 'enabled' in警告:非法字符串偏移“启用”在
【发布时间】:2016-09-14 18:18:48
【问题描述】:

我们正在尝试使用 jQuery 步骤将表单分解为多个页面。错误指向我们正在尝试创建的表单。对表单的调用最初如下所示:

$enable_paid_submission = houzez_option('enable_paid_submission');
$user_pack_id       = get_the_author_meta( 'package_id' , $userID );
$remaining_listings     = 0;

if( is_page_template( 'submit_property_test.php' ) ) {

    if( $enable_paid_submission == 'membership' && $remaining_listings != -1 && $remaining_listings < 1 ) {
        print '<div class="user_package_status"><h4>'.esc_html__('HTMLhere.','houzez' ).'</h4></div>';

<?php
            $layout = houzez_option('property_form_sections');
            $layout = $layout['enabled'];

            if ($layout): foreach ($layout as $key=>$value) {
                switch($key) {
                    case 'features':
                        get_template_part( 'features' );
                        break;
                    case 'location':
                        get_template_part( 'location' );
                        break;
                    case 'multi-units':
                        get_template_part('multi-units');
                        break;
                }
            }
            endif;
            ?>

我们想将表单的三个部分(特征、位置和多单元)分成三个不同的页面。我们添加了 jQuery 步骤,现在看起来如下所示:

    <script>
        $(function ()
        {
            $("#wizard").steps({
                headerTag: "h2",
                bodyTag: "section",
                transitionEffect: "fade"
            });
        });
    </script>

    <div id="wizard">
        <h2>Features</h2>
        <section>
            <?php
            $layout = houzez_option('property_form_sections');
            $layout = $layout['enabled'];

            if ($layout): foreach ($layout as $key=>$value) {
                switch($key) {
                    case 'description-price':
                        get_template_part('features');
                        break;
                    }
            }
            endif;
            ?>
        <h2>Location</h2>
        <section>
            <?php
            $layout = houzez_option('property_form_sections');
            $layout = $layout['enabled'];
            if ($layout): foreach ($layout as $key=>$value) {
                switch($key) {
                    case 'description-price':
                        get_template_part('location');
                        break;
                    }
            }
            endif;
            ?>
        </section>
        <h2>Multi-units</h2>
        <section>
            <?php
            $layout = houzez_option('property_form_sections');
            $layout = $layout['enabled'];
            if ($layout): foreach ($layout as $key=>$value) {
                switch($key) {
                    case 'description-price':
                        get_template_part('multi-units');
                        break;
                    }
            }
            endif;
            ?>
        </section>

在最初的几个小时内运行良好。现在它正在返回错误。

【问题讨论】:

  • 这是 JavaScript 错误还是 PHP 错误?如果出现 PHP 错误(由于字符串 'enabled' 的位置,我预计会出现这种错误),请相应地更新您的标签。
  • 这实际上是 Wordpress 网站中的 PHP 错误。添加 PHP 作为标签之一。谢谢你的收获。

标签: php jquery string offset jquery-steps


【解决方案1】:

您似乎期望对houzez_option() 的函数调用返回一个数组。从您得到的错误看来,事实并非如此。如果没有看到 houzez_option() 代码的样子,就不可能告诉你为什么不是。

您仍然可以通过专门检查您希望从函数返回的内容来改进您的代码。

        $layout = isset( $layout['enabled'] ) ? $layout['enabled'] : '';
        if ( is_array( $layout ) ): foreach ($layout as $key=>$value) {
            switch($key) {
                case 'description-price':
                    get_template_part('features');
                    break;
                }
        }
        else:
            echo 'Unable to retrieve options';
        endif;

这将防止错误并提醒您它无法在前端运行。您还可以使最终用户的信息更清晰。我只是提供一个通用示例。

【讨论】:

  • 它确实使错误消失了,但是表单元素也不再出现在那里。它现在由一个空白表格组成。我已经编辑了我的初始查询以包括 houzez_option()。
  • @cErApHiM 我没有看到函数定义。您还应该通过 var_dump( $layout ); 来验证 $layout = houzez_option('property_form_sections'); 返回的内容
猜你喜欢
  • 2021-09-30
  • 2013-09-13
  • 1970-01-01
  • 2013-04-11
  • 2012-04-09
  • 2019-07-28
  • 1970-01-01
相关资源
最近更新 更多