I am using tinymce on a cms and I want to be able to have a dropdown menu of all pages on the site (stored in a db) to choose from when adding a link but all I am getting is an emtpy set of values.

I am using the Link plugin and have the following in my tinymce init:

tinymce.init({
            selector: "textarea#description, textarea#answer",
            menubar: false,
            width: '58%',
            height: 200,
            plugins: ["link","paste"],
            paste_as_text: true,
            toolbar: "undo redo | bold | bullist numlist | link unlink",
            link_list: "../inc/internal-links-list.php"
        });

I am not sure if the url for the link list is correct (it is relative to the file that this code is in).

And in my php:

<?php
require_once("core.php");

$objPage = new Pages;
$pages = $objPage->get_all();
$urls = array();

foreach ($pages as $page) {
    $urls[] = array('title' => $page['page'], 'value' => $page['url']);
}

return json_encode($urls);

// Also tried echo json_encode($urls);

// Below is something I have tried to remove the double quotes around the keys to see if that was the problem but no
//$array_final = json_encode($urls);
//$array_final = preg_replace('/"([a-zA-Z_]+[a-zA-Z0-9_]*)":/','$1:',$array_final);
//echo $array_final;

?>

When I inspect the dropdown element I basically see a lot of empty divs, like so:

<div id="mce_137" class="mce-menu-item mce-stack-layout-item" tabindex="-1" role="menuitem"><i class="mce-ico mce-i-none"></i>&nbsp;<span id="mce_137-text" class="mce-text"></span></div>
<i class="mce-ico mce-i-none"></i>
&nbsp;
<span id="mce_137-text" class="mce-text"></span>
</div>

Can anyone shed any light please?

change:

return json_encode($urls);

to:

echo json_encode($urls);

Thank you but as I have written in line 14 of my php code, I have also tried echo and that didn't work either

What does the json data look like that PHP is returning?

It looks like (shortened:

[{title:"About",value:"about-united-diesel"},{title:"About Viezu",value:"vehicle-tuning-remapping\/about-viezu"},{title:"Contact",value:"contact-united-diesel"},{title:"Delivery",value:"delivery"}]

As I say I also tried preg_replace to remove the double quotes from the keys but that didn't work either

There is no problem with the json that is being returned, so the problem must be elsewhere. I have basically the same setup in a test environment and I have no problems with seeing the drop down of links. I can even take your json and drop it in and it works fine here for me.

Maybe something else interfering with this in the markup? Some other js? Do you have a test page up somewhere that can be reached publicly?

EDIT: I am also suspecting that the path to your php file is not being resolved by the js file. What happens if you put in the full path to the PHP file?

OK, thank you for your help and for testing for me. I am going to remove all the code and try to run it separately and go from there as it may be that some other js is causing the problem (I don't have it public at the moment). I also thought about the path to the file but I have tried the full path and that doesn't work either.

I will let you know how I get on.

One other thing that just popped into my head, and this may sound dumb but, you have the call for tinymce.init inline on the page that has the form? I'm not sure that it would work in a separate JS file.

Yes it is in the page

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.