我试图回应PHP中的HTML表单,但我无法完成它。 它只是回显预先格式化的HTML。我没有收到表格。
这是我的PHP脚本
do-reset.php
<?php
require_once 'connect.php';
session_start();
if($_SERVER['REQUEST_METHOD'] === 'GET') {
if(isset($_GET['email']) && !empty($_GET['email']) && isset($_GET['hash']) && !empty($_GET['hash'])) {
$email = htmlentities(mysqli_real_escape_string($connection, trim($_GET['email'])));
$hash = htmlentities(mysqli_real_escape_string($connection, trim($_GET['hash'])));
$search_query = "SELECT email, hash, status FROM users WHERE email = '{$email}' AND forgot_password_hash = '{$hash}' AND
status = '1'";
$do_search_query = mysqli_query($connection, $search_query);
if($do_search_query) {
$count_rows = mysqli_num_rows($do_search_query);
if($count_rows > 0) {
$_SESSION['email'] = $email;
$_SESSION['hash'] = $hash;
echo "<form method='post' action='do-reset.php'><input type='password' name='password'><br><input type='submit' value='Reset My Password'></form>";
}
else {
$data = array("result" => -3, "message" => "Invalid URL");
}
}
else {
$data = array("result" => -2, "message" => "Something Went Wrong! Try Again Later.");
}
}
else
{
$data = array("result" => -1, "message" => "Certain Request Parameters Are Missing!");
}
}
else {
$data = array("result" => 0, "message" => "Incorrect Request Method!");
}
mysqli_close($connection);
/* JSON Response */
header('Content-type: application/json');
echo json_encode($data, JSON_PRETTY_PRINT);
?>
答案 0 :(得分:1)
1。:删除header('Content-type: application/json');
这基本上会告诉浏览器将输出显示为文本。
<强> 2:强>
要保留格式,您可以使用<pre>
- 标记:
echo "<pre>";
echo json_encode($data, JSON_PRETTY_PRINT);`
echo "</pre>";
不同方法:
仅在填充$data
- 数组时将内容类型设置为application / json
if(!empty($data)){
header('Content-type: application/json');
echo json_encode($data, JSON_PRETTY_PRINT);
}
答案 1 :(得分:0)
我找到了出路! <?php
require_once 'connect.php';
session_start();
$type_json = true;
if($_SERVER['REQUEST_METHOD'] === 'GET') {
if(isset($_GET['email']) && !empty($_GET['email']) && isset($_GET['hash']) && !empty($_GET['hash'])) {
$email = htmlentities(mysqli_real_escape_string($connection, trim($_GET['email'])));
$hash = htmlentities(mysqli_real_escape_string($connection, trim($_GET['hash'])));
$search_query = "SELECT email, hash, status FROM users WHERE email = '{$email}' AND forgot_password_hash = '{$hash}' AND
status = '1'";
$do_search_query = mysqli_query($connection, $search_query);
if($do_search_query) {
$count_rows = mysqli_num_rows($do_search_query);
if($count_rows > 0) {
$_SESSION['email'] = $email;
$_SESSION['hash'] = $hash;
$type_json = false;
echo "<form method='post' action='do-reset.php'><input type='password' name='password'><br><input type='submit' value='Reset My Password'></form>";
}
else {
$data = array("result" => -3, "message" => "Invalid URL");
}
}
else {
$data = array("result" => -2, "message" => "Something Went Wrong! Try Again Later.");
}
}
else
{
$data = array("result" => -1, "message" => "Certain Request Parameters Are Missing!");
}
}
else {
$data = array("result" => 0, "message" => "Incorrect Request Method!");
}
mysqli_close($connection);
/* JSON Response */
if($type_json) {
header('Content-type: application/json');
echo json_encode($data, JSON_PRETTY_PRINT);
}
?>
为我做了诀窍。
{{1}}
答案 2 :(得分:-5)
PHP在服务器端执行,被视为脚本而不是标记语言,这意味着请求页面上的HTML对服务器无关紧要,只关心PHP。所以,如果你做了
<?php
if(true) {
?>
<form>Hello</form>
<?php
}
?>
如果语句为true,则html表单将仅显示文本hello,其中true始终为true。您可以将其替换为任何声明,例如检查某人是否从他们提交的表单中输入某个字段中的内容。
希望这有帮助!