我正在设计一个相当简单的报告系统。现在使用php(以及后来的一些Jquery)让用户登录并计算总数并发布到数据库。当我在IE8中测试页面时,我的问题开始了。它在第75行的echo <<<_END
语句中存在重大问题。除此之外,任何人都知道在PHP中编写嵌套HTML的替代方法。任何帮助表示赞赏。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<link rel="stylesheet" type="text/css" href="OPSstyle.css" />
</HEAD>
<BODY>
<div id="wrapper">
<div id="bodyContent">
<div id="header">
<img id="logo" src="OPS_logo.gif" alt="OPS Logo"/>
<p>OPS ASSESSMENT</p>
</div>
<div id="leftNav">
<p>To begin please login</p>
<a href="loginusers.php">Test Login</a><br/>
<a href="sqltest.php">Form Test</a><br/>
</div>
<div id="content">
<?php
//Connects to database
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if(!$db_server) die("Unable to connect to MySQL: " .mysql_error());
mysql_select_db($db_database)
or die ("unable to select database: " .mysql_error());
//deletes record
if(isset($_POST['delete']) && isset($_POST['AssessmentID']))
{
$AssessmentID = get_post('AssessmentID');
$query2 = "DELETE FROM assessmentscores WHERE AssessmentID='$AssessmentID'";
if(!mysql_query($query2, $db_server))
echo "Delete Failed: $query(br />" .
mysql_error() . "<br /><br />";
}
//Inserts record
if (isset ($_POST['AssessmentID'])&&
isset($_POST['Date']) &&
isset($_POST['Inspector']) &&
isset($_POST['PlantAssist']) &&
isset($_POST['Safety_Total'])
)
{
$AssessmentID = get_post('AssessmentID');
$Date = get_post('Date');
$Inspector = get_post('Inspector');
$PlantAssist =get_post('PlantAssist');
$Safety_Total = get_post('Safety_Total');
;
$query = "INSERT INTO opsassessment.assessmentscores(AssessmentID, Date, Inspector, PlantAssist, `Safety_Total`) VALUES" .
"('$AssessmentID', '$Date', '$Inspector', '$PlantAssist', '$Safety_Total')";
if (!mysql_query($query, $db_server))
echo "INSERT failed: $query<br />" .
mysql_error() . "<br /><br />";
}
echo <<<_END
<form action = "sqltest.php" method="post"><pre>
AssessmentID <input type="text" name="AssessmentID" /><br>
Date <input type="text" name="Date" /><br>
Inspector <input type="text" name="Inspector" /><br>
PlantAssist <input type="text" name="PlantAssist" /><br>
Safety_Total <input type="text" name="Safety_Total" /><br>
<input type="submit" value="ADD RECORD" /><br>
</pre></form>
_END;
$query = "SELECT * FROM assessmentscores";
$result = mysql_query($query);
if (!$result) die("Database access failed: " .mysql_error());
$rows = mysql_num_rows($result);
for($j = 0; $j < $rows; ++$j)
{
$row= mysql_fetch_row($result);
echo <<<_END
<pre>
AssessmentID: $row[0]
Date: $row[1]
Inspector: $row[2]
PlantAssist: $row[3]
Safety_Total: $row[4]
</pre>
<form action="sqltest.php" method="post">
<input type ="hidden" name="delete" value ="yes" />
<input type = "hidden" name="AssessmentID" value = "$row[0]" />
<input type ="submit" value="DELETE RECORD" /></form>
_END;
}
mysql_close($db_server);
function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}
?>
</div>
<a href="form1.php">Login Form</a><br><br>
<a href="testOPS.php">OPS Assessment</a>
</div>
</body>
</html>
答案 0 :(得分:4)
Heredoc标识符必须在其自身的一行上,并且必须是此行上的唯一内容(包括缩进等)。删除前导空格并确保换行符在_END;
后直接显示。
但是,在你的情况下,我建议你离开PHP模式输出普通的html
例如,而不是
echo <<<_END
<form action = "sqltest.php" method="post"><pre>
AssessmentID <input type="text" name="AssessmentID" /><br>
Date <input type="text" name="Date" /><br>
Inspector <input type="text" name="Inspector" /><br>
PlantAssist <input type="text" name="PlantAssist" /><br>
Safety_Total <input type="text" name="Safety_Total" /><br>
<input type="submit" value="ADD RECORD" /><br>
</pre></form>
_END;
此
?>
<form action = "sqltest.php" method="post"><pre>
AssessmentID <input type="text" name="AssessmentID" /><br>
Date <input type="text" name="Date" /><br>
Inspector <input type="text" name="Inspector" /><br>
PlantAssist <input type="text" name="PlantAssist" /><br>
Safety_Total <input type="text" name="Safety_Total" /><br>
<input type="submit" value="ADD RECORD" /><br>
</pre></form>
<?php
它通常更具可读性,许多IDE能够识别它并突出显示“其他内容”为html。
答案 1 :(得分:3)
正如评论者所说,HEREDOC(&lt;&lt;&lt;和_END;)位与服务器端PHP相关,浏览器应该对它们一无所知。
HEREDOC标签的一个主要的,易于忘记的黄金法则是,结束标记不能被隐藏,永远!或者它将会中断。
下面是关于他们的读物:
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
如果它真的破坏了它,它真的就是这样做(虽然,它没有理由应该这样) - 记住它只是一个输出块,所以你可以用它来代替它:
$out = "blah blah";
$out .= "more blah blah";
$out .= "more blah blah";
$out .= "more blah blah";
$out .= "more blah blah";
echo $out;