php - PHP 输出 HTML,更好的方法?
问题描述
基本上这就是我所拥有的:
<form method="POST" action="model.php">
<input type="hidden" name="from" value="edit">
<?php
include 'model.php';
$contactID = $_POST['editConRad'];
selectSingle($contactID);
echo "<input type='hidden' name='contactID' value='$contactID'>";
echo "<label>First Name:</label>";
echo "<input type="text" name="fname">";
echo "<label>Last Name:</label>";
echo "<input type="text" name="lname">";
echo "<label>Email:</label>";
echo "<input type="email" name="email">";
echo "<label>Street Address:</label>";
echo "<input type="text" name="streetaddress">";
echo "<label>City:</label>";
echo "<input type="text" name="city">";
echo "<label>Province:</label>";
echo "<input type="text" name="province">";
echo "<label>Postal Code:</label>";
echo "<input type="text" name="postalcode">";
echo "<label>Phone:</label>";
echo "<input type="number" name="phone">";
echo "<label>Date of Birth:</label>";
echo "<input type="date" name="yob">";
echo "<input type="submit" value="Submit Edit">";
?>
</form>
而且我认为必须有更好的方法来输出 html 而不是必须用 echo 输出每一行,我认为有一个打印功能可以做到这一点,但我找不到。
有人有更好的方法吗?我知道我可以做一个巨大的回声,但肯定还有更好的东西。
我想这种方式比我上面的方式更好,但仍然没有寻找更好的方式。
echo '<label>First Name:</label>.
<input type="text" name="fname">.
<label>Last Name:</label>.
<input type="text" name="lname">.
<label>Email:</label>.
<input type="email" name="email">.
<label>Street Address:</label>.
<input type="text" name="streetaddress">.
<label>City:</label>.
<input type="text" name="city">.
<label>Province:</label>.
<input type="text" name="province">.
<label>Postal Code:</label>.
<input type="text" name="postalcode">.
<label>Phone:</label>.
<input type="number" name="phone">.
<label>Date of Birth:</label>.
<input type="date" name="yob">.
<input type="submit" value="Submit Edit">';
解决方案
只要添加适当的标签,您就可以随时在 PHP 和 HTML 之间切换。
例如:
<?php
echo "This is output from an echo statement<br>"
?>
This is plain old HTML<br>
<?php
echo "And now we're back to PHP<br>"
你的表格变成:
<form method="POST" action="model.php">
<input type="hidden" name="from" value="edit">
<?php
// Do this bit in PHP
include 'model.php';
$contactID = $_POST['editConRad'];
selectSingle($contactID);
?>
<!-- Now switch back to HTML -->
<!-- PHP variable embedded here-------------|................| -->
<input type='hidden' name='contactID' value='<?= $contactID?>'>";
<label>First Name:</label>
<input type="text" name="fname">
<label>Last Name:</label>";
<input type="text" name="lname">
<label>Email:</label>
<input type="email" name="email">
<!-- and so on -->
</form>
<?php
// You can store long text strings in HEREDOC or NOWDOC syntax
$longString = <<<'EOT'
This is an arbitrarily long string, but this one is only short<br>
EOT
echo $longString;
有关 PHP如何处理字符串、HEREDOC、NOWDOC 以及单引号和双引号的重要性的所有详细信息,请参阅PHP 手册。
推荐阅读
- sql-server - 从存储过程中的 Orderline 表更新 ItemId
- scala - 如何在 Spark Dataframe 上应用转换来生成元组?
- c++ - 给定代码是否有任何更改,因此我可以为问题提供正确的输出
- javascript - Javascript实践测试题:匿名函数
- amazon-web-services - 尝试使用信用卡付款时出现 AWS Workspaces 问题
- groovy - 如何逐行读取文本文件在jmeter中使用groovy脚本引擎定义计数函数
- python-3.x - 寻找平均蟒蛇的技术
- dask - 有没有办法使用 dask 在镶木地板文件上运行 SQL 语句?
- python - Django:无法分配“<Booking:loader2>”:“Booking.b_price”必须是“Loader_post”实例
- amp-html - 如何最初激活 amp-story-player 中的任何网络故事