将数据从javascript发送到mysql数据库

前端之家收集整理的这篇文章主要介绍了将数据从javascript发送到mysql数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个小点击计数器.我希望在 mysql表中包含每个点击.有人可以帮忙吗?
  1. var count1 = 0;
  2. function countClicks1() {
  3. count1 = count1 + 1;
  4. document.getElementById("p1").innerHTML = count1;
  5. }
  6.  
  7.  
  8. document.write('<p>');
  9. document.write('<a href="javascript:countClicks1();">Count</a>');
  10. document.write('</p>');
  11.  
  12. document.write('<p id="p1">0</p>');

万一有人想看看我做了什么:

  1. var count1 = 0;
  2. function countClicks1() {
  3. count1 = count1 + 1;
  4. document.getElementById("p1").innerHTML = count1;
  5. }
  6. function doAjax()
  7. $.ajax({
  8. type: "POST",url: "PHPfile.PHP",data: "name=name&location=location",success: function(msg){
  9. alert( "Data Saved: " + msg );
  10. }
  11. });
  12. }
  13.  
  14. document.write('</p>');
  15. document.write('<a href="javascript:countClicks1(); doAjax();">Count</a>');
  16. document.write('</p>');
  17. document.write('<p id="p1">0</p>');

这是PHPfile.PHP,用于测试目的将数据写入txt文件

  1. <?PHP
  2. $name = $_POST['name'];
  3. $location = $_POST['location'];
  4. $myFile = "test.txt";
  5. $fh = fopen($myFile,'w') or die("can't open file");
  6. fwrite($fh,$name);
  7. fwrite($fh,$location);
  8. fclose($fh);
  9. ?>

解决方法

您的问题中定义的JavaScript无法直接与MysqL一起使用.这是因为它不在同一台计算机上运行.

JavaScript在客户端(浏览器)中运行,数据库通常存在于服务器端.您可能需要使用中间服务器端语言(如PHP,Java,.Net或像Node.js这样的服务器端JavaScript堆栈)执行查询.

这里有一个关于如何编写一些将PHP,JavaScript和MysqL绑定在一起的代码,以及在浏览器和服务器上运行的代码的教程:

http://www.w3schools.com/php/php_ajax_database.asp

这里是该页面代码.它不完全符合您的方案(它执行查询,并且不将数据存储在数据库中),但它可能有助于您开始了解您需要的交互类型,以使其工作.

特别要注意这篇文章中的这些代码.

JavaScript的位:

  1. xmlhttp.open("GET","getuser.PHP?q="+str,true);
  2. xmlhttp.send();

PHP代码的位

  1. MysqL_select_db("ajax_demo",$con);
  2. $result = MysqL_query($sql);
  3. // ...
  4. $row = MysqL_fetch_array($result)
  5. MysqL_close($con);

此外,在您了解了这种代码的工作原理之后,我建议您使用use the jQuery JavaScript library to do your AJAX calls.与内置的AJAX支持相比,它更加干净,更容易处理,您不必编写特定于浏览器的代码,因为jQuery具有内置的跨浏览器支持.这是the jQuery AJAX API documentation页面.

文章代码

HTML / Javascript代码

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function showUser(str)
  5. {
  6. if (str=="")
  7. {
  8. document.getElementById("txtHint").innerHTML="";
  9. return;
  10. }
  11. if (window.XMLHttpRequest)
  12. {// code for IE7+,Firefox,Chrome,Opera,Safari
  13. xmlhttp=new XMLHttpRequest();
  14. }
  15. else
  16. {// code for IE6,IE5
  17. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  18. }
  19. xmlhttp.onreadystatechange=function()
  20. {
  21. if (xmlhttp.readyState==4 && xmlhttp.status==200)
  22. {
  23. document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
  24. }
  25. }
  26. xmlhttp.open("GET",true);
  27. xmlhttp.send();
  28. }
  29. </script>
  30. </head>
  31. <body>
  32.  
  33. <form>
  34. <select name="users" onchange="showUser(this.value)">
  35. <option value="">Select a person:</option>
  36. <option value="1">Peter Griffin</option>
  37. <option value="2">Lois Griffin</option>
  38. <option value="3">Glenn Quagmire</option>
  39. <option value="4">Joseph Swanson</option>
  40. </select>
  41. </form>
  42. <br />
  43. <div id="txtHint"><b>Person info will be listed here.</b></div>
  44.  
  45. </body>
  46. </html>

PHP代码

  1. <?PHP
  2. $q=$_GET["q"];
  3.  
  4. $con = MysqL_connect('localhost','peter','abc123');
  5. if (!$con)
  6. {
  7. die('Could not connect: ' . MysqL_error());
  8. }
  9.  
  10. MysqL_select_db("ajax_demo",$con);
  11.  
  12. $sql="SELECT * FROM user WHERE id = '".$q."'";
  13.  
  14. $result = MysqL_query($sql);
  15.  
  16. echo "<table border='1'>
  17. <tr>
  18. <th>Firstname</th>
  19. <th>Lastname</th>
  20. <th>Age</th>
  21. <th>Hometown</th>
  22. <th>Job</th>
  23. </tr>";
  24.  
  25. while($row = MysqL_fetch_array($result))
  26. {
  27. echo "<tr>";
  28. echo "<td>" . $row['FirstName'] . "</td>";
  29. echo "<td>" . $row['LastName'] . "</td>";
  30. echo "<td>" . $row['Age'] . "</td>";
  31. echo "<td>" . $row['Hometown'] . "</td>";
  32. echo "<td>" . $row['Job'] . "</td>";
  33. echo "</tr>";
  34. }
  35. echo "</table>";
  36.  
  37. MysqL_close($con);
  38. ?>

猜你在找的JavaScript相关文章