2016-11-27 50 views
0

基本上我想在我的HTML/PHP页面上有一个按钮,它将一旦点击发送到另一个PHP页面,将更新MySQL表中的值。脚本功能如何发送呼叫到PHP页面

<html> 
    <head> 
    <script> 
    src = "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> 
    function toggleText(button_id){ 
      if(document.getElementById(button_id).innerHTML == "Uninstall All"){ 
        document.getElementById(button_id).innerHTML = "Cancel Uninstall"; 
        //ajax//////////////////// 
        xmlhttp = new XMLHttpRequest(); 
        xmlhttp.open("GET", "valueChange.php?uninstalled=1&type=all", true); 
        xmlhttp.send(); 
        //////////////////////// 
      } 
} 
    </script> 
    </head> 

我很好奇为什么这不起作用。

我测试了我的toggleText函数而没有使用ajax,它正确地更改了HTML。

但是,一旦我加入ajax访问这个valueChange.php页面并更新一个sql值,它不起作用。

我已经测试了自己的php页面,它正确地更新了sql中的值。

我从来没有使用Ajax,所以我很好奇,如果我做错了?我认为我的src =“google/ajax/jquery”是安装它的方式。或者我需要在托管我的网站的VPS上安装一个库?

+0

请告诉我Ajax代码。 –

+0

我以为xmlhttp对象是ajax?把它直接关闭W3学校。 – mocode9

回答

2

我在上面的代码中看到的唯一错误只是一个错字。看到第3行你关闭了脚本标记之前,应该已经有第二个属性了<script src="*link to jQuery*" ></script>

另外一件你不需要使用jQuery库来使用XMLHttpRequest()的方法。

修改后的代码:

<html> 
<head>  
<script> 
function toggleText(button_id) 
{ 
     if(document.getElementById(button_id).innerHTML == "Uninstall All") 
      { 
       document.getElementById(button_id).innerHTML = "Cancel Uninstall"; 
       //ajax//////////////////// 
       var xmlhttp = new XMLHttpRequest(); 
       xmlhttp.open("GET", "valueChange.php?uninstalled=1&type=all", true); 
       xmlhttp.send(); 
       //////////////////////// 
     } 
} 
</script> 
</head>