php中的href html按钮

新手上路,请多包涵

我试图将一个按钮 [将重定向到主页] 放在一个 .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>

我试过将其制作成一个 .html 文件并放入 php 代码中,但没有成功。

我看到您可以将 html 代码放在 php 文件中,但这似乎也不起作用。我也尝试过不使用 div 标签,也尝试过放置 html 和 body 标签。

顺便说一句,数据库工作正常,它更新正常,我似乎无法显示 html 代码。

我究竟做错了什么?

原文由 gkri 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 226
2 个回答

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

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

<body>
<div id="center_button">
    <button onclick="location.href='index.html'">Back to Home</button>
</div>
</body>
</html>

编辑:删除此行:

 header("Content-Type: application/json; charset=UTF-8");

原文由 Falt4rm 发布,翻译遵循 CC BY-SA 4.0 许可协议

您的测试已中断引用。您需要转义双引号而不是用单引号替换它们,否则您的“index.html”链接将无法使用。因为我现在有空闲的 5 分钟…

 echo("<button onclick=\"location.href='index.html'\">Back to Home</button>");

字符串中的字符串中的字符串需要小心;-)

原文由 pete23 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏