How to access information in a javascript file that was send from a form?

175 views Asked by At

I have this following code:

<form action="main.js">
        <input type="text" required="required" pattern="[a-zA-Z]+" />
        <input type="submit" value="Submit" />
    </form>

When i click the submit button, the information that was in the input should be sent to my file "main.js". But there is nothing in "main.js". I want that "main.js" file would contain that passed information as a string, is there a way or method to do this?

2

There are 2 answers

2
Noam Smadja On BEST ANSWER

Seems like you've understood form action incorrectly.

Action defines which code will handle your form values, and not which page will the results be pasted into.

you'll want main.js to receive the form results and handle them in a way to be pasted into a results.txt file for example. But allowing a user of your website to create or edit files on your server is insecure.

The only option i think of, unless you have access to server side coding, like php or asp, is sending the submitted form information to your email using mailto:

<!DOCTYPE html>
<html>
<body>

<h2>Send e-mail to [email protected]:</h2>

<form action="MAILTO:[email protected]" method="post" enctype="text/plain">
Name:<br>
<input type="text" name="name" value="your name"><br>
E-mail:<br>
<input type="text" name="mail" value="your email"><br>
Comment:<br>
<input type="text" name="comment" value="your comment" size="50"><br><br>
<input type="submit" value="Send">
<input type="reset" value="Reset">
</form>

</body>
</html>

can you run asp or php?

1
Amnesh Goel On

Following could be the part of your javascript file.. If you are not going to include any JS file then you can use it directly.

function checkAge() {
    var x = document.forms["Form1"]["Age"].value;
    if (x == null || x == "") {
        alert("Age is empty");
        return false;
    }
    else
    alert(x);
}

Then your form should looks like

<form name="Form1" action="abc.jsp" onsubmit="return checkAge()" method="post">
Age: <input type="text" name="Age">
<input type="submit" value="Submit">
</form>

Cross check with your form and see what went wrong..