We would like to serve javascript from our database.

We assumed it would be like serving html but its not working.

What we have is a small news ticker and we would like to use a call to our database via php to serve this ticker, so we put the ticker information into the database and put the call in our php, we can see its getting it from the database but its not functioning. If we add the javascript right into our code it works fine.

Can someone suggest a way to make this work?

Currently we are using

<?php print "$news1"; ?>

and we tried

<?php echo "$news1"; ?>

but its not working.

The ticker we are trying to use is here http://dynamicdrive.com/dynamicindex2/fadescroll.htm

Any thoughts and help would be greatly appreciated. Thank you.

Member Avatar for diafol

This post doesn't really help with regard to the js you're trying to insert.

Get the bare script and insert it into the DB via php code (just set up a temporary php file for this):

<?php
$js = "..."; //insert your raw js here BUT the js string has to be escaped (backslashed) with regard to double quotes OR you could enclose in single quotes and escape those. 

//mysql connection details here...

$js = mysql_real_escape_string(<script type="text/javascript">
$query = mysql_query("INSERT INTO table SET field='$js'");
?>

Your js should now be in a suitable format in the DB. Just echo it back out where it's required.

HOWEVER, I would suggest that your js - if you don't want to put it in an external js file, place it in a php include file. The DB method is a bit messy. Storing code in a DB is not usually a problem, but just seems a bit of a fiddle - especially if you need to edit it (get at it easily).

I think you miss understood, we are trying to show the javascript from the database not put it there, I can put the javascript into the database via phpadmin.

The idea is to be able to show the code on multiple pages and only have to update one thing instead of multiple pages.

Member Avatar for diafol

Sorry, line 6 of previous post went a little weird:

$js = mysql_real_escape_string(<script type="text/javascript">

should have been something like:

$js = mysql_real_escape_string($js);

ANYWAY:

No I didn't misunderstand, your JS must be formatted correctly in the DB or it won't be retrieved and echoed correctly. I was simply making the point that pasting raw text (js in this case) into a textfield in phpmyadmin isn't straightforward and that it should be 'sanitized'. This is what the mysql_real_escape_string() function does for you.

However, I still maintain that a php include file containing the js may be easier to maintain.

For example:

file1.php
=========

...
<?php
include("js.php");
$js = getScript(3); //this is the only bit you need to change in pages (if at all)
?>
...
<head>
...
<?php echo $js;?>
...
</head>

js.php
======

<?php
function getScript($int){
  switch($int){
   case 1:
     $output = "<script>...</script>";
     break;
   case 2:
     $output = "<script>...</script>";
     break;
   case 3:
     $output = "<script>...</script>";
     break;
   case 4:
     $output = "<script>...</script>";
     break;
   ...
  }
  return $output;
}
?>

The above is just a quick example of how you can use conditionals to serve different js scripts to different pages. The way you implement this is a matter of preference to be honest. This is off the top of my head, but I would probably use a 'template' method were I producing this for a large site.

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.