У меня есть следующий код:PHP - Извлечение переменных данных из формы
<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>";
}
?>
То, что я ищу, чтобы сделать это пользователь вводит сообщение и номер в текстовом поле и вводе. Затем выполняется код, который сдвигает значения каждой буквы в строке. Я пытаюсь заставить заполнитель textarea печатать закодированное сообщение вместо заполнителя при нажатии «encode». Для ввода, я этого достиг с этим кодом:
заполнителя = «»
Для текстового поля, я пытаюсь получить переменный $ NewString для печати в качестве заполнителя. У меня есть:
заполнитель = ""
Я хочу $ _POST [ 'сбщ'], чтобы быть $ _POST [$ NewString], но это не будет работать.
Любые мысли о том, как это сделать?
@ ZombieBunnies, да, точно. – winthropite