I have a problem with javascript, Giulio Cesare's cipher - javascript

I have to create a javascript code that implements the Cipher of Caesar, I tried but it doesn't work, I think that the problem is in the for loop in the functions that I used, I thank everyone who will help me.
function Figure() {
text = eval(send.t.value);
key = eval(send.k.value);
for (i = 0; i < t.length; i++) {
tf.charCodeAt(i) = t.charCodeAt(i) + k;
if (t.charAt(i) > 'Z') {
tf.charCodeAt(i) = (('A' - 1) + (tf.charCodeAt(i) - 'Z'));
}
}
send.value.mc = messaggiocifrato;
}
<html>
<head>
<title>Cryptography of Giulio Cesare</title>
</head>
<body>
<h1>Cryptography of Giulio Cesare</h1>
<form name="send">
Enter text to encrypt
<input type="text" name="t"> <br/><br/> Enter key
<input type="text" name="k"> <br/><br/> Encrypted text.
<input type="text" name="tf"> <br/><br/>
<input type="button" value="Figura" onclick="Figure()">
<input type="reset" value="Reset">
</form>
</body>
</html>

There are a number of issues with your code, I'll point out the first one and leave the rest for you to solve (lest I write all your code for you, and undermine the lessons you'll learn by debugging yourself!)
You're not using a library which enables you to refer to the values of your fields as send.t.value or send.k.value, which is why you're getting the error message "ReferenceError: t is not defined". send is undefined, so trying to get the t property of undefined is causing this error.
You want to use something like document.getElementsByName("t")[0].value instead to get the value of t. You can read the MDN docs on this function here

Related

php cannot get value from html

I have questions that why my html file cannot connect with php file.
But I am sure that after I click the submit button, value of <input type="hidden" id="t1_response" name="t1_response" value=""> can be given.
However, url doesn't change to php form(like tocsv.php?t1_response=...).
And php gives me an notice :
Notice: Undefined index: t1_response in C:\xampp\htdocs\colors\tocsv.php on line 2
How can I fix it?
<script>
...
function pressSubmit() {
$('#startTrial').hide();
for (var i = 1; i <=3; i++) {
rt = rt + parseFloat(document.getElementById("t"+i+"_rt").value);
};
if (document.getElementById("t1_response").value === "diff") {acc=acc+1};
if (document.getElementById("t2_response").value === "diff") {acc=acc+1};
if (document.getElementById("t3_response").value === "same") {acc=acc+1};
document.write("<div>The average react time: " + rt/3 +" ms</div>");
document.write("<div>Acc Rate:" + acc/3*100+"%</div>");
}
</script>
<style>
...
</style>
<!DOCTYPE html>
<html>
<body>
<h3>Remember the colors</h3>
Press 's' if the two displays are the same. Press 'd' if a color changed (different).<br>
<form action="tocsv.php" method="get">
<input type="hidden" id="t1_response" name="t1_response" value="">
<input type="hidden" id="t1_rt" name="t1_rt" value="">
<input type="submit" id="submitButton" name="submitButton" onclick="javascript:pressSubmit()">
</form>
</body>
</html>
pressSubmit() contains calculating average number and document.write() to display the value to user.
tocsv.php
<?php
echo $_GET["t1_response"];
?>
Error code is clear: you have to define the var t1_response in your php file.
I bet you write:
t1_response = $_POST['t1_response ']
but you need:
$t1_response = $_POST['t1_response ']
The html file you post here is ok.
I have solved the problems.
Thanks everyone's help.
Here, in pressSubmit(), I wrote doucument.write(...), which make the page to be overwritten. Thus, it came out to be unable to get the value and I have no idea why so.
Thus, I use alert to avoid to overwrite the page. And everything works.
Thanks everyone's help again!
.

javascript undefined input value despite of writing in it

I have a strange problem: I am getting a undefined alert window by running this code. I looked up all the entrys here but I couldn't find a solution.
I've checked, there is no identical id except this one
running the php file the result of var_dump($user_id) shows the right value
this is why I don't understand why the value in javascript is undefined all the time...
HTML:
<form action="?atn=surveyCode" method="post" class="form mg_top">
<label id="entersID" for="usersID">Enter your prolific ID:</label>
<input id="usersID" value="" type='text' class="form-control talign_center input" name="usersID">
<button id="survey" class="btn btn-primary form-control">Go to questions</button>
</form>
JavaScript: every alert is returning "undefined". I never get in the else part. This is really strange. I've many parts like this in my code and they are working perfectly.
$("#survey").click(function(e){
var prolific = $("usersID").val();
alert(prolific);
if(prolific==undefined){
alert(prolific);
$("#entersID").addClass("error-msg");
$("#entersID").parent().addClass("has-error");
e.preventDefault();
}
else{
alert(prolific);
window.open("http://www.w3schools.com");
}
});
PHP:
private function surveyCode(){
$userID = $_POST["usersID"];
var_dump($userID); //shows a value
}
Please help me, maybe this is a silly bug but I can't find it....
Change the line into
var prolific = $("#usersID").val();
You have to add the "#" sign before the id selector like below :
var prolific = $("#usersID").val();

object HTMLInputElement error while entering a integer value to label in javascript

script code
intext = document.getElementById("intext");
totalin = document.getElementById("totalin");
function myFunction()
{
var x = document.getElementById("totalin");
x.innerHTML = intext.toString();
}
in the above code am getting object HTMLInputElement in the label with "totalin" id and textbox of number type with id = intext
i am new to javascript and i saw many other answers on similar problems but either cudn't understand the answers or they didn't worked .
thanks in advance for help.
part of Html code is as follows if required
<label for="text">input the income in numericals</label>
<input type="number" name="text" id="intext">
Submit
<label id="totalin" for="totalin">Label:</label>
i would really appreciate any help i am really in need of solution.
You need to get the value from the element:
x.innerHTML = intext.value;
You're also missing a quote close:
<input type="number" name="text id="intext">
^ RIGHT THERE

Specific number validation?

Is it possible to validate a field to a specific set of numbers using javascript? Say for example in a postcode field you can only enter four numbers to be be accepted e.g. 2747,2750, 2753, 2760, 2777. Any other number will be returned as false. I have done a bit of searching around but i can't seem to find anything of use for this specific situation and i'm quite knew to javascript so any sort of help would be great.
Should be really simple:
Live DEMO
Create a list of your numbers
Create an Object with every number as a key and whatever as a property.
If they come from the server, send and parse them as JSON.// skip
In JS, var validNumbers = JSON.parse(serverResponse); // skip
Use an Object, not an Array. Array.prototype.indexOf is slower and needs polyfill.
Object property access is O(1) and universally supported.
Your object looks like this: var validNumbers = {"2747": "2747", etc..}
Get the input from the user.
if (typeof validNumbers[userInput] !== undefined) {//valid} else {//invalid}
try this.
<html>
<head>
<script>
function validateForm() {
var x = document.getElementById("entry_834285895").value;
if ((x != 3242 )&&(x != 2342 )&&(x != 2343 )&&(x != 1111 )) {
document.getElementById("um").innerHTML = ('Invalid');
document.getElementById("um").style.color = 'Red';
}
else {
document.getElementById("um").innerHTML = ('Valid');
document.getElementById("um").style.color = 'Green';
}
}
</script>
</head>
<body>
<p>Valid numbers:<br> 3242<br>2342<br>2343<br>1111
<form name="myForm" action="" target="hiddenFrame"
onsubmit="return validateForm()" method="post" >
Name: <input id="entry_834285895" type="text" name="fname">
<input type="submit" value="Submit">
<div id="um"></div>
</form>
<iframe name="hiddenFrame" style="display:none;"></iframe>
</body>
</html>

How do I get the value of a textarea?

This is a follow up to my other question. I am asking again because this seems more a javascript question than a Google App Engine question.
I have a form
<form name="choice_form" id="choice_form" method="post" action="/g/choicehandler" onsubmit="writeToStorage()">
<textarea name="choice" rows="7" cols="50"></textarea><br />
<input type="submit" value="submit your choice">
</form>
I want to take the value of this textarea and send it to the app with formData. I tried this
var choice = document.getElementById("choice_form").value;
but I get "undefined" for the value of "choice". What am I doing wrong?
And also, if I understand correctly, the /g/choicehandler is called twice once by the form and once by the XHR. How do I fix that? Below is the handler:
class Choice(webapp.RequestHandler):
def get(self):
self.response.out.write("""
<html>
<head>
<script type="text/javascript">
var count = 0;
function writeToStorage()
{
var user = "user" + count;
count++;
localStorage.setItem("chooser", user);
var choice = document.getElementById("choice_form").value;
var formData = new FormData();
formData.append("chooser", user);
formData.append("choice", choice);
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:8086/g/choicehandler", true);
xhr.onreadystatechange = function (aEvt) {
if (xhr.readyState == 4 && xhr.status == 200){
console.log("request 200-OK");
}
else {
console.log("connection error");
}
};
xhr.send(formData);
//added this per Aaron Dufour's answer
return 0;
};
</script>
</head>
<body>
//changed onsubmit like this: onsubmit="return writeToStorage(); as per Aaron Dufour's answer
<form name="choice_form" id="choice_form" action="/g/choicehandler" method="post" onsubmit="writeToStorage()">
<textarea name="choice" rows="7" cols="50"></textarea><br />
<input type="submit" value="submit your choice">
</form>
</body>
</html>""")
UPDATE
See Aaron Dufour's answer for the solution.
choice_form is the <form>, not the <textarea>.
You need to give the <textarea> an ID and use that ID instead.
You can access the form formally using:
document.forms['choice_form']
or also:
document.forms.choice_form
Each form has an elements collection that is all the form controls. You can access them much the same way:
document.forms['choice_form'].elements['choice']
or
document.choice_form.choice
provided the names follow the rules for valid javascript identifiers. If they don't, you need to use square bracket notation:
document['choice_form']['choice']
all return a reference to the element named 'choice' in a form with name 'choice_form'. So to get the value:
document.choice_form.choice.value
(Some great answers here, but I haven’t seen one that puts it together with what you have…)
Since a form can have more than one input, you need to access value on a particular one. There are a few ways to do this, but the most straightforward might be to use the form’s elements property (only forms have this!):
document.getElementById("choice_form").elements.choice.value
As others have said, you're accessing the form, when the data you want is in the textarea. If you give the textarea an ID, that's probably the easiest way to get to its value.
After much discussion, we determined that you don't need XHR at all. Here's what your form should look like:
<form name="choice_form" id="choice_form" action="/g/choicehandler" method="post" onsubmit="writeToStorage()">
<textarea name="choice" rows="7" cols="50"></textarea><br />
<input type="hidden" name="chooser" id="form_chooser" />
<input type="submit" value="submit your choice">
</form>
And now, we use the javascript function to edit the form before allowing it to be submitted:
var count = 0;
function writeToStorage()
{
var user = "user" + count;
count++;
localStorage.setItem("chooser", user);
document.getElementById("form_chooser").value = user;
};

Categories

Resources