我在用户提交表单后尝试回显html页面时出现问题。按下提交按钮后,用户将被重定向到包含以下php代码的email.php页面。然而,echo以明文形式返回html内容,格式不正确。
这是我的PHP代码:
<?php
//error_reporting(E_ALL);
//ini_set("display_errors", "On");`
//$json_data=$_GET['track'];
//______________________________________
//Establish Connection
$db = new PDO('mysql:host=xxxx;dbname=xxxx;charset=utf8', 'xxxx', 'xxxx')
or die("cannot open database");
//$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
//$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$sql = "INSERT INTO xxxx (xxxx, xxxx, xxxx, xxxx) VALUES (:email, :firstname, :lastname, :year :month :day)";
$params = array(
':email' => $_POST['email'],
':firstname' => $_POST['firstname'],
':lastname' => $_POST['lastname'],
':day' => $_POST['day'],
':month' => $_POST['month'],
':year' => $_POST['year']
);
$points = $db->prepare($sql);
echo "<html>
<head>
<title>XXXX Confrimation</title>
</head>
<body style='background-color:#f3f3f3; margin:0;'>
<div style='background-color:#0a5a92; padding: 10px;color:#f3f3f3; margin:0px;'>
<h2>Thank You for Signing up!</h2>
</div>
<div style='padding: 0px 10px 0px 10px;'>
<p> If you did not subscribe to XXXX, please email <a href='mailto:XXXX@XXXX.com'>XXXX@XXXX.com</a> to unsubcribe.</p>
<br/>
</div>
</body>
</html>"
;
以下是输出:
<html>
<head>
<style type="text/css"></style>
</head>
<body>
<pre style="word-wrap: break-word; white-space: pre-wrap;">
<html>
<head>
<title>XXXX Confirmation</title>
</head>
<body style='background-color:#f3f3f3; margin:0;'>
<div style='background-color:#0a5a92; padding: 10px;color:#f3f3f3; margin:0px;'>
<h2>Thank You for Signing up!</h2>
</div>
<div style='padding: 0px 10px 0px 10px;'>
<p> If you did not subscribe to XXXX, please email <a href='mailto:XXXX@XXXX.com'>XXXX@XXXX.com</a> to unsubcribe.</p>
<br/>
</div>
</body>
</html>
</pre>
</body>
</html>
我不是最好的PHP,我运行的知识非常有限。我也使用wordpress运行我的网站。如果你知道为什么会这样,请告诉我。我已经在论坛上搜索了一个多星期了,并且没有找到解决方案的运气。
------的修改 -------
我意识到我在我的php代码的标题中犯了一个错误,它将内容声明为json文件。因为这是我的一个与上述问题无关的错误,我将删除这个问题。感谢您发布答案,因为没有它们我就不会看到这个愚蠢的错误。
---------的修改 ---------- 更正:我将向那些帮助我的人投票,只是因为其他人有类似的问题。我目前没有投票的声誉,但我会尽快。再次感谢你。
答案 0 :(得分:0)
您似乎将内容回显到已经具有基本HTML
框架(开放html
标签等)的网站。如果您查看输出结果,则可以看到有2个html
,2个body
和2个head
标记,每个网站只能显示一次。我想你的脚本会将你的脚本输出回传到<pre>
标签。
尝试使用此回音,看看它是否会产生您想要的输出:
echo "
<body style='background-color:#f3f3f3; margin:0;'>
<div style='background-color:#0a5a92; padding: 10px;color:#f3f3f3; margin:0px;'>
<h2>Thank You for Signing up!</h2>
</div>
<div style='padding: 0px 10px 0px 10px;'>
<p> If you did not subscribe to XXXX, please email <a href='mailto:XXXX@XXXX.com'>XXXX@XXXX.com</a> to unsubcribe.</p>
<br/>
</div>";
答案 1 :(得分:0)
默认情况下,您已在wordpress中打开了数据库连接。使用以下作为示例....处理同一页面上的提交并重定向到另一页再次拉动信息(如果他们按下刷新按钮,这将是重要的)因此,您可以使用以下代替pdo功能:
if($_POST):
global $wpdb;
$data[]=$_POST['value'];
$wpdb->insert( 'wp_custom', $data);
if($wpdb->insert_id):
$url= get_site_url().'/rest of the url'.'id='.$wpdb->insert_id;
wp_redirect($url);
exit();
endif;
endif;
get_header(); // will apply html and title tags.....check your theme for the the correct div / section to start html code with.
?>
<form action="" ></form>
重定向页面:
if($_GET['id']):
$info = $wpdb->get_row("SELECT * FROM $wpdb->custom WHERE id = $_GET['id']");
endif;
?>
<section class=""></section>
您还需要查看安全性 - 查找santize_text_field() http://codex.wordpress.org/Class_Reference/wpdb
答案 2 :(得分:0)
我意识到我在我的php代码的标题中犯了一个错误,它将内容声明为json文件。因为这是我的一个错误,与我的问题无关,我将接受这个帖子作为答案并向那些帮助我的人投票。感谢您发布答案,因为没有它们我就不会看到这个愚蠢的错误。