0

I have the following code written in html

<form id = "name" action="../js/customerData.js">
    First name: <input id = "firstName" type="text" name="firstName" placeholder="First Name"><br>
    Last name: <input id = "lastName" type="text" name="lastName" placeholder="Last Name"><br>
    <input type="submit" name="FLnames" id="FLnameVar" value="Submit">
</form>

And this code written in javascript which is what the form action calls

firstName = document.getElementByName("firstName");
lastName = document.getElementByName("lastName");
document.write(firstName);

However, when I submit the html form the browser just displays the javascript. Does anyone know why this is and how I can fix it? Thanks

7
  • what is mean by browser is showing only javascript. Commented Nov 2, 2019 at 20:08
  • After I press the submit button the browser just shows the JavaScript code Commented Nov 2, 2019 at 20:12
  • can you post the javascript code? Commented Nov 2, 2019 at 20:13
  • I did, it is the second code snippet Commented Nov 2, 2019 at 20:14
  • Post the code in customerData.js Commented Nov 2, 2019 at 20:18

4 Answers 4

0

Your form action is sending a post/get request to "../js/customerData.js"

your url would look like this

/js/customerData.js?first-name=jio&lastName=jio&FLnames=Submit

Usually you would use a server side language to handle the data from the URL where you could get those values through $_POST["first-name"] (PHP)

If you sending it to a javaScript file it is not "parsed" The browser sees it as a text file .In order to get the Javascript to work , make it an html file (or server file ) and tell the browser you running a script with the script tag

        <script type="text/javascript">          
         //...
        </script>.  

Alternatively if you using JS use the

!#

in form action i.e (<form id = "name" action="!#">)

then use an JavaScript event listener to listen for the form submit

var ele = document.getElementById('name');
if(ele.addEventListener){
    ele.addEventListener("submit", callback, false);  //Modern browsers
}else if(ele.attachEvent){
    ele.attachEvent('onsubmit', callback);            //Old IE
}

In the callback use JS to get the elements Value

function Callback ()
{
let fname =  document.getElementById("firstName").innerHTML;
// then you use the value to post 
}

The last stage using this method to save it is to use something like ajax to send to a server

I hope this helps you .

Sign up to request clarification or add additional context in comments.

7 Comments

I think we can safely never ever bring up attachEvent() ever again. Anyone still working with IE 8 deserves what they get.
I agree but for legacy reference and its default on a windows pc running anything less than XP
It's only used in IE 8 or less (that's 2009 by the way) and as I say, that's not worth keeping up with.
this seems to work better than before and it does not show the javascript code anymore but i do get an error that says it cannot read property 'addEventListener' of null
Ok so that means that var ele = document.getElementById('name'); is not finding your form by ID what is the ID of the form <form id = "name" ... easy to use google debug tools using console.log(ele) One possible cause could be that you using the JS before the HTML Use the script tags after the html
|
0

An HTML form submits its data to a resource that can provide a response. Since your form submits directly to a .js file, the browser just displays it because the HTML page that submitted the data is no longer loaded in the browser. There are no elements to get and no document to scan.

You need to post your form data to a resource that can process HTTP requests and provide an HTTP response like a .php file.

See this for an overview of processing form data (scroll to the "On the server side: retrieving the data" section). It gives several examples in various server-side languages.

Also, getElementByName() doesn't exist, it's getElementsByName(). But, you won't be able to use that when looking at the submitted form data because (again) that document and all of its elements will no longer be loaded. Instead, when processing form data, you must query the request object.

Comments

0

A few improvements:

  • Please do all styling in css and avoid <br> statements to structure your content
  • No need to use action attribute. Just place your js code in a separate script tag or load them in using a script tag with a src attribute.
  • Use the html <label> tag for your labels
  • To get the form values, register a form submit handler and get the values from e.target.elements
  • Don´t use document.write ( Why is document.write considered a "bad practice"? )
  • Instead, use something like document.body.append

const form = document.getElementById("name")

form.addEventListener("submit", e => {
  e.preventDefault();
  
  // Read all form values 
  const lastName = e.target.elements.lastName.value;
  
  // Write text to dom
  const ele = document.createTextNode(lastName); 
  document.body.append(ele);
})
<form id="name">
    <label for="firstName">
      First name:
    </label>
    <input id = "firstName" type="text" name="firstName" placeholder="First Name">
    <label for="lastName">
      Last name:
    </label>
    <input id = "lastName" type="text" name="lastName" placeholder="Last Name">
    <input type="submit" name="FLnames" id="FLnameVar" value="Submit">
</form>

Comments

-1

event.preventDefault() prevents that if you dont want server sided things

function send(event)
{
    alert("the form has been sent");
    event.preventDefault();
}

<input onsubmit("send(event)")>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.