1

我有一个创建基本表格布局结构的脚本,我正在尝试使用 AJAX 和 jQuery 将该脚本的输出输入到 TinyMCE 文本区域。我用 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 文本区域中时,它显示以下错误(脚本继续并输出表格):

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

这两个错误是: foreach($product_data as $key=>$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>

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

4

1 回答 1

1

再仔细看代码,就是一个简单的错误。我对 product_id 使用了 GET,但是在包含时,它当然没有设置,所以它会崩溃,但不会因为它是包含而出错。只需发送 GET 变量即可修复。

于 2013-05-01T09:07:33.007 回答