我试图在提交表单中提供的.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标签。
数据库工作正常btw,它正常更新,我似乎无法使html代码出现。
我做错了什么?
答案 0 :(得分:4)
U're missing HTML tag. That's why u cannot see ur button.
?>
<html>
<head>
</head>
<body>
<div id="center_button">
<button onclick="location.href='index.html'">Back to Home</button>
</div>
</body>
</html>
EDIT : remove this line :
header("Content-Type: application/json; charset=UTF-8");
答案 1 :(得分:2)
Your test had broken quoting. You need to escape the double quotes rather than replacing them with single quotes, as otherwise your 'index.html' link won't work. As I now have a spare 5 mins...
echo("<button onclick=\"location.href='index.html'\">Back to Home</button>");
A string inside a string inside a string needs care;-)
答案 2 :(得分:1)
这对我有用。
<a><?php echo( "<button onclick= \"location.href='inc/name_of_php_file.php'\">your buttons name goes here</button>");?></a>
换句话说,将您想要的PHP代码块嵌套在一个锚元素中以链接&#39;它。然后添加php代码块并在该代码块内部添加echo()。在回声内部,您希望像平常一样添加HTML代码。好像你甚至没有用PHP写作。
将文件夹名称和文件编辑到所需位置。然后最后编辑您希望按钮向观众显示的文字。
onclick = \&#34; location.href = &#39; folder_name_goes_here / name_of_php_file_goes_here.php &#39; \&#34;&gt; 您的按钮名称在此处< / strong>
这应该使用您想要的标题链接到您的网页。
请注意 \&#34; ,因为这非常重要。没有\在&#34;之前由于发现嵌套在彼此内部的许多代码,您的代码无法正确读取。 \允许HTML忽略第一个&#34;作为回声Sting的结束。