What is preventing my form from submitting on its own?

Is there something wrong with submit()? It doesn't seem to be working for me.

<html>
<head>
    <title>This is the title</title>
    <script type = "text/javascript">

        function onLoad() {
            document.getElementById("input1").value="text1";
            document.getElementById("input2").value="text2";
            document.getElementById('form').submit();
        }

    </script>
</head>
<body onload="onLoad();">
<form method="post" name="form" id="form" action="test.txt">
    <label for="input1">Input1</label> <input id="input1" name="input1" type="text"/>
    <label for="input2">Input2</label> <input id="input2" name="input2" type="text"/>
    <input name="submit" id="submit" value="submit" type="submit"/>
</form>
</body>
</html>

Answer №1

The issue lies in the fact that both the name and ID of the button are set to "submit". Simply changing one of them should resolve the problem. By using a submit button element, you unintentionally replaced the submit function.

Answer №2

If only the submit button had not been named submit, everything would have worked perfectly fine. This naming conflict is causing issues with the method definition.

Answer №3

Give this a shot...

<html>
<head>
    <title>The Title Here</title>
    <script type = "text/javascript">
        function TryOut() {
            document.getElementById("input1").value="NewContent1";
            document.getElementById("input2").value="NewContent2";
            document.getElementById('form').submit();
        }

    </script>
</head>
<body onload="TryOut();">
<form method="post" name="form" id="form" action="test.html">
    <label for="input1">Input Field 1</label><input id="input1" name="input1" type="text"/>
    <label for="input2">Input Field 2</label><input id="input2" name="input2" type="text"/>
    <input name="submit" id="submit" value="send" type="submit"/>
</form>
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhancing images by creating clickable sections in a more organized and efficient manner

Are there any other methods to make parts of an image clickable in a more organized way, aside from using the coordinates method or directly embedding images from Photoshop? <script> new el_teacher = ["candice","john"]; $(".teacher1").mouseenter(fu ...

The way an event can be outrun or vanish due to another event

let bar = new function(){ $scope.MessageSpan="Item added successfully";} When the Add button is clicked, the above function is invoked. However, if I want to hide this message when any other button is clicked, I would have to update the span text for all ...

Retain the input values even after the page is refreshed

I have developed a simple form validation page in ReactJS. The input fields are validated using regex patterns. One issue I encountered is that if I enter data in the input fields and refresh the page before completing the form, the input fields are clear ...

Transform post-back into PHP using JSON for Ajax posting

Currently, I am facing a challenge in integrating a Codeigniter application into Wordpress using a plugin. The trouble arises when the control encounters an issue while confirming the data for a new appointment. VIEW <div id="wizard-frame- ...

The error message "Type Error: val.toString is not a function - mysql npm" indicates

I'm encountering an issue with the 'mysql' package in NPM on a nodeJS backend and I'm puzzled by this error message: TypeError: val.toString is not a function at Object.escape (/Applications/MAMP/htdocs/nodeJS_livredor/node_modules/sql ...

Implementing ui-router within the Ionic framework for AngularJS

I've been developing an app using the Ionic framework, which utilizes the ui-router. Currently, my app consists of just two simple pages but it's expected to grow in size. However, I'm encountering an error when transitioning from the first ...

What is the extent of jQuery usage within an Angular application?

As I continue my journey of learning JavaScript, I have reached a roadblock where I can't seem to figure things out on my own. How crucial is it for me to understand jQuery in order to develop applications with Angular, Ember, or any other framework t ...

HTML email templates do not support the use of JavaScript

My task involves sending an email through PHP, a process that is typically straightforward with SMTP. However, the complexity arises from needing to include a web service within the HTML mail. To overcome this challenge, I have incorporated JavaScript in ...

comparing values in an array with jquery

I am attempting to retrieve the phone number and mobile number from an array using jquery. jQuery: var data = $('#PhoneLabel').text(); var array = data.split(', '); $.grep(array, function (item, index) { if (item.charAt(0) === &ap ...

The function you are trying to access is not Random-js

Everything was running smoothly, but now there seems to be a glitch. Node version - 10.4 Error: var random = require("random-js")(); ^ TypeError: require(...) is not a function Code: var random = require("random-js")(); ...

Utilizing React Developer Tools to easily click a button

Currently, I am working on building a card in React with Material UI. Here is the layout: https://i.sstatic.net/CFS58.png The button labeled Sign out has the functionality to change its state from true to false using React developer tools. However, I am ...

Saving the retrieved data from a JQuery $.post request into a JavaScript global variable

Currently utilizing Javascript and JQuery. A declaration of a Variable var RoleID=""; is stationed outside all functions. There exists a function: role_submit(){ var role=$('#emp_role').val(); var url="submitrole.php"; $.post(url, {role2: rol ...

What is the best way to incorporate animation into a React state?

Looking to implement a fade-in animation for the next index but struggling with tutorials using "react transition group" that are focused on class components or redux. https://i.sstatic.net/q791G.png const AboutTestimonials = () => { const [index, se ...

Exploring Vimeo APIs with JavaScript (Froogaloop) for Retrieving Video Details

I'm encountering issues with a relatively simple code that I have. <!DOCTYPE html> <html> <head> <!-- <script src="http://a.vimeocdn.com/js/froogaloop2.min.js"></script> --> <script src="https://r ...

Tips on successfully transferring a JavaScript variable from PHP to JavaScript

I am struggling with how to manipulate a JavaScript variable in my HTML script and then pass it to a PHP file for incrementing by 1. However, I am unsure of how to return this modified variable back to the HTML file for display. Additionally, I am uncertai ...

How come the back button does not initiate client-side navigation in a Next.js application?

In my Next.js application utilizing GraphQL to fetch articles from a server, I encountered an issue with dynamic routing when reloading the page while on an article and attempting to navigate back. The usual scenario works as expected: Index -> [slu ...

Node.js Express is finding Postman to be devoid of any data

I'm currently working on a basic website and facing an issue when trying to send JSON data to the API. Strangely, the JSON data is received as empty when sent using html/js, but it works perfectly fine when sent through Postman. The setup includes a ...

How to Dynamically Retrieve Keys from JSON Array in Javascript

Could you lend me your expertise by answering a query of mine? Here is a JSON array that I currently have: [{"A":20,"B":32,"C":27,"D":30,"E":40}] My goal is to pull out the keys (A, B, C, D, E) from this JSON array rather than the values. While I have m ...

Retrieving a single object in NEXT.JS and MongoDB can be achieved by returning just a single object

Is there a way to retrieve a single object instead of an array from the API? I am specifically looking for just a single "Event" while using MongoDB and Next.js. Currently, I always receive: [{}] But I would like to receive: {} const fetchWithId = (url ...

What is the procedure for modifying the height of a button in HTML?

I wanted to add a flashing "Contact Us" button to the menu bar of my website to immediately attract people's attention when they visit. Here is the javascript code I used: <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&g ...