【发布时间】:2018-07-05 04:30:54
【问题描述】:
我正在尝试在表单提交后将文本区域内容保留在其字段中。
我每次点击提交按钮时都会丢失 textarea 的内容,因为 textarea 会被清除。
表格和文本区域代码:
<form method="post" action="ProcessText.php" role="form" id="TextEntry">
<fieldset style=" height: auto; margin-left: 4%; margin-top: 4%; margin-right: 4%; border: solid 1px black !important; border-radius: 5px; padding: 0 10px 10px 10px; border-bottom: none;">
<legend style=" width: auto !important; height: auto !important; border: none; font-size: 17px"><b>Text Entry</b></legend>
<div class="form-group" style=" margin-bottom: 3px;margin-top:20px; margin-left: 5%; margin-right: 5%;">
<div style="position: absolute; margin-top: 5px"><label for="TextArea" class="label label-primary" >Text Area</label></div><br>
<div style="position: relative"><textarea style=" resize: none" id = "TextArea" name="TextArea" class="form-control"
rows = "19"
cols = "50"
style=" width: auto; height: auto; text-align: left; border-right: 1px solid#c0c0c0; " placeholder="Add some text"><?php if(isset($_POST['TextArea'])) { echo htmlentities ($_POST['TextArea']); }?></textarea></div>
</div>
<br><button style="float: right; margin-right: 5%" type="submit" class="btn btn-success" name="Submit">Analyse</button>
</fieldset>
</form>
表单和文本区域位于 index.php 页面中,该页面提交到另一个页面(即 ProcessText.php)。提交运行成功,ProcessText.php 页面按预期接收到发布的数据。
我浏览了论坛的答案并尝试了许多解决方案,例如将以下代码放在 textarea 标签中:
<?php if(isset($_POST['textarea1'])) {
echo htmlentities ($_POST['textarea1']); }?>
我也在 textarea 标签中尝试了这段代码,但没有任何好处:
<?php echo $_POST['TextArea'] ? $_POST['TextArea'] : "" ?>
为澄清起见,包含 textarea 的 index.php 页面代码:
<html>
<head>
<title>Text Sorting Form</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" type="text/css" href="css/Styles.css"/>
<script src="js/jquery-2.1.4.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<style> </style>
</head>
<body>
<div style="width: 100%; overflow: hidden;">
<div style=" background-color: white; border: 1px solid Grey;
border-radius: 3px; box-shadow: 3px 0px 3px #888888;
width:50%; max-width: 50%; height: 90%; margin-top:3%; margin-left:
3%; margin-right:auto; overflow: hidden; float: left;">
<form method="post" action="ProcessText.php" role="form" id="TextEntry">
<fieldset style=" height: auto; margin-left: 4%; margin-top: 4%; margin-right: 4%; border: solid 1px black !important; border-radius: 5px; padding: 0 10px 10px 10px; border-bottom: none;">
<legend style=" width: auto !important; height: auto !important; border: none; font-size: 17px"><b>Text Entry</b></legend>
<div class="form-group" style=" margin-bottom: 3px;margin-top:20px; margin-left: 5%; margin-right: 5%;">
<div style="position: absolute; margin-top: 5px"><label for="TextArea" class="label label-primary" >Text Area</label></div><br>
<div style="position: relative"><textarea style=" resize: none" id = "TextArea" name="TextArea" class="form-control"
rows = "19"
cols = "50"
style=" width: auto; height: auto; text-align: left; border-right: 1px solid#c0c0c0; " placeholder="Add some text"></textarea></div>
</div>
<br><button style="float: right; margin-right: 5%" type="submit" class="btn btn-success" name="Submit">Analyse</button>
</fieldset>
</form>
</div>
<div style=" background-color: white; border: 1px solid Grey;
border-radius: 3px; box-shadow: 3px 0px 3px #888888;
width:40%; margin-left: 700px; height: 90%; margin-top:3%;">
<fieldset style=" height:90%; margin-left: 4%; margin-top: 5%; margin-right: 4%; margin-bottom: 10px; border: solid 1px black !important; border-radius: 5px; padding: 0 10px 10px 10px; border-bottom: none;">
<legend style=" width: auto !important; height: auto !important; border: none; font-size: 17px"><b>Result</b></legend>
<div class="Notes" style=" margin-bottom: 3px;margin-top:40px; margin-left: 5%; margin-right: 5%; height: 75%; width: auto">
<?php
session_start();
// Display Custom Field Value
echo '<pre>';
echo nl2br("The Submitted Paragraph:\n".$_SESSION['Text']."\n\n");
echo nl2br ("Number of Words: " . $_SESSION['number']."\n\n");
echo nl2br("Word Frequency in Descending Order:\n");
print_r($_SESSION['results']);
echo '</pre>';
session_unset();
session_destroy();
?>
</div>
</fieldset>
</div>
</div>
</body>
</html>
ProcessText.php 页面代码:
<html>
<head>
<title>Text Sorting Form</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" type="text/css" href="css/Styles.css"/>
<script src="js/jquery-2.1.4.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<style>
</style>
</head>
<body>
<?php session_start(); ?>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$text = $_POST['TextArea'];
$wordcount = word_count ($text);
$words = utf8_str_word_count($text, 1);
$frequency = array_count_values($words);
arsort($frequency);
$_SESSION['results'] = $frequency;
$_SESSION['number'] = $wordcount;
$_SESSION['Text'] = $text;
header("location: index.php");
exit();
};
function utf8_str_word_count($string, $format = 0, $charlist = null) {
$result = array();
if (preg_match_all('~[\p{L}\p{Mn}\p{Pd}\'\x{2019}' . preg_quote($charlist, '~') . ']+~u', $string, $result) > 0) {
if (array_key_exists(0, $result) === true) {
$result = $result[0];
}
}
if ($format == 0) {
$result = count($result);
}
return $result;
}
function word_count ($string) {
$words_to_count = strip_tags($string);
$pattern = "/[^(\w|\d|\'|\"|\.|\!|\?|;|,|\\|\/|\-\-|:|\&|@)]+/";
$words_to_count = preg_replace ($pattern, " ", $words_to_count);
$words_to_count = trim($words_to_count);
$total_words = count(explode(" ",$words_to_count));
return $total_words;
}
?>
</body>
</html>
【问题讨论】:
-
听说过 CSS 吗?
-
为什么下面这段代码没有php部分?
-
表单提交到
ProcessText.php后,您如何返回index.php?直接 include() 或 header() 重定向?如果它是一个标头重定向,那将不包含 $_POST 变量。 -
我看到
TextArea和TextArea1 -
别对我说对不起,对以后要维护它的人说对不起。 ;p
标签: php html forms post textarea