Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 11 months ago.
Improve this question
I want the submit button's to call the function only if the input fields meet the requirements set. Is there an easier way to do it than making an if statement for each input element?
<div class="container">
<form>
<input id="inputname" type="text" name="name" required placeholder="Your Name">
<input id="email" placeholder="Your E-mail" type="email" name="email" required>
<input id="phone" type="tel" name="phone" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" placeholder="123-456-7890" required>
<textarea type="text" rows="4" placeholder="Add comments(optional)" id="comments"></textarea>
<button type="submit" onclick="submitted()"> Reserve a Table</button>
</form>
</div>
<script>
const container = document.querySelector('.container');
function submitted() {
container.innerHTML = `<i class="fa-regular fa-circle-check"></i><br>
Thank you, the form has been submitted!`
}
</script>
Yes. See this tutorial for more details on form validation.
In particular, under the header Using built-in form validation:
When an element is invalid, the following things are true:
...
If the user tries to send the data, the browser will block the form and display an error message.
See also the note under the constraint validation process to learn more about when this happens.
The originally posted sample is shown this Fiddle but lacked a dot at the start of the container selector.
As a solution here, you can move the function call to the onsubmit handler of the form, instead of the click handler of the submit button. That fiddle is here and includes just the following changed tags: <form onsubmit="submitted()"> and <button type="submit">.
Related
This question already has answers here:
What is AJAX and how does it work? [duplicate]
(1 answer)
How to submit an HTML form without redirection
(12 answers)
Closed 5 years ago.
I have a very simple html form I have created for a contact page. I would like to know if it is possible to disable/avoid the page redirect that occurs when the form is sent but still allow for the form to be submitted (obviously). I essentially have almost zero knowledge of php at this time and I did not create the php aspect (see form action). Any help here is greatly appreciated.
<form action="http://sonic.dawsoncollege.qc.ca/~dpomerantz/web/contact_me.php" method="post" class="flex flex--column">
<input type="hidden" name="toEmail" value="spencer.miller#me.com"/>
<input type="text" name="name" placeholder="Name*" required><br>
<input type="email" name="email" placeholder="Email*" required><br>
<input type="text" name="phone" placeholder="Phone"><br>
<input type="text" name="message" placeholder="Message*" required><br>
<input type="submit" value="Submit" class="submit">
</form>
Configure the form to post to a hidden iframe, as explained here: How do you post to an iframe?. The redirect will still happen, but it will be within the iframe and thus invisible to the user. It's a bit of a hack, and not the best way to do things, but it does work.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
<input name="input1" id="input1" type="text" value="" class="problem">
<input name="input2" id="input2" type="text" value="" class="hidden">
I'm trying to populate a hidden text input (e.g. input#input2) field with the current value from another text input field (e.g. input#input1) (un-submitted) using jQuery. I've sourced this jsfiddle doing exactly what i require but with a drop down field. And i was wondering if anyone is able to point me in the right direction?
EDIT: Sorry if i wasn't clear, the jsfiddle works perfectly but i'm trying to use a text field instead of a drop down to populate the other field.
i always handle this by storing that value in a variable:
var yourText = document.getElementById("input1").value;
and then just plug it into the hidden field:
$('#input2').val(yourtext);
here's your hidden field:
<input id="input2" type="hidden" />
$("#my_form").submit(function(event){
var visibleInputVal = $("#input1").val();
if(visibleInputVal != ""){
$("#input2").val(visibleInputVal);
}else{
alert("you need to provide a value for input 1");
event.preventDefault();
}
});
<form id="my_form">
<input type="text" value="" id="input1" />
<input type="hidden" value="" id="input2" />
<input type="submit" value="Submit" />
</form>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a form and it has several fields. There is an ID field which is generated from another form. Now i want to prevent users to change that field value.
How do i do this? DO i need any javascript or anything else?
You can use disabled on the field:
<input type='text' disabled />
Or readonly
<input type='text' readonly />
<input type="text" name="NAME-OF-FIELD" value="VALUE-NOT-TO-CHANE" disabled="disabled" />
or you could use
<input type="text" name="NAME-OF-FIELD" value="VALUE-NOT-TO-CHANE" readonly />
or if you need to hide it
< input type="hidden" name="NAME-OF-FIELD" value="VALUE-NOT-TO-CHANE" />
You will still have access to it when you submit the form.
If you can change the HTML, I'd suggest making the input hidden with type="hidden".
If you can't, apply the CSS style display:hidden to it.
Or did you want to leave it visible?
You have to disable the field:
<input type="text" disabled="disabled" />
Or set it readonly
<input type="text" readonly="readonly" />
A readonly element not editable, but gets sent when the form submits. a disabled element isn't editable and isn't sent on submit.Readonly elements can be focused while disabled elements can't.
<input type="text" readonly />
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I want to use JavaScript to wipe out default text and allow user to enter a his choice word. How do I integrate this thing with my HTML code. I need it for testing purpose. Below is my HTML code.
<html>
<body bgcolor="black"><form method="get" action ="http://localhost:2013">
<center>
<font color="white" size=65>Enter Word:
<input type="text" name="word"></font></center>
</br><center>
<font color="Green" size=65>
<input type="submit" value="SUBMIT"></font></center><
form>
</body>
</html>
Any suggestions?
<!DOCTYPE html>
<body bgcolor="black"><form method="get" action ="http://localhost:2013">
<center>
<font color="white" size=65>Enter Word:
<input type="text" name="word" placeholder='Default Text'></font></center>
</br><center>
<font color="Green" size=65>
<input type="submit" value="SUBMIT"></font></center><
form>
</body>
</html>
Just add placeholder='Default Text' to your input element.
No need of much items, this will help you...
<form action="demo_form">
<input type="text" name="fname" placeholder="Your default text"><br>
<input type="text" name="lname" placeholder="Your default text"><br>
</form>
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm working on the following page:
http://mockingbirdagency.com/thebox/bettercss/login-1.html
and would like for the right part of the first sign up box to appear only once "Register with your email address" is clicked on.
I know very little Javascript but I'm guessing that's what I'll need to do that, can somebody point me to a tutorial ?
Cheers
(Edit: The reason I am asking for a link is that I wouldn't know what to look for on Google)
Assuming you'll add an id to these 2 elements:
<div class="form-step-1-container" id="myForm"> <!-- <-- ID here -->
<h1>Register with your email address</h1>
<div style="display:none;" id="targetDiv"> <!-- <-- ID here -->
<input id="username" type="text" placeholder="Choose a Username" required="required" name="username">
<input id="email" type="text" placeholder="Enter your Email" required="required" name="email">
<input id="password" type="text" placeholder="Enter your Password" required="required" name="password">
<input id="password" type="text" placeholder="Re-Enter your Password" required="required" name="password">
</div>
</div>
With native JavaScript:
document.getElementById('myForm').addEventListener('click', function(){
document.getElementById('targetDiv').style.display = "block";
});
This adds a "click" event listener to the "form-step-1-container" div, that displays the div containing the input elements, when clicked.
So, what does this actually do?
document.getElementById('myForm') "Gets" the DOM elements with id="myForm".
.addEventListener() adds an event listener to this object. It's first parameter ('click') specifies what event it should listen ("react") to, it's second parameter is a function to execute when the event happens. This function can be defined in the addEventListener(), like I did up here, but you can also define the function elsewhere in the code, and just use the function name in the addEventListener().
.style.display = "block" sets the element's display CSS style to the value "block", to display the inputs fields again.
Your code would look like this :
HTML
<div class="form-step-1-container">
<h1>Register with your email address</h1>
<div style="display:none;">
<input id="username" type="text" placeholder="Choose a Username" required="required" name="username">
<input id="email" type="text" placeholder="Enter your Email" required="required" name="email">
<input id="password" type="text" placeholder="Enter your Password" required="required" name="password">
<input id="password" type="text" placeholder="Re-Enter your Password" required="required" name="password">
</div>
</div>
JS
$(document).ready(function() {
$('.form-step-1-container h1').click(function() {
$('.form-step-1-container div').css('display', 'block');
});
});
Put the HTML element which contains the elements you want to hide into a common HTML element like a <div>. Then assign a CSS class to it which includes the style "display:none". You can do that in the HTML code, but a better solution would be to assign it with Javascript when the document loads, because in that case the input will be visible for those who have Javascript disabled.
Then add a onclick handler to the the div which replaces the CSS class with one which has dispay:block.