Saving actual javascript code into a database

I don't know how to save javascript code to a database.

Page 1 of 1

14 Replies - 4583 Views - Last Post: 08 March 2010 - 08:39 AM Rate Topic: -----

#1 heavensbest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-March 10

Saving actual javascript code into a database

Posted 04 March 2010 - 03:00 PM

I want to be able to save my google analytics code to a database using a textarea.

So if my analytics was:
<script type="text/javascript">
blah blah
</script>



How could I insert that into my database using php. I know it can be done because MODx and phpMyAdmin will allow inserting javascript into the database using a textarea.
Is This A Good Question/Topic? 0
  • +

Replies To: Saving actual javascript code into a database

#2 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Saving actual javascript code into a database

Posted 05 March 2010 - 01:39 AM

Do you know how to code in PHP?
Was This Post Helpful? 0
  • +
  • -

#3 Kein  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 36
  • Joined: 04-March 10

Re: Saving actual javascript code into a database

Posted 05 March 2010 - 02:05 AM

so wheres the problem?

input your google analytics code into the database (witch is a bad idea), and every time you need it create a query
select google_analytics_code from some_table

Was This Post Helpful? 1
  • +
  • -

#4 heavensbest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-March 10

Re: Saving actual javascript code into a database

Posted 05 March 2010 - 08:20 AM

The problem I'm having is getting the javascript from the form.

When I do this after submitting the form:

<?php $var = $_POST['textarea_name']; ?>


All I get is this:

\r\n


or even just a blank.

Do I need to edit the javascript code I'm trying to insert into the database using javascript before submitting the form? I would think I could just use PHP on the next page, like phpMyAdmin and MODx seems to do.


I mean this forum allows me to do the exact thing I want to do. Because these posts are stored in a database and I can put this in my entry:

<script type="text/javascript"> 
blah blah 
</script>


And even this:

<script type="text/javascript">
blah blah
</script>

This post has been edited by heavensbest: 05 March 2010 - 08:23 AM

Was This Post Helpful? 0
  • +
  • -

#5 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Saving actual javascript code into a database

Posted 05 March 2010 - 08:27 AM

heavensbest, don't down vote people just for asking a question to try and help you. They have no idea how much PHP you know or don't know and we don't just provide code for people here. Just because you didn't get what you wanted doesn't meant they weren't "helpful", they were trying to help with the very limited information you gave them.
Was This Post Helpful? 1
  • +
  • -

#6 heavensbest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-March 10

Re: Saving actual javascript code into a database

Posted 05 March 2010 - 08:30 AM

Do you know how to do what I'm talking about?
Was This Post Helpful? 0
  • +
  • -

#7 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Saving actual javascript code into a database

Posted 05 March 2010 - 08:53 AM

I sure do. Since it doesn't appear you're very familiar with PHP, I'd suggest reading our tutorial here at dream.in.code on Inserting and Updating Database Records in PHP

You should be able to adapt the code and information from that tutorial to insert anything you want in to a database. You may have to (and probably should) use htmlspecialchars() and (if you need it to actually execute) htmlspecialchars_decode() to store and retrieve your javascript so you never actually output or save dangerous code to your database.
Was This Post Helpful? 0
  • +
  • -

#8 heavensbest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-March 10

Re: Saving actual javascript code into a database

Posted 05 March 2010 - 03:23 PM

I guess I'm not making sense.


So I have a form with a textarea.

I put this in the textarea:
<b>stuff</b>


Hit submit and it shows up on the next page when I echo it.


I put this in the textarea:
<script>stuff</script>


Hit submit and nothing shows up on the next page when I echo it.
I even viewed the source code and there wasn't anything.


The issue isn't with mySQL or inserting it into the database, the issue is that when there is a script tag (<script></script>) in the textarea, the script tag and everything inside it doesn't seem to go to the next page after hitting submit.

Maybe this isn't a php question, maybe it's a "HTML Form" question.

I know it's possible because when I hit "Post" on this forum, my script tags don't dissappear, even if they aren't in the [ code ] brackets.

This post has been edited by heavensbest: 05 March 2010 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Saving actual javascript code into a database

Posted 05 March 2010 - 03:25 PM

Wow, I'm not sure how much clearer skyhawk133 could have explained it with this statement

Quote

You may have to (and probably should) use htmlspecialchars() and (if you need it to actually execute) htmlspecialchars_decode() to store and retrieve your javascript so you never actually output or save dangerous code to your database.


The only way you're going to get it to show and work right is to follow those instructions.
Was This Post Helpful? 0
  • +
  • -

#10 heavensbest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-March 10

Re: Saving actual javascript code into a database

Posted 05 March 2010 - 03:29 PM

So I do this:

<?php
$var = htmlspecialchars( $_POST['textarea_name'] );

echo $var;

?>



And it should show my javascript on the page?
Was This Post Helpful? 0
  • +
  • -

#11 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Saving actual javascript code into a database

Posted 05 March 2010 - 03:35 PM

You're going to need to use htmlspecialchars_decode to decode it so it will run as actual Javascript
Was This Post Helpful? 0
  • +
  • -

#12 heavensbest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-March 10

Re: Saving actual javascript code into a database

Posted 05 March 2010 - 04:07 PM

So here's the problem.
I decided to see if it was an issue with modx, since that's what I use to manage me site.

I created two files, one for the form and one for the form information to be sent to.
The form worked and sending script tags through it worked.

If some one knows why modx wouldn't allow it to work, that would be helpful.
Was This Post Helpful? 0
  • +
  • -

#13 Kein  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 36
  • Joined: 04-March 10

Re: Saving actual javascript code into a database

Posted 05 March 2010 - 04:26 PM

this is just unbelivable, how cant you understand ?


Saveing the code
<?

 if(isset($_POST['data'])){
  $save = htmlspecialchars($_POST['data']);
  $save = str_replace("'","<q>",$save);
  mysql_query("insert into table (script) values('{$save}')");
 }

echo <<<HTML
<form method="post" action="">
<textarea name="data"></textarea>
</form>
HTML;
?>



Outputing the code
$sql = mysql_query("select script from table");
$data = mysql_fetch_array($sql);
$data = str_replace("<q>","'",$data['script']);
$data = htmlspecialchars_decode($data);

echo <<<HTML
<script type="text/javascript">
{$data}
</script>
HTML;
?>



Thank you.
Was This Post Helpful? 1
  • +
  • -

#14 Lang14  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 29
  • Joined: 15-August 09

Re: Saving actual javascript code into a database

Posted 06 March 2010 - 12:59 PM

The reason why you don't see the Javascript in the output is because you do not see Javascript output directly on the page!

Why not try submitting:
<script>
document.write("Yo.");
</script>


Was This Post Helpful? 0
  • +
  • -

#15 heavensbest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-March 10

Re: Saving actual javascript code into a database

Posted 08 March 2010 - 08:39 AM

Thanks for helping.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1