1

I have a script that creates a basic table layout structure and I'm trying to use AJAX with jQuery to input the output of that script into a TinyMCE textarea. I wrote the script in PHP and it works perfectly well if I open up the script directly.

What I'm trying to do is let the user choose something from a dropdown (in this case, a product) and it return a table filled with the relevant data for that product. I use the following script to insert it into the 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' does return but when it's placed in the tinyMCE textarea, it shows the following error(the script carries on and outputs the table):

Warning: Invalid argument supplied for foreach() in /home/sites/very-dev.co.uk/public_html/cms/includes/ajax.php on line 71Warning: Invalid argument supplied for foreach() in /home/sites/very-dev.co.uk/public_html/cms/includes/ajax.php on line 100

Those two foreachs which error are: foreach($product_data as $key=>$product): and 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>

It's probably quite a simple problem that I'm not seeing, any help on this one?

2
  • Try to build a string with all html output and at the end echo that string, instead of output the html directly like you have shows in above code. This doesn't look like AJAX problem. Commented May 1, 2013 at 7:43
  • Also you can try by putting it to a simple div first with $('#div_id').html(data) and see if it working fine. Commented May 1, 2013 at 7:45

1 Answer 1

1

After looking at the code more carefully, it was a simple error. I was using a GET for the product_id but upon include that of course isn't set so it's breaking down but doesn't error out because of it being an include. Fixed simply by sending the GET variable along.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.