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>
2
  • 3
    Ajax. Commented Jan 29, 2014 at 9:48
  • you can do that with AJAX Commented Jan 29, 2014 at 9:49

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

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
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
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
If you provide what you tried so far in your question, stackoverflow family helps you better
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>.");

5 Comments

sorry idont know ajax
Try the second answer
try to do that but no idea
Are you programming in PHP?
im student of web development

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.