I am a newbie to javascript and am currently building a website with a form that navigates users to a different page if they enter the correct code into a form and press the 'Submit' button.
Currently users have to click the 'Submit' button on the page in order for it to work — they can't use the 'Go' button on virtual keyboard on devices like tablets or iPhones at the moment. I want to fix the site so that users can also use the 'Go' button on their tablets and smartphones as an alternative to having to click the 'Submit' button'.
My code:
The button:
<button class="btn btn-info btn-large span12" id="joe_btn" onclick="onSubmit()" touchstart="onSubmit()">Submit</button>
The Javascript that runs it:
<script type="text/javascript">
function onSubmit() {
if (document.getElementById('password').value == '03010213') {window.location.href = 'map.html'; }
else{ alert('Please check your passcode and try again');}
}
</script>
How do I fix the code so that users can use the 'Go' button on their tablets and smartphones and it works as well?
Any feedback would be gratefully recieved! Please note, I am a complete beginner with Javascript, so please show the exact corrected code in your answers that I would need to use.
Many thanks,
Joe
I believe that in order for the Go button to show up, you need an input type="submit" on the page. On click of that button, run your function and return false. An actual form submit will not go through, as your script will take you to a different URL. Note that the inputs need to be part of a form as shown in the answer and in the fiddle. Here is a JSFiddle showing it working: http://jsfiddle.net/3TG6w/3/embedded/result - browse to this on your mobile device.
<form method="post" action="home.html">
<input type="password" id="password"/>
<input type="submit" class="btn btn-info btn-large span12" id="joe_btn" onclick="onSubmit(); return false;" touchstart="onSubmit(); return false;" value="Submit" />
</form>
<script type="text/javascript">
function onSubmit() {
if (document.getElementById('password').value == '03010213') {window.location.href = 'map.html';
}
else{
alert('Please check your passcode and try again');
}
}
</script>
Related
I have a web-form written in ASp.Net MVC5 which is used to gather some details from the user. However, before I get them to submit the form, I want them to have the option to look at another web-page (in a new window or tab) which gives them more information if they need it prior to submitting the page. To that end, on the web-form, I have a form with the following buttons:
<form action="/Application/MyAction" method="post" id="myForm">
// various fields ...
<button onclick="getMoreInfo()">More Information</button>
<button type="button">Submit Form</button>
</form>
Then, at the bottom of the page I have the following javascript defined:
<script>
function getMoreInfo()
{
var urlToUse = 'http://some-other-page.html';
window.open(urlToUse);
return false; // trying to stop the form submission from occurring
}
</script>
My problem is that when this "More Information" button is clicked, it has the effect of submitting the form [which I don't want to do yet] - since there is a separate submit button for doing that task. Is there a way to use a button to jump to another page without actually submitting the current form?
thanks heaps,
David.
I found that answer #3 at this question helped me:
How do I cancel form submission in submit button onclick event?
My solution was to change the code thus:
I changed the button code to look like this:
<form action="/Application/MyAction" method="post" id="myForm">
// various fields ...
<button id="moreInformationButton" >More Information</button>
<button type="button">Submit Form</button>
</form>
And then I changed the javascript to look like this:
$("#moreInformationButton").click(function (event) {
event.preventDefault(); // This stops the submit form being triggered
var urlToUse = 'http://some-other-page.html';
window.open(urlToUse); // open the help page
});
This allowed me to open up another window or tab with more information without actually submitting the form.
I want to ask about "multiple form actions with one submit button"
The form actions will be 2 places, Zoho CRM and Google form.
I have tested but the lead didn't save and can't go to thank you page.
I'm not sure if change to this script is correct or not:
function SubmitForm()
{
if(document.forms['leadform'].onsubmit())
{
showResultDiv();
document.forms['leadform'].action='https://crm.zoho.com/crm/WebToLeadForm';
document.forms['leadform'].submit();
document.forms['leadform'].action='https://docs.google.com/forms/d/e/1FAIpQLScQzsovLE8zusA88V7VTKQ5ACVYkbg0PWQmTsPd1NKGum8Tsw/formResponse';
document.forms['leadform'].submit();
}
return true;
}
The type of button is submit:
<input class="button" type="submit" value=">> Submit" onclick="javascript: return SubmitForm()">
I think the SubmitForm() will never be executed. the form has been submitted,and go to another page.
EDIT:
I guess that you are a freshman for web development. this page can help you know about <form> and <input type='submit'>
I am new here, so maybe you need to give me some hints about how everything works in this community. I was already reading a lot here on Stackoverflow but finally signed up.
I am designing a small website for a museum near me which is a non-profit organization. They have a huge collection of ammunition and the information is currently available on paper.
I want a website where I can enter a number and the appropiate information is shown. Everything works so far in my test site. (since no internet available there, it should run locally on a android tablet later)
The only problem I have is that the form submit works with the button, but not with the enter key or "open" key on the androids numberpad.
I am also quite new to javascript-coding since I come from electronics and c-programming on microprocessors, so I may have made mistake.
i currently have the iframe in the main page, but i originally wanted it to open up in a modal. It did not work properly, so maybe I may try that later again.
Live demo here: museum.smallfilms.ch/drei
The code for the form is the following:
<!-- Jumbotron Header -->
<header class="jumbotron hero-spacer">
<h1>Katalog:</h1>
<p>Mit der Munitionsnummer können hier weitere Informationen zur jeweiligen Patrone angezeigt werden.</p>
<p>
<form onsubmit="searchpage()">
<input type="number" pattern="\d*"/min="1" max="9999" id="num" >
<button type="button" class="btn btn-danger" onclick="searchpage()" id="search">Suchen</button>
</form>
The Javascript code is the following:
function searchpage() {
var num = document.getElementById('num');
var targetFrame = document.getElementById('targetFrame');
if (num.value) {
var page = 'pages/' + (+num.value) + '.html';
targetFrame.setAttribute('src', page);
}
}
If you need more code I can deliver this. Just let me know that you need.
The site is now designed to show something for the numbers 1 and 2.
The whole site uses bootstrap and the sites displayed in the iframe use strapdown.js for easier editing. (We need to digitalize about 900 datasets in the end)
I think it is only a small mistake somewhere but after hours of coding and searching the internet i still did not get the source of the error.
Thanks in advance for any help and hint.
Dabbax
Edit: if it helps, i packed the whole page into a zip... museum.smallfilms.ch/drei/drei.zip
I think that the error comes from the line where you are calling the function searchPage(). I would recommend you to try the line below :
<input type="sumbit" class="btn btn-danger" onclick="searchpage()" id="search" value="Suchen">
In this case, when you press enter, the form will be submitted and call the searchPage function.
On your code for the form, try:
<button type="submit" class="btn btn-danger" onclick="searchpage()" id="search"> Suchen </button>
edit: Shaam's answer can be correct but if you say input then you just trying to make it a look like button with bootstrap, a more proper approach would be input type="button" but in your case you should say that this is a button that submit the form.
That's why you should use button and not input here.
This could be your html:
<form id="searchForm" action="some_url">
<input type="number" pattern="\d*"/min="1" max="9999" id="num" >
<input type="button" value="Suchen" class="btn btn-danger entr" onclick="searchpage()" id="search">
</form>
Now add an event listener to the class entr and submit the form if the key is Enter. So the event listener in jquery like
$('.entr').keypress(function(event) {
if (event.which == 13) { // this is the enter key code
document.getElementById('searchForm').submit();
}
});
Ok so I have two submit buttons, a standard submit button, as well as a , etc, etc button.
Anyway, the second button is a delete button, but both of them go to the same function, because of onSubmit.
What I want to do is set up an if statement to see if the delete button was pressed.
Here's the code to generate the delete button (the value is generated by PHP):
<button type="submit" name="del" id="deletebutton" value="'.$org.'">Delete</button>
Here's my idea so far:
if (document.getElementById('deletebutton').value != '') {
BLAHBLAHBLAH CODE HERE for delete button click;
}
else {
BLAHBLAHBLAH Code for non-delete button click;
}
but my IDE (NetBeans) seems to show an error when I try to do this. How would the correct way to do this be?
EDIT: DOH typo!
if (document.getElementById('deletebutton').value != '' {
You forgot the closing parenthasis, that is why you are getting an error:
if (document.getElementById('deletebutton').value != '') {
One thing you could consider, have both buttons have an onclick action which sets a hidden value in the form. You can then check that in your onsubmit function. Plenty of ways to do this.
you could create and assign a event handler to each of the form buttons and pass the reference to the function to determine which was fired, like this for example.
<script>
function whichButton(o){
if(o.id == "deletebutton"){
alert("delete was pressed");
//document.yourFormName.submit();
}else{
// document.yourFormName.submit();
alert("delete wasn\'t pressed");
}
return false
}
</script>
set up the button
<form name="yourFormName" action="" onsubmit="return false">
<button type="submit" name="del" id="notdeletebutton" value="'.$org.'" onclick="return whichButton(this)">Not Delete</button>
<button type="submit" name="del" id="deletebutton" value="'.$org.'" onclick="return whichButton(this)">Delete</button>
</form>
Suppose a form has multiple submit buttons:
...
<button type="submit" value="deletefoo">Delete Foo</button>
<button type="submit" value="deletebar">Delete Bar</button>
<button type="submit" value="Edit">Edit</button>
...
I am intercepting the clicks for only the 2 delete buttons and disabling the form submit to trigger a custom modal dialog which has OK and CANCEL buttons on it to confirm user choice. If user presses OK, I want to submit the form. If cancel, then dialog dismissed and nothing happens.
I have the first part wired up to trigger the dialog but I am at a loss on how to get the OK button in the dialog to trigger the form submit contingent on which original submit button was pressed (e.g. if Delete button pressed, I want to confirm with user they want to delete, then if so, submit the form as normal.
I've searched around and look at jQuery docs but haven't found the answer yet so I must be missing something really straightforward.
Update: I don't want to use JS confirm function. In my original question above I'm looking to use a custom modal dialog for various reasons.
Check out the JS confirm function and put it as an onclick event.
You have a nice example here.
Why not have them be regular buttons and then onclick set a variable to determine the action type and then when the form submits include this hidden variable and check that to find what you're supposed to do
First, you'd have to intercept both (all) the buttons, you could do this easily by fetching any of the submit buttons within a specific form, then you can ask your question and given you still have the current event handler, you can figure out what button was pressed and do the callback you'd like. For example:
<form id="myform">
<button type="submit" value="delete">Delete</button>
<button type="submit" value="Edit">Edit</button>
</form>
--
$(function() {
$("form#myform button[type='submit']").click(function(ev) {
ev.preventDefault();
if (confirm("you sure")) {
var action = $(ev.currentTarget).val();
console.log(action);
}
});
});
JSLint is here: http://jsfiddle.net/r48Cb/
Basically, console.log(action) will output either "delete" or "Edit" based on the original click. How you handle that value is up to you. A switch statement, a simple if block could work, but it's up to you, I don't know the scope of your app.
The window.confirm function returns a true if the user selects okay and a false if the user cancels. Using this logic you could do something like this:
<button id="delete" type="submit" value="delete">Delete</button>
<button type="submit" value="Edit">Edit</button>
var question;
$("#delete").click(function(){question=window.confirm("Are you sure?");)
if (question){
//Submit the form here
}
else{
alert("Not deleted!");
}
I think you are making it too complex, you can do something as simple as:
<form >
<input name="foo" value="foo">
<button name="sub0" value="sub0" onclick="
return window.confirm('sure?');
">submit 0</button>
<button name="sub1" value="sub1" onclick="
return window.confirm('sure?');
">submit 1</button>
</form>
If the user clicks OK on the confirm dialog, the form submits from whichever button was pressed. If not, it doesn't.
My 2c:
... (edited: removed the value parameter. buttons don't need that)
<button onclick='deleteFoo(); ' >Delete Foo</button>
<button onclick='deleteBar(); ' >Delete Bar</button>
<button onclick='allowEdit(); ' >Edit</button>
...
function deleteFoo() {
do-your-modal-whichever-way-you-want;
if confirmed,
$('#form-id').attr('action','your-action-for-delete-foo');
$('#form-id').submit();
else-just-return
}
function deleteBar() {
do-your-modal-whichever-way-you-want;
if confirmed,
$('#form-id').attr('action','your-action-for-delete-bar');
$('#form-id').submit();
else-just-return
}
function allowEdit() {
whatever
}