drexlz0623 2013-06-23 12:09
浏览 16
已采纳

从javascript更新数据库表

I want to update my database after a condition in javascript. What I do now is something like:

 <a href="#" onclick="hello()">Update me</a>

<srcipt>
function hello(smthing)
{
 if(smthing == true)
 {
   <?php //Update database table ?>
 }
}
<script>

I have also tried to update my table according an example in w3schools (http://www.w3schools.com/ajax/ajax_example.asp):

<script>

function hello(smthing)
{
   if(smthing==true)
   {
      var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.open("GET","db_update.php",true);
xmlhttp.send();
   }
}

</script>

In the first case it updates my table even if the variable smthing = false (but if i enter an alert("hi") inside the if, the alert will be displayed only if 'smthing' is true.

In the second case nothing happens. :/

Any help or guidance pls?

  • 写回答

2条回答 默认 最新

  • douxian7808 2013-06-23 12:46
    关注

    Javascript is client side scripting language.So it cannot communicate directly with database.

    Only Server side scripting language can interact with database through its library functions.

    So if you need to update data in database on condition of javascript variable you can accomplish it using ajax technology.

    Through AJAX code snippet it can communicate with server in background without reloading the web page using XMLHttpRequest Object.

    Please refer the example mentioned in following URL which uses ajax technique for communication with database

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题