用AJAX执行PHP

用AJAX执行PHP,php,ajax,Php,Ajax,我正在尝试从我的“main.php”使用AJAX运行一个php文件 问题是它不允许我停留在main.php上,当我想停留在“main.php”时,它会将我移到“tallennus.php”。php应该在后台完成 “tallennus.php”的工作非常完美(将数据保存到数据库)。但我想留在主站,因为它这样做 我试图使用AJAX在后台运行“tallennus.php” 为什么网站从main.php改为tallennus.php <button onclick="testi()">tes

我正在尝试从我的“main.php”使用AJAX运行一个php文件

问题是它不允许我停留在main.php上,当我想停留在“main.php”时,它会将我移到“tallennus.php”。php应该在后台完成

“tallennus.php”的工作非常完美(将数据保存到数据库)。但我想留在主站,因为它这样做

我试图使用AJAX在后台运行“tallennus.php”

为什么网站从main.php改为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>
testi
函数testi() { 警报(“功能启动”); var-xmlhttp; if(window.XMLHttpRequest){ xmlhttp=新的XMLHttpRequest(); }否则{ xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”); } xmlhttp.onreadystatechange=函数(){ if(xmlhttp.readyState==4&&xmlhttp.status==200) } open(“get”,“tallennus.php”,true); xmlhttp.send(); }
默认的
类型是
type=“submit”
,因此它会提交表单。将其更改为
type=“button”

testi

根据以下参考资料:


“未指定类型属性的button元素表示与属性相同的内容。”

我通过执行以下操作修复了我的问题:

<script>
function tallenna()
{


    var tunnus=document.getElementById('tunnus').value;
    var nimi=document.getElementById('nimi').value;
    var deadline=document.getElementById('deadline').value;
    var vaihe=document.getElementById('vaihe').value;
    var dataString=
    'tunnus='+ tunnus + 
    '&nimi='+ nimi+
    '&deadline='+ deadline+
    '&vaihe='+ vaihe;

    $.ajax({
        type:"post",
        url: "tallennus.php",
        data: dataString,
        cache:false,
        success:function(html){
            $('#msg').html(html);
        }
    });

    document.getElementById("frm").reset();// for clearing form
    return false;

}
</script>

<br><input type="submit" value="TALLENNA" onclick="return tallenna()">

函数tallenna()
{
var tunnus=document.getElementById('tunnus').value;
var nimi=document.getElementById('nimi').value;
var deadline=document.getElementById('deadline')。值;
var vaihe=document.getElementById('vaihe').value;
var数据串=
“tunnus=”+tunnus+
“&nimi=”+nimi+
“&截止日期=”+截止日期+
“&vaihe=”+vaihe;
$.ajax({
类型:“post”,
url:“tallennus.php”,
数据:dataString,
cache:false,
成功:函数(html){
$('#msg').html(html);
}
});
document.getElementById(“frm”).reset();//用于清除表单
返回false;
}

<script>
function tallenna()
{


    var tunnus=document.getElementById('tunnus').value;
    var nimi=document.getElementById('nimi').value;
    var deadline=document.getElementById('deadline').value;
    var vaihe=document.getElementById('vaihe').value;
    var dataString=
    'tunnus='+ tunnus + 
    '&nimi='+ nimi+
    '&deadline='+ deadline+
    '&vaihe='+ vaihe;

    $.ajax({
        type:"post",
        url: "tallennus.php",
        data: dataString,
        cache:false,
        success:function(html){
            $('#msg').html(html);
        }
    });

    document.getElementById("frm").reset();// for clearing form
    return false;

}
</script>

<br><input type="submit" value="TALLENNA" onclick="return tallenna()">