php php中的href html按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30427891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:47:41  来源:igfitidea点击:

href html button inside php

phphtmlbutton

提问by gkri

I'm trying to put a button [that will redirect to the home page] inside a .php file that comes as a result of a submit form.

我正在尝试将一个按钮 [将重定向到主页] 放在一个 .php 文件中,该文件是提交表单的结果。

The php file is this:

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>

I 've tried making this an .html file and put inside the php code, but didn't work.
I saw that you can place html code inside a php file, but this doesn't seem to work either. I've also tried it without the div tag, and also tried putting html and body tags, too.
The database works fine btw, it updates normally, I just can't seem to make the html code appear.

我试过把它变成一个 .html 文件并放入 php 代码中,但没有用。
我看到您可以将 html 代码放在 php 文件中,但这似乎也不起作用。我也试过没有 div 标签,也试过放入 html 和 body 标签。
顺便说一句,数据库工作正常,它正常更新,我似乎无法显示 html 代码。


What am I doing wrong?


我究竟做错了什么?

回答by Falt4rm

U're missing HTML tag. That's why u cannot see ur button.

你缺少 HTML 标签。这就是为什么你看不到你的按钮。

?>
<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");

回答by pete23

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...

你的测试已经破坏了引用。您需要转义双引号而不是用单引号替换它们,否则您的 'index.html' 链接将不起作用。因为我现在有空闲的 5 分钟...

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

A string inside a string inside a string needs care;-)

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

回答by JefWit1F

This is what worked for me.

这对我有用。

<a><?php echo( "<button onclick= \"location.href='inc/name_of_php_file.php'\">your buttons name goes here</button>");?></a>

In other words nest your desired php code block inside an anchor element to 'link' it. Then add the php code block and inside that code block you'll want to add an echo(). Inside that echo you want to add your HTML code like normal. As if you weren't even writing in PHP.

换句话说,将所需的 php 代码块嵌套在锚元素中以“链接”它。然后添加 php 代码块,并在该代码块中添加一个 echo()。在该回声中,您想像往常一样添加 HTML 代码。就好像您甚至不是用 PHP 编写一样。

Edit the folder name and file to your desired location. And then finally edit what text you want your button to show your viewers.

将文件夹名称和文件编辑到所需位置。然后最后编辑您希望按钮向观众显示的文本。

onclick= \"location.href='folder_name_goes_here/name_of_php_file_goes_here.php'\">your buttons name goes here

onclick= \"location.href= 'folder_name_goes_here/name_of_php_file_goes_here.php'\">你的按钮名称在这里

This should link to your page with your desired caption.

这应该链接到您的页面,并带有您想要的标题。

Please keep in mind the \"as this is utterly important. Without \ in front of the " your code wont read correctly due to the many "s found nested inside each other. \ allows HTML to ignore the first " as an end to the echo Sting.

请记住\",因为这非常重要。如果没有 \ 在 " 前面,您的代码将无法正确读取,因为许多 " 相互嵌套。\ 允许 HTML 忽略第一个 " 作为结束回声斯汀。