插入脚本输出TinyMCE和Jquery

时间:2013-05-01 07:34:19

标签: php jquery html ajax tinymce

我有一个创建基本表格布局结构的脚本,我正在尝试使用AJAX和jQuery将该脚本的输出输入到TinyMCE textarea中。我用PHP编写了脚本,如果我直接打开脚本,它的效果非常好。

我要做的是让用户从下拉列表中选择一些内容(在本例中为产品),然后返回一个填充了该产品相关数据的表格。我使用以下脚本将其插入textarea:

$('#product_id').change(function(e) {

    product_id = $(this).val();

    $.ajax({
        url: 'http://<? echo $_SERVER['HTTP_HOST']; ?>/includes/ajax.php',
        type: 'POST',
        data: { product_choice: true, product_id: product_id },
        success: function(data) {
            $('#mce_1').val(data);
        }
    });

});

'data'确实返回但是当它放在tinyMCE textarea中时,它显示以下错误(脚本继续并输出表):

警告:在第71行的/home/sites/very-dev.co.uk/public_html/cms/includes/ajax.php中为foreach()提供的参数无效警告:为foreach()提供的参数无效第100行/home/sites/very-dev.co.uk/public_html/cms/includes/ajax.php

这两个错误是: foreach($ product_data as $ key =&gt; $ product): foreach($ product_data as $ product):

// Connect to database
$link = mysqli_connect(DBHOST,DBUSER,DBPASS,DBNAME);

// check connection //
if (mysqli_connect_errno()):
    return printf("Connect failed: %s\n", mysqli_connect_error());
endif;

$col_qry = mysqli_query($link,"
            SELECT column_name
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE table_name = 'cms_plugins_products_models'
            AND column_name != 'id'
            AND column_name != 'product_id';")

            or die(mysqli_error($link)
);

while($col_row = mysqli_fetch_object($col_qry)):
    $col_names_array[] = $col_row->column_name;
endwhile;

$qry = "SELECT product.name AS product_name, product.id, model.*, image.src AS thumb
        FROM cms_plugins_products AS product
        LEFT JOIN cms_plugins_products_models AS model
        ON (product.id=model.product_id)
        LEFT JOIN cms_plugins_products_images AS image
        ON (product.id=image.product_id)
        AND (image.is_thumb=1)
        WHERE product.id='" . $_GET['product_id'] . "'";

$result = mysqli_query($link, $qry) or die(mysqli_error($link));

while($row = mysqli_fetch_object($result)):
    foreach($col_names_array as $column):

        $columns = explode('_', $column);

        if($column != 'name' && $column != 'description'):

            $components_qry = "SELECT name
                    FROM cms_plugins_products_components_" . $columns[0] . "
                    WHERE id='" . $row->$column . "'";

            $components_result = mysqli_query($link, $components_qry) or die(mysqli_error($link));
            $components_row = mysqli_fetch_object($components_result);
        endif;

        if($columns[0] == 'name'):
            $product_data[$row->product_name][$row->name][$columns[0]] = $row->name;
        elseif($columns[0] == 'description'):
            $product_data[$row->product_name][$row->name][$columns[0]] = $row->description;
        else:
            $product_data[$row->product_name][$row->name][$columns[0]] = $components_row->name;
        endif;
    endforeach;
    $thumb = $row->thumb;
endwhile; ?>

<table>

    <thead>

        <tr>
            <th></th> <?
            $i = 0;
            foreach($product_data as $key=>$product):
                foreach($product_data[$key] as $key2=>$model): ?>
                    <th <? if($i == 1): echo 'id="recommended"'; endif; ?>> <?
                        switch($i):
                            case 0:
                                echo 'Basic';
                            break;
                            case 1:
                                echo 'Mid';
                            break;
                            case 2:
                                echo 'Pro';
                            break;
                        endswitch; ?>
                        <div>
                            <img alt="<? echo $key; ?>" src="http://cms.very-dev.co.uk<? echo $thumb; ?>">
                        </div>
                        <h5><? echo $key2; ?></h5>
                        <p><? echo $product_data[$key][$key2]['description']; ?></p>
                        <a>Customise?</a>
                    </th> <?
                    $i++;
                endforeach;
            endforeach; ?>
        </tr>

    </thead>

    <tbody> <?
        foreach($product_data as $product):
            $i = 0;
            foreach($product as $model):
                foreach($model as $key2=>$component): ?>
                    <tr> <?
                        if($key2 != 'name' && $key2 != 'description'): ?>
                            <td><? echo $key2; ?></td> <?
                            foreach($product as $key=>$model): ?>
                                <td><? echo $product[$key][$key2]; ?></td> <?
                            endforeach;
                        endif; ?>
                    </tr> <?
                endforeach;
                if($i == 0): exit(); endif;
                $i++;
            endforeach;
        endforeach; ?>
    </tbody>

</table>

这可能是一个非常简单的问题,我没有看到,对此有任何帮助吗?

1 个答案:

答案 0 :(得分:1)

仔细查看代码之后,这是一个简单的错误。我正在为product_id使用GET,但是当包括当然没有设置因此它正在崩溃但是因为它是一个包含而没有错误。只需通过发送GET变量即可修复。