简体   繁体   中英

Call a function from Database to make a user defined function

Hi I'm working on a project that allows a user to add his/her own functions to the site, my problem is how can I make those functions work to elaborate further here's an image.

All the functions that are made in this page are stored in a Database

在此处输入图片说明

Once the user toggles the function that he/she made they should work(this the current problem)

在此处输入图片说明

My idea is to put a function in a database, In a way that it can be called so I/user can modify it for the adding function purposes. How can I accomplish this?any suggestion or comment is highly appreciated

you can execute strings with eval() command:

eval("<?php echo $row['jscode']; ?>");

but if you use ajax to get the function and use this:

var s = document.createElement('script');
s.type = 'text/javascript';
var code = 'function lowercase() {}'; //get this from db
try {
  s.appendChild(document.createTextNode(code));
  document.body.appendChild(s);
} catch (e) {
  s.text = code;
  document.body.appendChild(s);
}

you'll ensure that the function is added and ready to execute.

Create a script tag and append it to the body.

var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.text = "alert('your function here')";
$("body").append( script );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM