Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript mysqli_查询在函数内部自动激活?_Javascript_Php_Sql - Fatal编程技术网

Javascript mysqli_查询在函数内部自动激活?

Javascript mysqli_查询在函数内部自动激活?,javascript,php,sql,Javascript,Php,Sql,无论出于什么原因,我的数据库都会更新,就像函数运行一样,但它从未响应任何内容。当我重新加载页面时,它会自动将名称设置为“John”,但我从未单击过按钮 <?php function a(){ $con = mysqli_connect("localhost", "example", "example", "example"); $sql = "UPDATE User SET name = 'John' WHERE name = '$username'

无论出于什么原因,我的数据库都会更新,就像函数运行一样,但它从未响应任何内容。当我重新加载页面时,它会自动将名称设置为“John”,但我从未单击过按钮

    <?php
    function a(){
      $con = mysqli_connect("localhost", "example", "example", "example");
      $sql = "UPDATE User SET name = 'John' WHERE name = '$username'";

      mysqli_query($con, $sql);

      //test to see if function fires:
      echo "function executed";
    }
    ?>

当然,由于这一行,每次加载页面时都会更新数据库

document.write("<?php a(); ?>");
您需要使用来通过单击
按钮调用PHP脚本。像下面这样的东西会起作用

不要在页面上包含PHP脚本,只有在单击
按钮后才会调用mysql

<script type="text/javascript">
function ajax(url) {
    var http = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari, IE7+ ...
        http = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE 6 and older
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (! http) {
        // old browser or terminal maybe?
        return false;
    }
    http.onreadystatechange = function() {
        if ((http.readyState == 4) && (http.status == 200)) {
            // do stuff with the html/json/data returned
            // this alert will trigger once the PHP is run.
            alert(http.responseText);
        }
    }
    http.open('GET',url,true);
    http.send(null);
}

function b(){
    // call the code on PHP script to run.
    ajax("/path/to/your/php");
    //test if javascript function executes:
    // this alert should fire first.
    alert("function b() executed");
}
</script>

<button onclick="b()">Click me!</button>

函数ajax(url){
var-http=false;
如果(window.XMLHttpRequest){//Mozilla、Safari、IE7+。。。
http=newXMLHttpRequest();
}else如果(window.ActiveXObject){//IE 6及更早版本
http=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
如果(!http){
//可能是旧的浏览器还是终端?
返回false;
}
http.onreadystatechange=函数(){
如果((http.readyState==4)和&(http.status==200)){
//处理返回的html/json/数据
//PHP运行后将触发此警报。
警报(http.responseText);
}
}
http.open('GET',url,true);
http.send(空);
}
函数b(){
//调用PHP脚本上的代码以运行。
ajax(“/path/to/your/php”);
//测试javascript函数是否执行:
//应首先发出此警报。
警报(“函数b()已执行”);
}
点击我!

php在您的页面上的位置在哪里?这不会像您期望的那样工作。PHP脚本将不会执行(编译并运行)。你可能想看看,仔细看看。当用户单击
按钮调用Javascript函数
b()
时,PHP代码不会运行@Tigger,是的,当用户单击按钮时,PHP函数当然不会执行。我的意思是,每次解析器试图解析这一行时,php函数都会执行,而不是在单击按钮时。让我困惑的是,该文档。write(“”)被激活,即使它位于function@ArvidvandenHoogen你是说在JavaScript函数中?如果是的话,有什么让人困惑?当你写
谢谢你的时候,我对javascript了解很少,以前只听过几次ajax。我很困惑,因为据我所知,函数中的代码只有在函数被调用时才被调用。考虑到这个想法,我写了上面的代码。感谢您的解释:这个脚本是运行整个php脚本还是声明一个函数?我需要向函数发送一些变量,我将如何使用ajax做到这一点?请阅读链接页面。您可以传递一个表单对象,然后使用这些变量确定要执行的操作。
document.write("function executed");
<script type="text/javascript">
function ajax(url) {
    var http = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari, IE7+ ...
        http = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE 6 and older
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (! http) {
        // old browser or terminal maybe?
        return false;
    }
    http.onreadystatechange = function() {
        if ((http.readyState == 4) && (http.status == 200)) {
            // do stuff with the html/json/data returned
            // this alert will trigger once the PHP is run.
            alert(http.responseText);
        }
    }
    http.open('GET',url,true);
    http.send(null);
}

function b(){
    // call the code on PHP script to run.
    ajax("/path/to/your/php");
    //test if javascript function executes:
    // this alert should fire first.
    alert("function b() executed");
}
</script>

<button onclick="b()">Click me!</button>