I'm trying to include static HTML form inside my Angular 2 (beta2) app but it doesn't do anything when I hit the submit button.
Here's the HTML I use:
<form action="upload_path" method="POST">
<input type="text" name="text" />
<button type="submit">Send</button>
</form>
How can I enable my form to work with Angular2?
With
<form ngNoForm ...>
you can prevent Angular from handling the form.
If you want to use the action attribute of HTML form, you need to disable the behavior of the NgForm directive that catches the submit event and prevents it from being propagated. See this line: https://github.com/angular/angular/blob/master/modules/angular2/src/common/forms/directives/ng_form.ts#L81.
To do that simply add the ngNoForm attribute to your form:
<form ngNoForm action="https://www.google.com" target="_blank" method="POST">
<input name="q" value="test">
<button type="submit">Search</button>
</form>
In this case, a new window will be opened for your form submission.
That said, Angular2 tackles SPAs (Single Page Applications) and in most use cases, you need to stay in the same page when submitting a form. For such use cases, you can leverage the submit event with a submit button:
<form [ngFormModel]="companyForm" (submit)="onSubmit()">
Name: <input [ngFormControl]="companyForm.controls.name"
[(ngModel)]="company.name"/>
<button type="submit">Submit</button>
</form>
Hope it helps you,
Thierry
Angular 2 adds an event handler to forms submit event and blocks the forms form being sent. This is done for the sake of normal AJAX use case where you don't actually want to submit the form (and thus reload the page), but instead catch it on JavaScript and send an AJAX request (or handle the data other ways).
You can bypass this by using your custom event handler which will be called first and in which you send the form already before it goes to the Angular's handler.
To do this, you need to add onsubmit="this.submit()" on your form element like this:
<form action="upload_path" method="POST" onsubmit="this.submit()">
Related
Not sure how I did this last time or else I wouldnt asking here but here is what I'm trying to do.
I have the usual basic form with a javascript function that will submit the form. Question is that after the form is submitted, I have an if statement in PHP that echos a that the form has been submitted. Any help would be greatly appreciated.
//PHP
if($_POST['submitDelete']){
echo "welcome, You form has been submitted";
}
//HTML
<form id="form_id" action="" method="POST">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br><br>
<input type="hidden" name="submitDelete" TYPE="submit">
</form>
<button type="button" onclick="myFunction()">Submit</button>
//JAVASCRIPT
<script>
function myFunction() {
document.getElementById("form_id").submit();
}
</script>
I can't seem to trigger the if statement in PHP. I also tried using the form name in the if statement and that didnt work either.
A form element must be told where to submit its data to when the submit event takes place. This is accomplished by setting the action attribute value for the form. Leaving that attribute empty does not implicitly set the form to post back to the current page. So, if you want to have a single page form/form processor, you need the action to be set to the current page file name:
<form action="currentPageFileName.php" method="post">
Next, there's no reason a single page can't have multiple forms on it. In that case you would need multiple submit buttons, each tied to a specific form. For this reason, you can't just drop a submit button anywhere on the page that you like unless you add the form attribute to the button to tie it back to the form it is supposed to trigger the submit for. Also, if you simply place the submit button within the form element it "belongs" to, you don't have to worry about this.
Also, you have some invalid HTML with:
<input type="hidden" name="submitDelete" TYPE="submit">
An element may not have the same attribute repeated within it (the case that you type the attribute in makes no difference since HTML is not case-sensitive). So, that code would wind up simply creating a submit button.
Lastly, if all you want to do with your submit button is cause its related form to be submitted, there is no need for JavaScript at all. That is what submit buttons do by default.
So, in the end, you can get rid of the JavaScript in your code completely and change your HTML to this:
<form id="form_id" action="currentFileName.php" method="POST">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br><br>
<input type="hidden" name="submitDelete" value="true">
</form>
<button type="submit" form="form_id">Submit</button>
I have a form that includes some Knockout code, but the form is being submitted too early. I have the following
<form data-bind="submit:addItem">
Add illness: <input type="text" data-bind='value:itemToAdd, valueUpdate: "afterkeydown"' />
<button type="submit" data-bind="enable: itemToAdd().length > 0">Add</button>
</form>
The button is interfering with my Razor form, I assume that because it is of type="submit", so when I click this button unfortunately the form is being submitted, when all I want to use this button for is to call a Knockout function.
So I guess one way to solve the problem is to not use the code above, but I don't know what the alternative would be. Hopefully I don't need to put type="submit" right there, because I need to save that functionality for when I submit my final form much much later
Try this:
<form data-bind="submit:addItem.bind($data)">
I use the .bind in all my click events as it stops the click events being fired on applying of bindings and also allows you to pass extra parameters.
I've written a form with Angular.js that requires a field to be filled out before it is submitted. The validation works correctly (the field shows a validation error when I submit the form) but the form still seems to perform its ng-click action.
Are angular forms supposed to submit despite validation errors? What's the best way to prevent it from submitting if there are validation errors?
Here's the form in question:
<form role="form">
<div class="form-group">
<label>Book Id</label><br>
<input ng-model="bookToSend.bookId" class="form-control" maxlength="40" required type="text">
</div>
<button type="submit" ng-click="sendBookUpdate(BookToSend)">Send Book Update</button>
</form>
Angular doesn't prevent forms from submitting when there are validation errors.
Actually with the snippet you pasted, the errors are shown just because by default error validation is provided with html5.
You should check the docs: https://docs.angularjs.org/guide/forms
Basically you have to name your form:
<form name="myForm" role="form">
and then you can prevent your form from submitting inside your controller with:
$scope.sendBookUpdate(BookToSend, form) {
if (form.$invalid) {
return; // and add any error to the view if you want
}
...
}
another option is to prevent submitting from the view:
<form name="myForm" role="form" ng-submit="myForm.$valid && sendBookUpdate(BookToSend)">
You could disable the button until the form has valid data
<button type="submit" data-ng-disabled="form.$invalid">Send Book Update</button>
EDIT:
When I wrote the answer below, I was under the assumption that the ngClick was completely separate from any form submission handlers that angular uses. I was wrong, however, as shown in the comments below. I'll keep this answer up, despite its inaccuracy, to inform those that come here with the same misunderstanding as I had, since, to me at least, it's kind of counter-intuitive to have an ng-click double as a submit handler.
ng-click is separate from the form's submit handler, and will run every time you click the button regardless of whether or not it passes the validation checks.
The solution to your problem would be to take sendBookUpdate(BookToSend), and place it in an ng-submit attribute on the form itself. See the code below:
<form role="form" ng-submit="sendBookUpdate(BookToSend)">
<div class="form-group">
<label>Book Id</label><br>
<input ng-model="bookToSend.bookId" class="form-control" maxlength="40" required type="text">
</div>
<button type="submit">Send Book Update</button>
</form>
Let me know if that helps.
Edit:
Here's some more info regarding ngSubmit:
https://docs.angularjs.org/api/ng/directive/ngSubmit
I am building a PhoneGap application using JavaScript, HTML and jQuery Mobile.
All the HTML is in the same file, separated into <div data-role="page"> as pages.
Several pages have a form including one or more text/selection input and a submit button.
The submit is not a traditional form submit button but a button which using onClick runs a JavaScript function which can do many things.
I want the form to have this features:
When pressing the button and after running the function, clear the form.
In some cases the function should change the page.
The enter button on one of the inputs should submit the form (Activate the function).
Should I use the form HTML tag? If so what should I use for action? How to clear the form?
etc.
If you are trying to bind onClick to an input type="submit" then you're gonna have a bad time.
Unfortunately even if you return false or e.preventDefault when clicking that button, the form still sends the submit trigger so once your onClick code is finished then it will submit.
Example:
<form action="woot.php" method="POST">
<input type="submit" value="submit" onClick="alert('You clicked me! How could you?! It's cool the form will still go to woot.php. return FALSE wont help you either.'); return FALSE;">
</form>
What you probably want to do:
<form action="woot.php" method="POST">
<input type="submit" value="Submit" onSubmit="alert('You aint goin nowhere!'); return FALSE;">
</form>
What you should do:
<form action="woot.php" method="POST">
<input type="button" value="Button" onClick="alert('Away with you!'); window.location = 'http://www.google.com/';">
<input type="button" value="Button" onClick="someCoolFunction();">
</form>
I wouldn't use type="button", especially if you want to have the best chance of the form submitting when the user presses enter.
Use your regular form <input type="submit"> and then your JavaScript:
$('form').submit(function(e) {
// all your form handling here;
if (your_form_was_validated_and_handled) {
$('input[type!="submit"]').val('');
}
e.preventDefault();
});
Generic fiddle: http://jsfiddle.net/
You can still use the form tag, as it's useful for markup.
Just make sure that your buttons have attribute
type="button"
otherwise the button will submit the form by default.
To reset the form:
function resetForm() {
$('#form').each(function(){
this.reset();
});
}
I'm relatively new to AngularJS and I am trying to sumbit a regular form. I have basic form that looks like this:
<form method="post" enctype="multipart/form-data">
input type="text" class="title span5" name="post_title" placeholder="A catchy title here..." value="" />
<input type="file" name="post_image" />
<input type="submit" class="btn btn-success" value="Create Post" />
</form>
But I noticed that AngularJS adds its own values to the form.
<form method="post" enctpye="multipart/form-data" class="ng-pristine ng-valid">
And I am ununable to submit the form. How can I disable the automatic validation that Angular JS is adding to the app?
Quoted from the documentation:
For this reason, Angular prevents the default action (form submission to the server) unless the element has an action attribute specified.
IMHO, you should read the doc to gain some general understanding of single page application, and the reason why angular's preventing the default behavior.
UPDATE : This does NOT work ... well at least not in a way you'd like it to. Adding ng-non-bindable to the form or any input breaks ALL binding. So, your ng-model in the inputs won't work anymore.
ng-non-bindable is possibly your best choice. It will prevent AngularJS from doing ANY validation. So, you'll be responsible for showing invalid and checking validity.
https://stackoverflow.com/a/19387233/75644