PHP and HTML have many interactions: PHP can generate HTML, and HTML can pass information to PHP.
HTML parsing. To specify an arbitrary string, you must put it in double quotes and use htmlspecialchars() to process the entire value. (Recommended learning: PHP video tutorial)
URL: URL consists of several parts. If you want your data to be interpreted as one of these, you must encode it with urlencode().
Hidden HTML form unit
<?php echo "<input type='hidden' value='" . htmlspecialchars($data) . "' />\n"; ?>
Note:
It is wrong to use urlencode() to process $data. Because it is the browser's responsibility to urlencode() the data. All popular browsers handle it correctly. Note that this happens regardless of the method (such as GET or POST). You'll only notice this when using GET requests, since POST requests are usually hidden.
Data waiting for user editing
<?php echo "<textarea name='mydata'>\n"; echo htmlspecialchars($data)."\n"; echo "</textarea>"; ?>
Note:
The data will be displayed in the browser as expected because Browsers interpret HTML escape symbols. When submitted, whether it is the GET or POST method, the data will be urlencoded by the browser and transmitted directly by PHP urldecode. So in the end you don't need to handle any urlencoding/urldecoding yourself, it's all handled automatically.
Example in URL
<?php echo "<a href='" . htmlspecialchars("/nextpage.php?stage=23&data=" . urlencode($data)) . "'>\n"; ?>
#Note:
In fact, this is making up an HTML GET request, so you need to manually urlencode( ).
The above is the detailed content of How to connect php to html. For more information, please follow other related articles on the PHP Chinese website!