weixin_33694620 2016-11-16 22:47 采纳率: 0%
浏览 49

用AJAX执行PHP

I'm trying to run a php file with AJAX from my "main.php".

The problem is that it doesnt let my stay on the main.php and moves me to "tallennus.php", when I want to stay at "main.php". The tallennus.php should be done on the background.

"tallennus.php" does it's work perfectly (saves data to database). But I want to stay on mainsite while it does it.

Im trying to use AJAX to run "tallennus.php" on the background.

Why does the site change from main.php to tallennus.php?

<button onclick="testi()">testi</button><br>
<script>
function testi()
{
    alert("function started");
    var xmlhttp;
   if (window.XMLHttpRequest) {
     xmlhttp=new XMLHttpRequest();
   } else {
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }

    xmlhttp.onreadystatechange=function() {
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
            }
            xmlhttp.open("get", "tallennus.php",true);
            xmlhttp.send();
        }   




</script>
  • 写回答

2条回答 默认 最新

  • 谁还没个明天 2016-11-16 23:18
    关注

    The default type of <button> is type="submit", so it submits the form. Change it to type="button".

    <button type="button" onclick="testi()">testi</button><br>
    

    As per the following reference:

    "A button element with no type attribute specified represents the same thing as a button element with its type attribute set to "submit"."

    评论

报告相同问题?