简体   繁体   English

php中的href html按钮

[英]href html button inside php

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.我试过把它变成一个 .html 文件并放入 php 代码中,但没有用。
I saw that you can place html code inside a php file, but this doesn't seem to work either.我看到您可以将 html 代码放在 php 文件中,但这似乎也不起作用。 I've also tried it without the div tag, and also tried putting html and body tags, too.我也试过没有 div 标签,也试过放入 html 和 body 标签。
The database works fine btw, it updates normally, I just can't seem to make the html code appear.顺便说一句,数据库工作正常,它正常更新,我似乎无法显示 html 代码。


What am I doing wrong?我究竟做错了什么?

You're missing an HTML tag.你缺少一个 HTML 标签。 That's why you cannot see your 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");

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.您需要转义双引号而不是用单引号替换它们,否则您的 'index.html' 链接将不起作用。 As I now have a spare 5 mins...因为我现在有空闲的 5 分钟...

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

A string inside a string inside a string needs care;-)字符串内的字符串内的字符串需要小心;-)

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.换句话说,将所需的 php 代码块嵌套在锚元素中以“链接”它。 Then add the php code block and inside that code block you'll want to add an echo().然后添加 php 代码块,并在该代码块中添加一个 echo()。 Inside that echo you want to add your HTML code like normal.在该回声中,您想像往常一样添加 HTML 代码。 As if you weren't even writing in PHP.就好像您甚至不是用 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 忽略第一个 " 作为结束回声斯汀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM