我之前運行過類似的代碼,但現在我丟失了它。無論我做什么,它都不會運行 php 代碼。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function kosarica() {
var vrednost = "Itworks!";
var httpr=new XMLHttpRequest();
httpr.open("POST","izpis3.php",true);
httpr.setRequestHeader("Content-type","aplication/x-www-form-urlencode");
httpr.onreadystatechange=function(){
if(httpr.readyState==4 && httpr.status ==200){
document.getElementById("responce").innerHTML=httpr.responseText;
}
httpr.send("vrednost" vrednost);
}
}
</script>
</head>
<body>
<p id="responce">a</p>
<button onclick="kosarica()">Click me</button>
</body>
</html>
PHP代碼:
<?php
echo $_POST['vrednost'];
?>
我知道我可以在 javascript 中為這個示例撰寫代碼,但我想在訪問我的資料庫的地方運行更多的 php 代碼。
uj5u.com熱心網友回復:
- 它不會失敗,但它永遠不會發生。您需要移動
send()處理程式的外部。 - 內容型別錯誤。
- 您需要使用等號使其成為 PHP 的變數。
- 請不要使用
var關鍵字。用于const常量或let變數。
function kosarica() {
const vrednost = "Itworks!";
const httpr = new XMLHttpRequest();
httpr.open("POST", "izpis3.php", true);
httpr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httpr.onreadystatechange = function () {
if (httpr.readyState === 4 && httpr.status === 200) {
document.getElementById("responce").innerHTML = httpr.responseText;
}
}
httpr.send("vrednost=" vrednost);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/516028.html
上一篇:遍歷jsonapi字典欄位
