[英]Converting HTML variable to PHP variable
I'm trying to save a variable to a text file using PHP code. 我正在尝试使用PHP代码将变量保存到文本文件。 Everything else works apart from line 33 of my code, where I try and pass an HTML variable into PHP , in order to save that variable to my text file.
其他所有工作都与代码的第33行不同,我尝试将HTML变量传递给PHP ,以便将该变量保存到我的文本文件中。 Wouldn't like to use forms as I don't want to change my earlier code round too much as it's taken me ages to get it to work.
我不想使用表格,因为我不想过多更改我的早期代码,因为花了我很多时间才能使它起作用。
Have already tried, $_GET
and $_REQUEST
已尝试
$_GET
和$_REQUEST
<DOCTYPE.html>
<html>
<body>
<h3>Please enter your UN and PW</h3>
<p id="demo"></p>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
var username, password, x, INput;
username = prompt("Username: ");
password = prompt("Password: ");
document.getElementById("demo").innerHTML= "UN: " + username;
document.getElementById("demo1").innerHTML = "PW: " + password;
x = (password<=0) ? "No":"Yes";
document.getElementById("demo2").innerHTML = "Typed password: " + x;
INput= String(username + ", " + x + "\n" )
alert(INput);
</script>
<?php
// Open the text file
$f = fopen("textfile.txt", "a");
// Write text line
fwrite($f, #######);
// Close the text file
fclose($f);
?>
</body>
</html>
The "##s" are where I would like "INput" to go, but it doesn't work as it is an html variable. 我想在“ ## s”中输入“ INput”,但由于它是一个html变量,所以它不起作用。
Expected Output: INput will save to the "textfile.txt" file 预期输出: INput将保存到“ textfile.txt”文件
Actual Output: "Notice: Use of undefined constant INput - assumed 'INput' in /storage/ssd1/856/9172856/public_html/UNPW.php on line 33" 实际输出: “注意:使用未定义的常量INput-在第33行的/storage/ssd1/856/9172856/public_html/UNPW.php中假定为'INput'”
EDIT: Using 000webhost.com, if this changes anything 编辑:使用000webhost.com,如果这更改任何内容
The easiest way to make AJAX in Javascript is to use the Fetch function. 用Java语言制作AJAX的最简单方法是使用Fetch函数。 There are lots of examples available on the web, but here's what I use:
网络上有很多可用的示例,但这是我使用的示例:
side1.html side1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h3>Please enter your UN and PW</h3>
<p id="demo"></p>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
var username, password, x, INput;
username = prompt("Username: ");
password = prompt("Password: ");
document.getElementById("demo").innerHTML = "UN: " + username;
document.getElementById("demo1").innerHTML = "PW: " + password;
/* -----------------------------------------------------------------------
x = (password <= 0) ? "No" : "Yes";
document.getElementById("demo2").innerHTML = "Typed password: " + x;
INput = String(username + ", " + x + "\n")
alert(INput);
----------------------------------------------------------------------- */
fetch("side2.php", {
credentials : "same-origin",
method : "POST",
headers : { "Content-Type": "application/json" },
body : JSON.stringify(
{
"Username": username,
"Password": password
})
})
.then(response => response.json())
.then(data=>{
if (data.status == 'good world :)')
{
document.getElementById("demo2").innerHTML = data.User_Psw;
}
else
{
document.getElementById("demo2").innerHTML = '??? it was so easy, what did you miss?';
}
})
</script>
</body>
</html>
side2.php side2.php
<?php
mb_internal_encoding("UTF-8"); // old security story,
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
$ReturnInfo = array();
$ReturnInfo['status'] = 'bad world :/';
if ($contentType === "application/json") {
//Receive the RAW post data.
$content = trim(file_get_contents("php://input"));
$decoded = json_decode($content, true);
$User = $decoded['Username'];
$Pwd = $decoded['Password'];
// write data in a File...
$fp = fopen('data.txt', 'wb');
fwrite($fp, "last connected user info =\n");
fwrite($fp, 'Username='.$User."\n");
fwrite($fp, 'Password='.$Pwd."\n");
fclose($fp);
$ReturnInfo['status'] = 'good world :)';
$ReturnInfo['User_Psw'] = $User.'<=>'.$Pwd;
//If json_decode failed, the JSON is invalid.
//if(! is_array($decoded)) { ......
}
header("Cache-Control: no-cache, must-revalidate"); // force reset cache
header("Expires: Mon, 26 Jul 2000 05:00:00 GMT");
header('Content-type: application/json');
echo json_encode($ReturnInfo);
exit(0);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.