Angularjs: how to Call method in Controller while submitting the Form - php

Actually In my form page of Angularjs, have two submit buttons i.e In one field set i have one button for update and another button at the outside of all field sets for submission of whole page.
Code
<form ....... data-ng-submit="Register()">
<fieldset>
............
.............
<div data-ng-submit="update()">
<button type="submit" class="btn btn-success">Update Vehicle</button>
</div>
</fieldset>
<fieldset> .........</fieldset>
</form>
After submission of form to the ctrl
.ctrl(function(){
**we have two methods in it.......**
$scope.update=function(){
}
$scope.register=function(){
}
}
While updating the fieldset it goes to register method and its executes the logic in it.
how to format the data-ng-submit="update()", such that it will call the update() method in Controller

Related

Prevent Multiple Submitting in one button laravel

Before i make this question i use javascript method to prevent multiple submit on my blade template. But i know it's client side that still possible to get attack by.
This is my javascript code
<script>
function submitForm(btn) {
// disable the button
btn.disabled = true;
// submit the form
btn.form.submit();
}
</script>
<input id="submitButton" type="button" value="Submit" onclick="submitForm(this);" />
my question is, is there another way to prevent without client side in laravel?
The most straightforward way to guarantee the uniqueness of a form submission (In the sense of stopping someone mashing submit twice) is to generate a random token and storing it in a session AND a hidden field.
If it doesn't match, reject the form, if it does match, accept the form and nuke the session key.
OR
Force Laravel to regenerate a new session token after each time a token is verified correctly. (Easy Way Out)
To achieve this, create a new function tokensMatch() in app/Http/Middleware/VerfiyCsrfToken.php (which will overwrite the inherited one). Something like this:
protected function tokensMatch($request)
{
$tokensMatch = parent::tokensMatch($request);
if ($tokensMatch) {
$request->session()->regenerateToken();
}
return $tokensMatch;
}
In case you validate the form and the validation fails, the old data will be passed back to the form. So you need to make sure not to pass back the old token by adding _token to the $dontFlash array in app/Exceptions/Handler.php
protected $dontFlash = ['password', 'password_confirmation', '_token'];
Step 1: write a class name in the form tag Exp: "from-prevent-multiple-submits"
<form class="pt-4 from-prevent-multiple-submits" action="{{ route('messages.store') }}" method="POST">
#csrf
Step 2:
Write a class in button section
<button type="submit" id="submit" class="btn btn-primary from-prevent-multiple-submits">{{ translate('Send') }}</button>
Step 3:
write this script code
<script type="text/javascript">
(function(){
$('.from-prevent-multiple-submits').on('submit', function(){
$('.from-prevent-multiple-submits').attr('disabled','true');
})
})();
</script>
give id to submit button
<input class="main-btn" id="register" type="submit" value="Make Appointment">
give id to form
<form id="appointment_form" method="post" action="{{route('appointment')}}">
in your js add these
$('#appointment_form').on('submit', function () {
$('#register').attr('disabled', 'true');
});
Step 1: give id to form
<form action="{{ route('web.reports.store') }}" method="POST" enctype="multipart/form-data" id="kt_stepper_form">
Step 2: give id or add class to submit button
<button type="submit" class="btn btn-primary submit-btn" data-kt-stepper-action="submit">
<span class="indicator-label">
Submit
</span>
<span class="indicator-progress">
Please wait... <span
class="spinner-border spinner-border-sm align-middle ms-2"></span>
</span>
</button>
Step 3: and then, you can add some jquery script like this
$('#kt_stepper_form').on('submit', function(){
$('.submit-btn').attr('disabled', true);
$('.indicator-label').hide();
$('.indicator-progress').show();
});
with code above, button will be disabled and show indicator progress when user clicked the button

No POST data after dynamically creating a form and submitting it

I am creating a little shop system for myself (learning purpose) using AngularJS and PHP, and I am stuck with the following problem:
I created a dynamic shopping cart and filled it on ng-click with some information of the clicked object in the list. I saved to a scope variable called scope.cartwhich is then shown by ng-repeat within the shopping cart table. I then also automatically created input fields which are hidden to pass them to the $_POST variable after submit. Because my POST is empty after submitting, I tried to use the ng-submit directive to create a json file out of the scope.cart array at the time the formula is sent, so I can then call it via PHP. But this doesn*t work either. Now I am stuck and have no clue what the problem could be.
However I think that the input field is empty even though the browser adds new fields dynamically in the source code, and when I hit submit there is the state of the empty cart.
Whats the best way to solve my problem and send the AngJS data to the server?
navApp.controller('MainCtrl', ['$scope', '$http', function (scope, http){
scope.submit = function() {
var time = Math.floor(Date.now() / 1000);
http.post('../dampfen/cfg/orders/cart_' + time + '.json', scope.cart).then(function(data) {
alert("Order successfully transferred!");
});
}
}]);
<div class="panel-body">
<table class="table table-hover">
<thead><tr><th colspan="4">Bestellung</th></thead><tbody>
<form name="orderForm" method="POST" ng-submit="submit()" action="index.php?checkout">
<tr ng-repeat="item in cart">
<input type="hidden" name="order[]" value="{{item.id}}" />
<td>1x</td><td><strong>{{item.name}}</strong></td><td>{{item.preis}} <span class="glyphicon glyphicon-euro"></span></td><td><button class="btn btn-xs btn-danger" ng-click="removeFromCart($index)"><span class="glyphicon glyphicon-remove"></span></button></td></tr>
</tbody>
</table>
</div>
<div class="panel-footer">
<p class="text-right"><strong>{{cart.sum}} <span class="glyphicon glyphicon-euro"></span></strong></p>
<p class="text-left"><button type="submit" class="btn btn-sm btn-success">Checkout</button> <button type="button" class="btn btn-sm btn-danger" ng-click="deleteCart()">Warenkorb löschen</button></p>
</form>
<form> elements cannot be child nodes of <tbody> elements.
You can have an entire table inside a form. You can have an entire form inside a table cell.
Anything else leads to browser error recovery that can dump the form outside the table leaving the inputs behind.
Use basic QA. Write valid HTML.

How to make a button not to submit the form?

<form>
<button type=submit>save</button>
<button onclick="create();return false;">click</button>
<textarea id="some">Testing</textarea>
</form>
<script>
function create(){
window.location.href="some.php";
}
</script>
Though I have added return false in the onclick event, when I click the button it gets submitted. How to make that button to stop from form submission. If I have some other function in the create() function instead of redirecting, return false code will stop from submission. But here am redirecting the page so return false is not working.
I tried putting return false code in create() function too but no luck.
How to stop the form submit ?
<button type=submit>save</button>
Should be:
<input type=submit value="save" onclick="create();return false;"/>
And
<button>click</button>
USE..
<input type="button" onclick="create();">
As in create function you have redirected page so actually form is not submited but page is redirected.
If you use the button like
<button type="button">foobar</button>
it won't submit the form as long as you don't bind some js functions on it.
If you don't give this attribute type="button" the form takes it as a normal button and trys to submit it. Same like if you would add the attribute type="submit".

How to put button outside of form

I have a form that come with action like this.
<form action="edit.php" method="post">
.
.
<button class="btn btn-success" name="submit_mult" type="submit">Edit</button>
</form>
I need this to use for first button, and now I want to use same form for second button this time is for delete, but is because this form action using for "edit.php", so I use formaction="delete.php,this will This overrides the action attribute of the form when you click on that button.
But I want the second button to be show outside of form not inside of form, and I tried to figure out how that works..
Wrote like this...
<div id="delete"><button class="btn btn-success" formaction="Multi_Edit/delete.php" name="submit_mult" type="submit">delete</button></div>
Is not working when I put this button in outside of form, but is works fine inside of form.
How can I solve this to make works that button outside of forms with formaction="delete.php" if this works fine with jquery, how to write that in jquery!
try this
put and id to the form
<form action="edit.php" method="post" id="myForm" >
.
.
and an id to the button
<div id="delete"><button class="btn btn-success" formaction="Multi_Edit/delete.php" name="submit_mult" id="myDeleteButton" type="submit">delete</button></div>
then via jquery handle the form submit
$("#myDeleteButton").click(function(){
$("#myForm").attr('action','Multi_Edit/delete.php');
$("#myForm").submit();
});
You can use the form attribute, form="theForm":
<form action="edit.php" method="post" id="theForm">...</form>
<div id="delete"><button form="theForm" class="btn btn-success" formaction="Multi_Edit/delete.php" name="submit_mult" type="submit">delete</button></div>
See here: http://www.sitepoint.com/the-html5-form-attribute/

Change a form in order to use jQuery and avoid the page refresh

I'm trying to change the form tag below in order to use jQuery. Already, clicking the buttons changes the display from rows to columns and vice-versa but I want to avoid the page refresh. I'm really new at jQuery and can't honestly say what my mistakes are when trying to change it myself.
<form id="rowsToColumns" action="index.php?main_page=specials&disp_order=1" method="POST">
<input type="hidden" name="style_changer" value="columns"/>
<button type="submit" class="btn btn-large btn-primary" type="button">Change to Column</button>
</form>
<form id="columnsToRows" action="index.php?main_page=specials&disp_order=1" method="POST">
<input type="hidden" name="style_changer" value="rows"/>
<button type="submit" class="btn btn-large btn-primary" type="button">Change to Rows</button>
</form>
I'm also trying for the buttons to call a different stylesheet upon click. This stylesheet is not needed for the display to change from/to rows/columns as I mentioned above. The actual page is written using php as shown below:
<?php $this_page = zen_href_link($_GET['main_page'], zen_get_all_get_params()); ?>
<div id="style_changer">
<?php if($current_listing_style == 'rows') {?>
<form id="rowsToColumns" action="<?php echo $this_page;?>" method="POST">
<input type="hidden" name="style_changer" value="columns"/>
<button type="submit" class="btn btn-large btn-primary" type="button">Change to Column</button>
</form>
<?php } else { ?>
<form id="columnsToRows" action="<?php echo $this_page;?>" method="POST">
<input type="hidden" name="style_changer" value="rows"/>
<button type="submit" class="btn btn-large btn-primary" type="button">Change to Rows</button>
</form>
<?php } ?>
</div>
If the question is "how to change a form in order to use jQuery and avoid the page refresh", then the jquery form plugin is your friend, as it turns any html form into an ajax-powered one.
Simply follow their instructions and you'll get it working in no time (provided your form already works as is).
You can prevent the Default form Submission by preventing the default action on the submit button..
$('button[type=submit]').submit( function(e){
e.preventDefault(); // Stops the form from submitting
});
Well, for a very vague method you can use $.ajax and take advantage of reading the <form>'s pre-existing attributes to decide on submission method and read the elements' values as submissiong data:
$('form').on('submit',function(e){
var $form = $(this);
// submit the form, but use the AJAX equiv. instead of a full page refresh
$.ajax({
'url' : $form.attr('action'),
'method' : $form.attr('type'),
'data' : $form.serialize(),
'success' : function(response){
// process response (make CSS changes or whatever it is
// a form submission would normally do)
}
});
// prevent the normal submit and reload behavior as AJAX is now
// handling the submission
e.preventDefault();
});
However, for this to work you'll need some variation of a stripped-down PHP response just for the purpose of the AJAX request (avoid resending headers, script tags, etc. and just return the raw data that jQuery can use to make a UI decision).

Categories