This question already has answers here:
What is the difference between client-side and server-side programming?
(4个答案)
2年前关闭。
不管出于什么原因,我的数据库都好像函数在运行一样进行更新,但是它从不回显任何内容。当我重新加载页面时,它会自动将名称设置为“ 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";
}
?>
这是我的html / javascript代码:
<script type="text/javascript">
function b(){
document.write("<?php a(); ?>");
//test if javascript function executes:
alert("function b() executed");
}
</script>
<button onclick="b()">Click me!</button>
我必须使用javascript,因为我的整个页面都是一个表单(出于单个保存按钮的目的),而您不能直接让按钮执行php函数。
我真的很困惑为什么它不回显,但是当我重新加载页面时它确实更新了我的数据库,请帮忙。
javascript参考方案
当然,由于此行,您每次加载页面时都会更新数据库
document.write("<?php a(); ?>");
每次加载页面时,您都会调用a()
来更新数据库
函数a
之所以没有回显"function executed"
的原因是,确实存在,但是您没有在页面上看到它,因为它在行中回显了。我确定您会在页面源代码中看到它。
当PHP解析器解析您的脚本时,它将产生并对此作出响应
document.write("function executed");
用PHP返回Confirm()[重复] - javascriptThis question already has answers here: PHP parse/syntax errors; and how to solve them (18个回答) …
Mongo汇总 - javascript我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …
如何从客户端将数据插入数据库? - javascript我是Web开发的初学者,可以访问cpanel上托管的网站,并且需要对其进行一些更改。这个网站的后端是由蛋糕PHP制作的,我想使用ajax从客户端将一些数据插入其数据库。问题是我不知道如何获取负责插入数据库的PHP文件的URL。参见下面的代码:var xhttp = newXMLHttpRequest(); xhttp.onreadystatechange= …
编码JSON数据以保留json格式 - javascript由于JSON值是动态生成的,并且其中的值是基于用户输入的,因此,如果用户在字符串中输入了不可接受的字符(如"),则会使json无效。就像是:{ "tag" : "demo", "value": "user " input" } 有没有一种方法可以编码或转义JS…
执行onclick时获得意外令牌 - javascript我正在使用onclick事件从PHP调用JS函数。这是我的代码:我在一个函数中,因此我需要通过PHP来完成它,因为然后我会返回:$html = '<input type="checkbox" checked value="1" id="setGetSku" name="se…