Button Onclick does not work inside form - forms

Good Day
I am using an ASP.NET webform where I have wrapped the following button inside the form tags:
<form runat="server">
<button class="btn btn-primary" onclick="window.location='http://google.co.za';">Login</button>
</form>
ISSUE: The problem is that when clicking on the button, it just directs to the current page...does not redirect. However, when I remove the form tags, the button works. If I leave the form tags and I use an anchor tag to relocate, it works as well...It is just the button that is giving me issues.
Why is that?
I am using Twitter bootstrap as well(extra info..)
Thank you

Add type="button" attribute to your button tag. Without it button works as submit.

Related

Submit button with external link

I have two submit buttons inside a form:
<input class="submitButton button cancel" name="cancel"
value="Cancel" title="" type="submit">
<input class="submitButton button" name="Registrieren"
value="Register" title="Register" type="submit">
I would like the first button (cancel) to have a link to an external page. The link is known in the backend (FormController).
I had a look at the SubmitLink class. However it looks like that doesn't fit to my use case. I don't want to process the click on the "cancel" button in the backend, just redirect to the external page by integrating this dynamic link into the HTML file.
I agree with #jurgemaister that you can use <a> for the cancel button and style it with CSS to look like a button.
Otherwise you can add 'click' JavaScript listener to the cancel button to do something like location.href='/url/to/other/page'.

php two actions in one form

I have question about the other options... i have form like this...
<form action="edit.php" method="post">
.
.
.
.
<button class="btn btn-success" name="submit_mult" type="submit">Edit</button>
</form>
I have another button called (delete) inside of above form in same page that will take action to delete.php page.
But the delete button is need to action to delete.php, but is already used up with action="edit.php" see above codes
How can i solve that to make both works!
AM
You can use the formaction attribute on the button:
<button type="submit" formaction="delete.php">Delete</button>
This overrides the action attribute of the form when you click on that button.

How to make fancybox close after hitting "submit button"

My form comes from Hubspot and is embedded into a fancybox.
I would like the box to close as soon as the person hits "submit" but instead it redirects and stays open.
Secondly, I tried entering the code to clear the content box after submission but the old data stays in the fields after reopening.
How do I make the box close after they "submit"?
How do I code so that fields clear after each submission?
http://online.saintleo.edu/FancyBox2/FancyBox.html
In advance, thank you. You guys/gals are great.
After inspecting your Fancybox I found this html:
<form accept-charset="UTF-8" enctype="multipart/form-data" id="hsForm_bc762bf8-087a41ec-8f55-263df96f988a" class="hs-custom-form stacked hs-form" action="https://forms.hubspot.com/uploads/form/v2/206683/bc762bf8-087a-41ec-8f55-263df96f988a" method="POST" novalidate="novalidate">
It works as expected when I add target="_top" to it.
Your form.html may have a form tag. Add target="_top" to it.
See this link Link here
Add target="_top" in form tag
<form target="_top">

Avoid modal dismiss on enter keypress

I have set up a bootstrap modal with a form inside it, I just noticed that when I press the Enter key, the modal gets dismissed.
Is there a way not to dismiss it when pressing Enter?
I tried activating the modal with keyboard:false, but that only prevents dismissal with the ESC key.
I just had this problem too.
My problem was that i had a close button in my modal
<button class="close" data-dismiss="modal">×</button>
Pressing enter in the input field caused this button to be fired. I changed it to an anchor instead and it works as expected now (enter submits the form and does not close the modal).
<a class="close" data-dismiss="modal">×</a>
Without seeing your source, I can't confirm that your cause is the same though.
Just add the type="button" attribute to the button element, some browsers interpret the type as submit by default.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Attributes
This applies for all the buttons you have in the modal.
<button type="button" class="close" data-dismiss="modal">×</button>
I had this problem even after removing ALL buttons from my Bootstrap Modal, so none of the solutions here helped me.
I found that a form with a single text field would cause the browser to do a form submit (and result in dismiss), if you hit Enter while keyboard focus is on the text field. This seems to be more of a browser/form issue than anything with Bootstrap.
My solution was to set the form's onsubmit attribute to onsubmit="return false"
This may be a problem if you are actually using the submit event, but I'm using JS frameworks that generate AJAX requests rather than doing a browser submit, so I prefer disabling submit entirely. (It also means I don't have to manually tweak every form element that might trigger a submit).
More info here: Bootstrap modal dialogs with a single text input field always dismiss on Enter key
I had same problem, and i solved it with
<form onsubmit="return false;">
but there is one more solution, you can add dummy invisible input, so your form would look like this:
<form role="form" method="post" action="submitform.php">
<input type="text" id="name" name="name" >
<input type="text" style="display: none;">
</form>
You can put the login button before the cancel button and this would solve the issue you are having as well.
<div class="modal-footer">
<button type="submit" class="btn primary">Login</button>
<button type="submit" class="btn" data-dismiss="modal">Cancel</button>
</div>
I had a similar experience just now and the way I solved it was instead of using a tag, I changed the tag to an tag with type="button". This seemed to solve the problem of pressing the "enter" key and dismissing the bootstrap modal.
I had this problem too and I solved it this way. I added onsubmit to form. I also wanted to be able to use enter key as a saving key so I added save_stuff() javascript to onsubmit. return false; is used to prevent the form submit.
<form onsubmit="save_stuff(); return false;">
...
</form>
<script>
function save_stuff(){
//Saving stuff
}
</script>

How do I keep track of when what is the text entered in the form when the user navigates away from the page?

I have a piece of code like this.
<form method="get" action="{$self}" name="addcommentform">
<textarea title="{$enterComment}" name="comment" class="commentarea" </textarea>
<input class="Button" type="submit" value="{$postComment}" />
</form>
How do I keep track of when what is the text entered in the form's textarea when the user navigates away from the page? I want to prompt the user with a warning message so he/she doesn't lose the text.
Thanks.
You could use the javascript/jquery blur event, and if the user hasn't clicked the desired button have it display the form values. This might help JQuery Blur Validation Problem
Stu
Take a look at this Javascript code snippet:
http://www.boutell.com/newfaq/creating/disableclose.html
This takes advantage of the window's onbeforeunload event which fires when the user is about to leave the page.