PHP-从表单中提取变量数据
我有以下代码:PHP-从表单中提取变量数据,php,forms,variables,post,placeholder,Php,Forms,Variables,Post,Placeholder,我有以下代码: <form class="form-inline" role="form" method="post"> <div class="table-responsive"> <table> <tr class="tr_top"> <!-- <th class="th_left_top">Message:</th> -->
<form class="form-inline" role="form" method="post">
<div class="table-responsive">
<table>
<tr class="tr_top">
<!-- <th class="th_left_top">Message:</th> -->
<td class="td_top">
<textarea class="form-control" rows="3" name="msg" placeholder="<?php if (isset($_POST['encode'])) { echo htmlspecialchars($_POST['msg']);} else { echo " Your message here. ";} ?>" onfocus='this.select()'></textarea>
</td>
</tr>
<tr class="tr_mid">
<!-- <th class="th_left_mid">Shift Parameter:</th> -->
<td class="td_mid">
<input type=text class="form-control input_mid" name="offset" placeholder="<?php if (isset($_POST['encode'])) { echo htmlspecialchars($_POST['offset']);} else { echo " Enter a number. ";} ?>">
</td>
</tr>
<tr class="tr_bottom">
<!-- <th class="th_bottom empty"></th> -->
<td class="td_bottom">
<input class="input_bottom btn btn-default" type="submit" name="encode" value="Encode">
<input class="input_bottom btn btn-default" type="submit" name="decode" value="Decode">
<input class="input_bottom btn btn-default" type=button value='Clear' onclick='this.form.elements.msg.value=""' </td>
</tr>
</table>
</div>
<!-- close table-responsive -->
</form>
<p>Original message:
<?php
$string = $_POST['msg'];
echo "<p class='string ital'>" . $string . "</p>";
$newstring = $string;
$sp = $_POST['offset'];
//$offset = $sp % 26;
//$encode = $_POST['encode'];
$decode = $encode - $offset;
//echo "<p>sp = " . $sp . "</p>";
//echo "<p>offset = " . $offset . "</p>";
//echo "<p>decode = " . $decode . "</p>";
for ($i=0; $i < strlen($string); $i++) {
$ascii = ord($string[$i]);
for ($j=0; $j < $sp; $j++) {
if ($ascii == 90) { //uppercase bound
$ascii = 65; //reset back to 'A'
}
else if ($ascii == 122) { //lowercase bound
$ascii = 97; //reset back to 'a'
}
else {
$ascii++;
}
}
$newstring[$i] = chr($ascii);
}
echo "<p>Encoded message:</p>";
if (isset($_POST['encode'])) {
echo "<p class='string ital'>" . $newstring . "</p>";
} elseif (isset($_POST['decode'])) {
echo "<p class='string ital'>" . $decode . "</p>";
} else {
//echo "<p class='string ital'></p>";
//echo "<p class='string ital'></p>";
}
?>
你的代码有效;唯一的问题是编码字符串可能包含非打印字符,这将导致问题。我刚刚用以下输入对其进行了测试:
问题是您确实需要确保“编码”字符串不包含任何非打印字符。似乎您正在实现一个Caesar密码-为了有效地实现,我将使用模运算(您在代码中对偏移量使用模运算,然后将其注释掉)。因此
如果需要,可以添加一行
if($input!=strtoupper($input)) $newChar = strtolower($newChar);
(据我所知,php没有内置的“isupper()”函数…)
注意-这会破坏所有标点符号等。如果你不开始测试你是否有一个有效的字符,你可能会引入各种奇怪的字符(在下面的例子中,空格变成了逗号…)。事实上,如果您的初始代码中包含ascii值123,您就完蛋了。我已经为您的问题编写了一个可能的解决方案-请参阅下面的代码。您可以在以下网址找到此文件的工作副本:
解决具体问题的关键就在底部附近;我用
<script type="text/javascript">
document.forms['codeForm'].codeMsg.value = <?php echo '"'.$newString.'"'; ?>;
document.forms['codeForm'].msg.value = <?php echo '"'.$string.'"'; ?>;
</script>
document.forms['codeForm'].codeMsg.value=;
document.forms['codeForm'].msg.value=;
获取php变量的值并将其放入表单的输入区域
<html>
<form class="form-inline" name="codeForm" role="form" method="post">
<div class="table-responsive">
<table>
<tr class="tr_top">
<!-- <th class="th_left_top">Message:</th> -->
<td class="td_top">
Plain text message<br>
<textarea class="form-control" rows="3" name="msg"
<?php
if (isset($_POST['encode'])) {
echo 'value = "'.htmlspecialchars($_POST['msg']).'"';
}
else {
echo 'placeholder = "Your message here"';
}
?>
onfocus='this.select()'>
</textarea>
</td>
<td class="td_top">
Coded message<br>
<textarea class="form-control" rows="3" name="codeMsg"
<?php
if (isset($_POST['decode'])) {
echo "value=".htmlspecialchars($_POST['codeMsg']);
}
else {
echo 'placeholder = "Encoded message here. "';
}
?>
onfocus='this.select()'>
</textarea>
</td>
</tr>
<tr class="tr_mid">
<td class="td_mid">
<input type=text class="form-control input_mid" name="offset"
<?php
if (isset($_POST['encode'])) {
echo 'value = "'.htmlspecialchars($_POST['offset']).'"';
}
else {
echo 'placeholder = "Enter a number:"';
}
?>
>
</td>
</tr>
<tr class="tr_bottom">
<!-- <th class="th_bottom empty"></th> -->
<td class="td_bottom">
<input class="input_bottom btn btn-default" type="submit" name="encode" value="Encode">
<input class="input_bottom btn btn-default" type="submit" name="decode" value="Decode">
<input class="input_bottom btn btn-default" type=button value='Clear' onclick='this.form.elements.msg.value=""' </td>
</tr>
</table>
</div>
<!-- close table-responsive -->
</form>
<?php
$string = defaultKey($_POST, 'msg', '');
$offset = defaultKey($_POST, 'offset', 0);
$newString = defaultKey($_POST, 'codeMsg', '');
if(isset($_POST['encode'])) {
$newString = caesar($string, $offset);
}
if(isset($_POST['decode'])) {
$string = caesar($newString, -$offset);
}
function caesar($input, $offset) {
$output = array();
foreach(str_split($input) as $k=>$i) {
if (ctype_alpha($i)) {
$oldChar = strtoupper($i);
$newChar = chr(((ord($oldChar) - ord('A')) + $offset) % 26 + ord('A'));
if($i!=strtoupper($i)) { $newChar = strtolower($newChar); }
}
else {
$newChar = $i;
} // don't do anything with punctuation
$output[$k]=$newChar;
}
$output = implode($output);
return $output;
}
function defaultKey($arr, $key, $default) {
if(isset($arr[$key])) {
return $arr[$key];
}
return $default;
}
?>
<script type="text/javascript">
document.forms['codeForm'].codeMsg.value = <?php echo '"'.$newString.'"'; ?>;
document.forms['codeForm'].msg.value = <?php echo '"'.$string.'"'; ?>;
</script>
</html>
纯文本消息
onfocus='this.select()'>
@僵尸兔子,是的,没错。目前,我对非印刷字符没什么意见。我可以在以后修理。我真的在寻找一个占位符作为编码字符串的帖子。尽管如此,我还是很感激你的代码;它会派上用场的。那么,有人有什么想法吗?@winthropite-重新表述一下你的问题:你想发出一个POST请求,使用php变量的内容作为POST的内容(而不是表单的内容)。我理解对了吗?您希望如何通过单击按钮触发该请求?你可能会考虑改写你的问题(或者甚至问一个新的问题),这样你就不会因为你想解决的问题而忽略那些细节。即便如此,你真正想要实现的是什么?你的问题越干净(“越纯净”),就越容易给出一个好的答案……我有一个表格,其中输入了x数据。数据存储为变量$string=$\u POST['msg'];。然后处理该数据,结果为$newstring=$string;。我想使用以下代码将变量$newstring反馈回表单:是否希望占位符的内容成为变量$newstring
中的内容?或者POST请求是否包含另一个元素,其索引是$newstring
的内容?或者其索引等于文字字符串“$newstring
”(美元符号和全部)?
<html>
<form class="form-inline" name="codeForm" role="form" method="post">
<div class="table-responsive">
<table>
<tr class="tr_top">
<!-- <th class="th_left_top">Message:</th> -->
<td class="td_top">
Plain text message<br>
<textarea class="form-control" rows="3" name="msg"
<?php
if (isset($_POST['encode'])) {
echo 'value = "'.htmlspecialchars($_POST['msg']).'"';
}
else {
echo 'placeholder = "Your message here"';
}
?>
onfocus='this.select()'>
</textarea>
</td>
<td class="td_top">
Coded message<br>
<textarea class="form-control" rows="3" name="codeMsg"
<?php
if (isset($_POST['decode'])) {
echo "value=".htmlspecialchars($_POST['codeMsg']);
}
else {
echo 'placeholder = "Encoded message here. "';
}
?>
onfocus='this.select()'>
</textarea>
</td>
</tr>
<tr class="tr_mid">
<td class="td_mid">
<input type=text class="form-control input_mid" name="offset"
<?php
if (isset($_POST['encode'])) {
echo 'value = "'.htmlspecialchars($_POST['offset']).'"';
}
else {
echo 'placeholder = "Enter a number:"';
}
?>
>
</td>
</tr>
<tr class="tr_bottom">
<!-- <th class="th_bottom empty"></th> -->
<td class="td_bottom">
<input class="input_bottom btn btn-default" type="submit" name="encode" value="Encode">
<input class="input_bottom btn btn-default" type="submit" name="decode" value="Decode">
<input class="input_bottom btn btn-default" type=button value='Clear' onclick='this.form.elements.msg.value=""' </td>
</tr>
</table>
</div>
<!-- close table-responsive -->
</form>
<?php
$string = defaultKey($_POST, 'msg', '');
$offset = defaultKey($_POST, 'offset', 0);
$newString = defaultKey($_POST, 'codeMsg', '');
if(isset($_POST['encode'])) {
$newString = caesar($string, $offset);
}
if(isset($_POST['decode'])) {
$string = caesar($newString, -$offset);
}
function caesar($input, $offset) {
$output = array();
foreach(str_split($input) as $k=>$i) {
if (ctype_alpha($i)) {
$oldChar = strtoupper($i);
$newChar = chr(((ord($oldChar) - ord('A')) + $offset) % 26 + ord('A'));
if($i!=strtoupper($i)) { $newChar = strtolower($newChar); }
}
else {
$newChar = $i;
} // don't do anything with punctuation
$output[$k]=$newChar;
}
$output = implode($output);
return $output;
}
function defaultKey($arr, $key, $default) {
if(isset($arr[$key])) {
return $arr[$key];
}
return $default;
}
?>
<script type="text/javascript">
document.forms['codeForm'].codeMsg.value = <?php echo '"'.$newString.'"'; ?>;
document.forms['codeForm'].msg.value = <?php echo '"'.$string.'"'; ?>;
</script>
</html>