(jsfiddle example here: caesar cipher)
I am making a Caesar cipher - it's an alphabetic shift, basically; a shift parameter of 3 is as follows: a => d, b => e, c => f, ...
My code so far is
$string = "hello";
echo "<p>" . $string . "</p>";
$newstring = "hello";
for ($i=0;$i<strlen($string);$i++) {
$ascii = ord($string[$i]);
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>" . $newstring . "</p>";
and it works okay.
I would like a user to be able to enter their own string into a form's textarea as follows:
<form>
<table>
<tr><th class="w">Message:</th>
<td><textarea class="t" onfocus='this.select()' name=msg>HELLO</textarea></td>
</tr>
<tr><th>Shift Parameter:</th>
<td><input type=text size=2 name=sp value='11'></td>
</tr>
<tr><td></td><td><input type=submit name=submit value='Encode'>
<input type=submit name=submit value='Decode'>
<input type=button value='Clear' onclick='this.form.elements.msg.value=""'</td></tr>
</table>
</form>
I just need to change the PHP to output the table, but don't quite know how. So, in textarea, a user could type "TESTING", choose a shift parameter of 8, and click "Encode" and that would run an encode function; likewise, "Decode" would run a decode function.
Any ideas how to change the PHP code to make it work the way I'd like?