jsTree - PHP - MySQL

1,745 views
Skip to first unread message

P.Dubkov

unread,
Jan 21, 2010, 2:09:44 AM1/21/10
to jsTree, dub...@satro.ru
Good day!
Beautiful and useful plugin, thanks very much.

But there is the question.
Keep the tree in MySQL and editing need to send there data. How do you
manage this? Long tormented himself - now writing to you ...
If possible give an example, it is wonderful!

I would be very happy to answer.
Thank you!

vakata

unread,
Jan 21, 2010, 5:03:23 AM1/21/10
to jsTree
This is a rather regular question here - there have been 2 or 3
threads about this one. You have two options:
1) save the whole tree to a database at some point (user clicks save,
or some other event) which is done using the .get method which will
return the tree in whatever format you need it. And then just post
that string (remember to convert JSON to a string if JSON is what you
are using) to the server, where you can use PHP to save the string to
the database.

2) Keep persisting changes whenever the user changes something - in
that case - use the callbacks (oncreate, onrename, ondelete, onmove,
etc) and in each of those callbacks make an AJAX call to the server
notifying it about the user action. Then the PHP can modify the
database as needed.

Kindest regards,
Ivan

P.Dubkov

unread,
Jan 21, 2010, 9:46:53 AM1/21/10
to jsTree
I all understand it. A problem that I the beginner in jquery and
ajax... Therefore also has come to you behind advice.
I use this code, but the data in a script is not transferred...

...
callback : {
onrename : function (NODE) {
NODE.id = id;
$.ajax( {
type: "POST",
url: "ajax_love.php",
data: 'file=' + id + '&rate=' + id,
cache: false,
success: function(html)
{
parent.html(html);
parent.fadeIn(300);
}
});
}
}
....


ajax_love.php:
include("config.php");

$f=$_POST['file'];
$r=0;

$sql_in = "insert into images (img_url,love) values ('$f','$r')";
mysql_query( $sql_in);
....

I so have understood sending does not pass. Or I in general all do not
that?

Ramunas Kiupelis

unread,
Jan 21, 2010, 10:10:07 AM1/21/10
to jst...@googlegroups.com
In the code you have an error
>>   NODE.id = id;
this have to be   
 
var id =  NODE.id;
also with date i think that you data argument can be made    
 
data : {'file':id, 'rate':id},
 
insted of using "data: 'file=' + id + '&rate=' + id"
 
 
 
R.K.
 
--
You received this message because you are subscribed to the Google Groups "jsTree" group.
To post to this group, send email to jst...@googlegroups.com.
To unsubscribe from this group, send email to jstree+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jstree?hl=en.




P.Dubkov

unread,
Jan 21, 2010, 12:24:49 PM1/21/10
to jsTree
Thanks for reply, but....

Data and is not transferred...

Has already omitted hands. The same script of sending/reception of the
data through ajax without jstree works perfectly...

I do not understand in what an error.

Has reduced a script on a maximum
Here a code:
$(function () {
$("#tree").tree({
ui : { theme_name : "default2" },
types : {
"vis" : {
icon : { image : "design/vis.gif" }
},
"unvis" : {
icon : { image : "design/unvis.gif" }
},
"default" : {
renameable : true,
deletable : true,
creatable : true,
draggable : false
}
},
plugins : {
cookie : { prefix : "jstree_" },
contextmenu :
{
items :
{
create : { label : "Создать" },
rename : { label : "Переименовать" },
remove : { label : "Удалить" },
my_act :
{
label : "Сменить видимость",
icon : "vis",
separator_before : true
}
}
}
},
callback : {
onrename : function (NODE) {
var id = NODE.id;


$.ajax( {
type: "POST",
url: "ajax_love.php",

data: { 'file':id },


cache: false,
success: function(html)
{
parent.html(html);
parent.fadeIn(300);
}

});
},
ondblclk : function (NODE) {
document.location.href = $(NODE).children("a:eq(0)").attr
("href");
},

}
});
});

TREE:

<div id="tree">
<ul>
<li rel='unvis' id='6'><a href='123'><ins>&nbsp;</ins>English version</
a></li>
<li rel='unvis' id='1'><a href='123'><ins>&nbsp;</ins>Русская версия</
a><ul>
<li rel='vis' id='32'><a href='#'><ins>&nbsp;</ins>Company profile</
a></li>
<li rel='unvis' id='17'><a href='123'><ins>&nbsp;</ins>On-line опросы</
a></li>
<li rel='unvis' id='61'><a href='123'><ins>&nbsp;</ins>Акции С-П
Групп</a></li>
<li rel='unvis' id='54'><a href='123'><ins>&nbsp;</ins>Архив рассылки</
a></li>

<li rel='unvis' id='20'><a href='123'><ins>&nbsp;</ins>Действующие
акции</a></li>
<li rel='unvis' id='62'><a href='123'><ins>&nbsp;</ins>ДиВиаРиум</a></
li>
<li rel='vis' id='56'><a href='#'><ins>&nbsp;</ins>Доставка</a><ul>
<li rel='vis' id='58'><a href='#'><ins>&nbsp;</ins>Доставка по Москве</
a></li>
<li rel='vis' id='57'><a href='#'><ins>&nbsp;</ins>Доставка по России</
a></li>
<li rel='vis' id='60'><a href='#'><ins>&nbsp;</ins>Обратная связь</a></
li>
<li rel='vis' id='59'><a href='#'><ins>&nbsp;</ins>Получение грузов</
a></li>
</ul>

....

ajax_love.php
$f=$_POST['file'];
$r=0;

$sql_in = "insert into images (img_url,love) values ('$f','$r')";
mysql_query( $sql_in);


Help please!

Ramunas Kiupelis

unread,
Jan 21, 2010, 12:30:28 PM1/21/10
to jst...@googlegroups.com
use this for data send
      data: ({file : id}),
R.K.

John Arrowwood

unread,
Jan 21, 2010, 1:00:56 PM1/21/10
to jst...@googlegroups.com
Create a sample page that is up on the Internet and send out a URL.  Maybe if we can look at it in context, we may be able to figure out where things are going wrong.

--
You received this message because you are subscribed to the Google Groups "jsTree" group.
To post to this group, send email to jst...@googlegroups.com.
To unsubscribe from this group, send email to jstree+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jstree?hl=en.






--
John Arrowwood
John (at) Irie (dash) Inc (dot) com
John (at) Arrowwood Photography (dot) com
John (at) Hanlons Razor (dot) com
--
http://www.irie-inc.com/
http://arrowwood.blogspot.com/

P.Dubkov

unread,
Jan 21, 2010, 4:21:44 PM1/21/10
to jsTree
Unfortunately it has not helped...
The script of sending of the data works if not to use a tree.
Inquiries all too work.
I have absolutely got confused...

John Arrowwood

unread,
Jan 21, 2010, 4:54:15 PM1/21/10
to jst...@googlegroups.com
We can't help you if you don't give us something to look at. 

--
You received this message because you are subscribed to the Google Groups "jsTree" group.
To post to this group, send email to jst...@googlegroups.com.
To unsubscribe from this group, send email to jstree+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jstree?hl=en.



P.Dubkov

unread,
Jan 22, 2010, 1:06:22 AM1/22/10
to jsTree
Ur! I have found an error!
Error awfully silly. Because of the carelessness I have forgotten to
add to these two files a file config.php in which the given
connections are stored... :((( The Silly and banal error... Forgive.

Thanks you huge for the invaluable help!!!
All that here has been told - works, given-are transferred!!!

2 ivan:
In the first I see so worked plug-in for jquery. Thanks big for fine
operation!

P.Dubkov

unread,
Jan 22, 2010, 8:33:35 AM1/22/10
to jsTree
There were still problems:

At creation of a new unit through the shortcut menu, the unit forms,
but renaming function does not work.
If separately to rename this unit - all is excellent.
What here it is possible to make?

And still:
Has created a unit - the child flowing, I try to create still same and
it becomes in flowing though they for both should have a same
parent...

Very much I hope for your help!

code:

oncreate : function
(NODE, REF_NODE, TYPE, TREE_OBJ, RB) {
var pi = REF_NODE.id;
var i = NODE.id;
var parent = $("#txt_block");
alert(i); alert(pi);
$.ajax( {
type: "POST",
url: "ajax.php",
data: ({ id:i, par_id:pi, act:'create' }),


cache: false,
success: function(html)
{
parent.html(html);

parent.fadeIn(400);
}
});
},
onrename : function (NODE, TREE_OBJ, RB) {
var i = NODE.id;
var nam = TREE_OBJ.get_text(NODE);
var parent = $("#txt_block");
alert(i);
alert(nam);
$.ajax( {
type: "POST",
url: "ajax.php",
data: ({ id:i, name:nam, act:'rename' }),


cache: false,
success: function(html)
{
parent.html(html);

parent.fadeIn(400);
}

});
},
ondelete : function (NODE) {
var id = NODE.id;
var parent = $("#txt_block");
$.ajax( {
type: "POST",
url: "ajax.php",
data: ({ id:id, act:'delete' }),


cache: false,
success: function(html)
{
parent.html(html);

parent.fadeIn(400);
}

});
}

P.Dubkov

unread,
Jan 22, 2010, 9:31:31 AM1/22/10
to jsTree
The problem with creation of sites has dared. It here has already been
disassembled.

There was a problem with renaming ущла after its creation...
Help please!....

P.Dubkov

unread,
Jan 22, 2010, 9:32:22 AM1/22/10
to jsTree
Problem with creation of sites has solved - it here has been already
disassembled...

P.Dubkov

unread,
Jan 23, 2010, 5:50:58 AM1/23/10
to jsTree
Really nobody can help? ((((

Ramunas Kiupelis

unread,
Jan 23, 2010, 9:00:31 AM1/23/10
to jst...@googlegroups.com
try debug with mozilla firebug,
Check main problems if id is sending and text correct etc.

R.K.

On Sat, Jan 23, 2010 at 12:50, P.Dubkov <dub...@satro.ru> wrote:
Really nobody can help? ((((

P.Dubkov

unread,
Jan 25, 2010, 2:58:30 AM1/25/10
to jsTree
Good day!
I decided to rename the problem after the creation element!

1. In ajax.php after the creation of know through mysql_insert_id ()
ID of the new element.
2. Sent back through the html in <div style="display: none;">
3. Just give the new item ID from a MySQL database
4. Function renaming works fine!
Everything!

oncreate : function(NODE, REF_NODE, TYPE, TREE_OBJ, RB) {
if(TYPE != "inside") {
TYPE="inside";
REF_NODE = $(NODE).parents("li:eq(0)").get(0);
}
var pi = REF_NODE.id;


var parent = $("#txt_block");

var name = $(NODE).value;
$.ajax( {
type: "POST",
url: "ajax.php",
data: ({ par_id:pi, name:name, act:'create' }),


cache: false,
success: function(html)
{
parent.html(html);

parent.fadeIn(400);
var atr = $('#nomer').text();
$(NODE).attr('id',atr);
}
});
}

ajax.php:

<?PHP
...
switch ($act) {
case 'create':
$sql_in = "insert into sektors (parent_id,visible) values
('$parent_ind','0')";
mysql_query( $sql_in);
$index = mysql_insert_id();
?>
<p style="color: green !important;"> Created | id=<?
php echo $index; ?></p> <div id="nomer" style="display:none;"><?php
echo $index; ?></div>
<?
break;
...

I thank everyone for their support!
Excellent plugin with great functionality and support!

guest...@mail.ru

unread,
Jan 25, 2010, 1:27:21 PM1/25/10
to jsTree
Thanks for excellent plugin.

But how can I use it ?

On 21 янв, 15:03, vakata <ivan.bozha...@gmail.com> wrote:
> This is a rather regular question here - there have been 2 or 3
> threads about this one. You have two options:
> 1) save the whole tree to a database at some point (user clicks save,
> or some other event) which is done using the .get method which will
> return the tree in whatever format you need it. And then just post
> that string (remember to convert JSON to a string if JSON is what you
> are using) to the server, where you can use PHP to save the string to
> the database.

Could I save all tree in xml-code ?

Thank you!

John Arrowwood

unread,
Jan 25, 2010, 1:52:58 PM1/25/10
to jst...@googlegroups.com
Are you populating the tree using xml?  If so, calling .get() will return it in xml, which you can then save.  Otherwise, there are things you can do...

--
You received this message because you are subscribed to the Google Groups "jsTree" group.
To post to this group, send email to jst...@googlegroups.com.
To unsubscribe from this group, send email to jstree+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jstree?hl=en.

Message has been deleted

guest...@mail.ru

unread,
Jan 26, 2010, 12:52:40 PM1/26/10
to jsTree
i try
$(function () {
$("#xml_n").tree({
data : {
type : "xml_nested",
opts : {
url : "basic_xml_nested_data.xml"
}
}
});
});
var xmlCode=($("#xml_n").get());
but xmlCode=[object HTMLDivElement]

John Arrowwood

unread,
Jan 26, 2010, 2:04:54 PM1/26/10
to jst...@googlegroups.com
You called .get() on the wrong thing.  You need to get the instance of the tree (there are several ways of doing this, if you don't know how, consult the docs), and call .get() on that.  What you did was called .get() on the jQuery instance returned by searching for your container.  Naturally, that won't do what you want it to do.

On Tue, Jan 26, 2010 at 9:01 AM, guest...@mail.ru <guest...@mail.ru> wrote:
if i use


       $(function () {
               $("#xml_n").tree({
                       data : {
                               type : "xml_nested",
                               opts : {
                                       url : "basic_xml_nested_data.xml"
                               }
                       }
               });
       });

       var xmlCode=($("#xml_n").get());

it`s only return <b>[object HTMLDivElement]</b>


On 25 янв, 23:52, John Arrowwood <jarro...@gmail.com> wrote:
> Are you populating the tree using xml?  If so, calling .get() will return it
> in xml, which you can then save.  Otherwise, there are things you can do...
>
> On Mon, Jan 25, 2010 at 10:27 AM, guestlo...@mail.ru <guestlo...@mail.ru>wrote:
>
>
>
>
>
> > Thanks for excellent plugin.
>
> > But how can I use it ?
>
> > On 21 янв, 15:03, vakata <ivan.bozha...@gmail.com> wrote:
> > > This is a rather regular question here - there have been 2 or 3
> > > threads about this one. You have two options:
> > > 1) save the whole tree to a database at some point (user clicks save,
> > > or some other event) which is done using the .get method which will
> > > return the tree in whatever format you need it. And then just post
> > > that string (remember to convert JSON to a string if JSON is what you
> > > are using) to the server, where you can use PHP to save the string to
> > > the database.
>
> > Could I save all tree in xml-code ?
>
> > Thank you!
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "jsTree" group.
> > To post to this group, send email to jst...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > .
> > For more options, visit this group at
> >http://groups.google.com/group/jstree?hl=en.
>
> --
> John Arrowwood
> John (at) Irie (dash) Inc (dot) com
> John (at) Arrowwood Photography (dot) com
> John (at) Hanlons Razor (dot) com
> --http://www.irie-inc.com/http://arrowwood.blogspot.com/

--
You received this message because you are subscribed to the Google Groups "jsTree" group.
To post to this group, send email to jst...@googlegroups.com.
To unsubscribe from this group, send email to jstree+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jstree?hl=en.

guest...@mail.ru

unread,
Jan 26, 2010, 2:56:31 PM1/26/10
to jsTree
var yourTree = jQuery.tree.create();
yourTree.init({

data : {
type : "xml_nested",
opts : {
url : "basic_xml_nested_data.xml"
}
}
})
but it also wrong...

On 27 янв, 00:04, John Arrowwood <jarro...@gmail.com> wrote:
> You called .get() on the wrong thing.  You need to get the instance of the
> tree (there are several ways of doing this, if you don't know how, consult
> the docs), and call .get() on that.  What you did was called .get() on the
> jQuery instance returned by searching for your container.  Naturally, that
> won't do what you want it to do.
>

> > <jstree%2Bunsu...@googlegroups.com<jstree%252Bunsubscribe@googlegroups.­com>

John Arrowwood

unread,
Jan 26, 2010, 3:45:15 PM1/26/10
to jst...@googlegroups.com
wrong how?  you called yourTree.get() and got what?

To unsubscribe from this group, send email to jstree+un...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/jstree?hl=en.




--
John Arrowwood
John (at) Irie (dash) Inc (dot) com
John (at) Arrowwood Photography (dot) com
John (at) Hanlons Razor (dot) com

guest...@mail.ru

unread,
Jan 27, 2010, 4:04:44 AM1/27/10
to jsTree
for this code
function getXML() {

var yourTree = jQuery.tree.create();
yourTree.init({
data : {
type : "xml_nested",
opts : {
url : "basic_xml_nested_data.xml"
}
}
});
document.getElementById('getXMLstr').value=yourTree.get();
}

i have an error
error: (this[0].ownerDocument || this[0]).createDocumentFragment is
not a function
source: http://127.0.0.1:4080/tmp/jstree/lib/jquery.js
string: 12

On Jan 27, 1:45 am, John Arrowwood <jarro...@gmail.com> wrote:
> wrong how?  you called yourTree.get() and got what?
>

> > > > <jstree%2Bunsu...@googlegroups.com<jstree%252Bunsubscribe@googlegroups.­com>

John Arrowwood

unread,
Jan 27, 2010, 9:31:18 AM1/27/10
to jst...@googlegroups.com
You are missing a parameter to your call to .init().  Unfortunately, the missing parameters is not mentioned in the docs (Are you listening, Ivan?). 

            init : function(elem, conf) {

The first parameter to init should be the element that is the container for the tree.  That is why you got the error you got.

To unsubscribe from this group, send email to jstree+un...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/jstree?hl=en.




--
John Arrowwood
John (at) Irie (dash) Inc (dot) com
John (at) Arrowwood Photography (dot) com
John (at) Hanlons Razor (dot) com

guest...@mail.ru

unread,
Jan 27, 2010, 1:07:55 PM1/27/10
to jsTree
Great... it`s really works

Thanks a lot.

On 27 янв, 19:31, John Arrowwood <jarro...@gmail.com> wrote:
> You are missing a parameter to your call to .init().  Unfortunately, the
> missing parameters is not mentioned in the docs (Are you listening, Ivan?).
>
>             init : function(elem, conf) {
>
> The first parameter to init should be the element that is the container for
> the tree.  That is why you got the error you got.
>

vakata

unread,
Jan 27, 2010, 4:44:08 PM1/27/10
to jsTree
John, note taken :) I will definitely do my best for the 1.0 docs :)
(well, at least in the 1.0 there is no init method ;) )

guest...@mail.ru

unread,
Feb 1, 2010, 4:22:21 AM2/1/10
to jsTree
Can I save\restore tree from xml like this

<item id="pxml_3" name="hild node 2" link="page.php" title="page
comment"/>


except like
<item id="pxml_3" llink="page.php" title="page comment">
<content><name><![CDATA[Child node 2]]></name></content>
</item>

John Arrowwood

unread,
Feb 1, 2010, 10:42:18 AM2/1/10
to jst...@googlegroups.com
My advice would be to use an XML formatting library on the XML after it has been extracted.  The tree does not (nor should it) generate pretty xml, so formatting it yourself is your only real option.

--
You received this message because you are subscribed to the Google Groups "jsTree" group.
To post to this group, send email to jst...@googlegroups.com.
To unsubscribe from this group, send email to jstree+un...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/jstree?hl=en.




--
John Arrowwood
John (at) Irie (dash) Inc (dot) com
John (at) Arrowwood Photography (dot) com
John (at) Hanlons Razor (dot) com
Reply all
Reply to author
Forward
0 new messages