0

I have problem how to add this javascript data to mysql database?

<script type="text/javascript">    
var count = 0;

function countClicks() {    
 count = count +1 ;    
    document.getElementById("likes").innerHTML = count;     
}    
</script>


<input type="button" value="Like "onclick="javascript:countClicks();" alt="alt text"/>    
<div id="likes">0</div>
Liath
  • 9,913
  • 9
  • 51
  • 81
Imesh
  • 1
  • 5

2 Answers2

0

You need to use mysql-real-escape-string while inserting your js code to mysql db. Assign your code string to a variable and use mysql-real-escape-string while inserting db. Example usage;

HTML:

<form action="save_code.php" method="POST">
    <table>
    <tr>
        <td>Code</td>
        <td><textarea name="code"></textarea></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" name="Save" value="Save"/></td>
    </tr>
    </table>
</form>

PHP: save_code.php

// Comment out this for real test
//$jsCode = $_POST['code'];

//Assign js code string to variable
$jsCode = '<script type="text/javascript">    
var count = 0;

function countClicks() {    
 count = count +1 ;    
    document.getElementById("likes").innerHTML = count;     
}    
</script>


<input type="button" value="Like "onclick="javascript:countClicks();" alt="alt text"/>    
<div id="likes">0</div>';

$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
    or die(mysql_error());

$query = "INSERT INTO your_table(id, code) VALUES('', mysql_real_escape_string($jsCode))";
mysql_query($query);
Hüseyin BABAL
  • 15,400
  • 4
  • 51
  • 73
  • i'm a student.i'm try to do that but not work.i want to add my school project simple like system.if you have any script give me.thanx – Imesh Jan 29 '14 at 12:30
  • If you are trying to get some code without any effort, you are in wrong place. Also you are in wrong way in your life. I suggest you learning something by yourself with researching – Hüseyin BABAL Jan 29 '14 at 12:34
  • tnx,my js code also working but i cant adding js data to mysql database.im so tiered and i want to do that.thanx your honestly i'll do it myself.i'll do – Imesh Jan 29 '14 at 12:43
  • If you provide what you tried so far in your question, stackoverflow family helps you better – Hüseyin BABAL Jan 29 '14 at 13:13
0

You have 2 ways to do:

1- AJAX

You must make a SaveData.php for example and make a ajax Post to this page and then insert in database.

2- JS to PHP

In the same page you do this

Insert into TABLE (count) VALUES (".<script> write(count); </script>.");
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
Ahmsem
  • 1