PHP - 从表单中提取可变数据

时间:2014-01-20 20:39:36

标签: 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> -->
                <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中输入消息和数字并输入。然后运行代码,移动字符串中每个字母的值。我试图让textarea占位符在按下“encode”时打印编码的消息来代替占位符。对于输入,我使用以下代码实现了这一点:

  

占位符= “”

对于textarea,我试图将变量$ newstring打印为占位符。我有:

  

占位符= “”

我希望$ _POST ['msg']为$ _POST [$ newstring],但它不起作用。

关于如何做到这一点的任何想法?

2 个答案:

答案 0 :(得分:0)

您的代码有效;唯一的问题是编码的字符串可能包含非打印字符,这将导致麻烦。我刚用以下输入测试了它:

enter image description here

问题是您确实需要确保“编码”字符串不包含任何非打印字符。看来你正在实现一个Caesar cypher - 为了有效地这样做,我会使用模运算(你在代码中对偏移量使用了模运算,然后将其注释掉)。因此

$oldChar = strtoupper($input);
$newChar = (($oldChar - ord('A')) + $offset) % 26 + ord('A');

如果需要,可以添加一行

if($input!=strtoupper($input)) $newChar = strtolower($newChar);

(据我所知,php没有内置的“isupper()”函数......)

注意 - 这会破坏所有标点符号等。如果你没有通过测试开始你有一个有效的字符,你可以引入各种奇怪的字符(在下面的例子中,空格变成了逗号......)。实际上,如果您的初始代码中包含ascii值123,那么您就会敬酒。

答案 1 :(得分:0)

我已经为您的问题编写了可能的解决方案 - 请参阅下面的代码。您可以在http://www.floris.us/SO/codeForm.php

找到此工作副本

解决您的具体问题的关键是靠近底部;我用

<script type="text/javascript">
document.forms['codeForm'].codeMsg.value = <?php echo '"'.$newString.'"'; ?>;
document.forms['codeForm'].msg.value = <?php echo '"'.$string.'"'; ?>;
</script>       

获取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>