Question

I have the following code:

<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>";
                }               

                ?>

What I am looking to do is a user enters a message and a number in a textarea and input. Code then runs that shifts the values of each letter in the string. I'm trying to get the textarea placeholder to print the encoded message in place of the placeholder when "encode" is pushed. For the input, I achieved this with this code:

placeholder=""

For the textarea, I am trying to get the variable $newstring to print as the placeholder. I have:

placeholder=""

I want $_POST['msg'] to be $_POST[$newstring] but it won't work.

Any thoughts on how to do this?

Was it helpful?

Solution

I have written a possible solution for your problem - see code below. You can find a working copy of this at http://www.floris.us/SO/codeForm.php

The key to solving your specific problem is right near the bottom; I use

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

to take the value of a php variable and put it in an input area of the form.

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

OTHER TIPS

Your code works; the only problem is that the encoded string may contain non-printing characters which will cause trouble. I just tested it with the following inputs:

enter image description here

The problem is that you really need to make sure that the "encoded" string doesn't contain any non-printing characters. It seems that you are implementing a Caesar cypher - to do so efficiently I would use modulo arithmetic (you used a modulo operation on the offset in your code, then commented it out). Thus

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

If you want, you can add a line

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

(as far as I know, php doesn't have a built in "isupper()" function...)

Note - this destroys all punctuation etc. If you don't start by testing that you have a valid character you could introduce all kinds of strange characters (in the example below, the space became a comma...). As it is, if your initial code had ascii value 123 in it, you would be toast.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top