PHP中的href html按钮

gkr*_*kri 3 html php button

我正在尝试将一个[将重定向到主页的按钮]放在由于提交表单而产生的.php文件中。

php文件是这样的:

<?php 

$user = 'root';
$pass = '';
$db = 'testdb';

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$conn = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect.");

$sql = "INSERT INTO Customers (CompanyName, City, Country) VALUES ('$_POST[post_company]', '$_POST[post_city]', '$_POST[post_country]')";

if(mysqli_query($conn, $sql)){
    echo "New record created successfully!";
    $last_id = $conn->insert_id;
    echo "\nNew record has ID: " . $last_id;
}else{
    echo "Error 1: " . $sql . "<br>" .mysqli_error($conn);
}


// echo("<button onclick='location.href='index.html'>Back to Home</button>");
// this is a test I did before and didn't work

$conn->close();

?>

<div id="center_button"><button onclick="location.href='index.html'">Back to Home</button></div>
Run Code Online (Sandbox Code Playgroud)

我尝试将其制作为.html文件并放入php代码中,但是没有用。
我看到您可以将html代码放在php文件中,但这似乎也不起作用。我也尝试过不使用div标签,也尝试放置html和body标签。
顺便说一句,数据库工作正常,它可以正常更新,但似乎无法使html代码出现。


我究竟做错了什么?

Fal*_*4rm 5

您缺少HTML标记。这就是为什么您看不到您的按钮的原因。

?>
<html>
<head>
</head>

<body>
<div id="center_button">
    <button onclick="location.href='index.html'">Back to Home</button>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

编辑:删除此行:

header("Content-Type: application/json; charset=UTF-8");
Run Code Online (Sandbox Code Playgroud)