how to check if form submitted in laravel? - laravel

I am trying to check if the form submit button is clicked so I tried:
if($request->input('submit')){
//do something
}
and tried:
$clicked=$request->input('submit');
if(isset($clicked)){
//do something
}
and also tried:
if($request->input('submit')!=null){
//do something
}
but when I do click the submit button in the form the execution of if-inside never happens so what is the right way to check if submit button clicked in laravel?

Check if the value of submit button is set. This might be the easiest also, I always use it.
HTML:
<button type="submit" name="find" value="Find">Find</button>
LARAVEL:
if (isset($request->find))
{
//code
}

this can be solved two ways:
first way if your form contains just one button and your form is using POST then like Adnan Mumtaz said we can use :
if($request->method() == 'POST'){
//ur code here
}
but if the form contains multiple buttons and we want check if button than have name btn1 clicked then we can use:
if($request->has('btn1')){
//rest of the code here
}

here is what I am using :
if($request->has('submit')){
//do something
}

If you are submitting the using POST method then you can check whether it is submitted via POST
if($request->method() == 'POST'){
//ur code here
}

You can use get() methid of request
if($request->get('submit')){
//do something
}
Make sure in view you have given button name submit

For example if your submit button html is like below
<input type="submit" name="submit_btn" value="add">
if($request->submit_btn == 'submit')
{
//code
}
Here it compare that if the submit_btn value is submit it means if submit ckicked.
Hope this can help.

Related

how to clear validation errors for Angular Material mat-error

I'm trying to reset a form after I've added a value.
Form Code Snippet
<form [formGroup]="addAttributeForm" fxLayout="column">
<mat-form-field>
<input matInput formControlName="title" placeholder="Title" required>
<mat-error>This field is required</mat-error>
</mat-form-field>
</form>
In the component
onSubmit(form: FormGroup) {
// do work
form.reset();
}
What I'm observing:
The form values are set to empty.
But the validation messages are still displayed from mat-error.
I've tried form.markAsPristine(), form.markAsUntouched() and combining all three.
How can I reset the form so the mat-error is not displayed?
The form group has no "knowledge" about whether the actual HTML form has been submitted or not. It only keeps track of the form values/validity/enabled state. So resetting the form group does reset the values but not any state regarding whether the form has been submitted.
To do this, you need to get a hold of the FormGroupDirective and call resetForm() on it.
Form Code Snippet
<form [formGroup]="addAttributeForm" fxLayout="column">
<!-- ... -->
</form>
In the component
#ViewChild(FormGroupDirective) formDirective: FormGroupDirective;
onSubmit(form: FormGroup) {
// do work
this.formDirective.resetForm();
}
This solution works with me.
You need to do next:
formReset(form: FormGroup) {
form.reset();
Object.keys(form.controls).forEach(key => {
form.get(key).setErrors(null) ;
});
}
This reset the form and clear all error.
The only way I've been able to successfully do this is by setting a flag to hide the form when you want to reset it and then using a timeout to set that flag back to true. As far as I know, there is no built-in way to do this yet.
showForm = true;
reset(): void {
this.showForm = false;
setTimeout(() => this.showForm = true);
}
And then in the HTML on the form element use *ngIf="showForm".

how to check If checkbox is checked or not laravel 5.4

I want to show a button if checkbox gets checked and if not checked button will not appear in laravel 5.4. How can I do this.
Suppose you html
<input type="checkbox" name="checkbox" id="checkbox">
In front-end you can check using jQuery
if ($('#checkbox').is(':checked')){
//
}
In back-end you can check using:
if (isset($request->checkbox) {
//
}
you can do this by using jquery
$('#checkbox').change(function(){
if($('#checkbox').is(':checked')){
console.log("asdad");
$('#button').css('display','inline');
}else{
$('#button').css('display','none');
}
})
Or you can verify if checkbox is checked in BackEnd
if($request['checkBoxName'])
return "Checkbox is checked";
else
return "Checkbox is not checked";

If condition for 2 forms(2 submit buttons) in one controller in CI

I have a controller auth and there are 2 methods login and register.
And it is working nicely.
However, I need to have another function called login_and_register which will have an if condition that will determine which part will be executed. If the login part (basically code from login method) or register part (code from register method).
The if condition should be checking which submit button was clicked.
But here is the problem.
I need the form_open for both forms (login and register) stay like this:
<?php echo form_open('auth/login_and_register'); ?>
And this cannot be changed to e.g. auth/login_and_register_log or auth/login_and_register_reg or auth/login_and_register/log or auth/login_and_register/reg etc.
The login and register forms are in the same view and after submit is pressed this function login_and_register in auth controller is exectuted.
I was thinking that I can make the if condition base on the name of submit value, because for login form it is called submitlog and for registration form it is called submitreg.
So, maybe something like this:
if( isset($this->form_validation->set_value('submitlog')) ) {
//code for login part
}
elseif ( isset($this->form_validation->set_value('submitreg')) ) {
//code for registration part
}
else {
//code for redirect to homepage
}
But it is not working. Any idea why?
Querying the submit button names is a good idea. Here is a piece of code that works:
if ($this->input->post("submitlog") !== false) {
//code for login part
} elseif ($this->input->post("submitreg") !== false) {
//code for registration part
} else {
//code for redirect
}
instead of that, you can keep a hidden field in both the forms with the value of corresponding form name. In the controller you can check this field value and identify which form is submitted. Then you can redirect into the required function..

jQuery.validate stops my form from being submitted

jQuery.validate stops my form from being submitted. I would like it to just show the user what is wrong but allow them to submit anyway.
I am using the jquery.validate.unobtrusive library that comes with ASP MVC.
I use jquery.tmpl to dynamically create the form and then I use jquery.datalink to link the input fields to a json object on the page. So my document ready call looks something like this.
jQuery(function ($) {
// this allows be to rebind validation after the dynamic form has been created
$("form").removeData("validator");
$("form").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse($("form"));
// submit the answers
$("form").submit(function(e) {
$("input[name=jsonResponse]").val(JSON.stringify(answerArray));
return true;
});
}
I note that there is an option
$("form").validate({ onsubmit: false });
but that seems to kill all validation.
So just to recap when my form is rendered I want to show all errors immediately but I don't want to prevent the submit from working.
So after some research (reading the source code) I found I needed to do 2 things
add the class cancel to my submit button
<input id="submitButton" type="submit" class="cancel" value="OK" />
This stops the validation running on submit.
To validate the form on load I just had to add this to my document ready function
$("form").valid();
Hope this helps someone else

How can I stop a form from processing/submitting that is using jquery AJAX submission?

I have a form with two buttons, a submit button and a cancel/close button. When the user clicks submit, the entered data is validated using http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/. If everything validates, the form is submitted with jQuery/AJAX. That all works fine and dandy. I run into problems with the cancel button though. I want the cancel button to require confirmation. If the user chooses to proceed, they are taken to a page of my choosing. If they decide they don't want to cancel, then they are simply left on the page. It's the last part that isn't working.
My form code looks like this:
<form name="createPage" id="createPage" method="post" action="pager.php" class="ajax updateForm">
<input name="whatever" type="text" />
<button type="submit" id="submitQuickSave" class="submitSave"><span>save</span></button>
<button type="submit" id="submitCancel" class="submitClose" onclick='confirm_close()'><span>close</span></button>
</form>
My current cancel script looks like the following. If the user does indeed want to cancel, I unbind the form submit so that validation isn't executed. The form then proceeds to submit and includes cancel as a parameter in the action attribute. I handle the cancellation server side and direct the user to a new page.
function confirm_close()
{
var r=confirm("All changes since your last save operation will be discarded.");
if (r==true)
{
$(".ajax").unbind("submit");
}
else
{
}
}
I cannot figure out what to put in the 'else' argument. What happens is that if the users cancels the cancellation (i.e., return false), then the form still tries to submit. I cannot make it stop. I've tried several things from this site and others without success:
event.stopImmediatePropogation
.abort()
Any ideas? Basically, how can I get the cancel/close button work properly?
Consider separating your JavaScript from your HTML. With this in mind, you could write the handler for your the click event you're trying to intercept like this:
$("button#cancel").click(function($event) {
var r = confirm("All changes since your last save operation will be discarded.");
if (r) {
$(".ajax").unbind("submit");
}
else {
$event.preventDefault();
}
});
You would have to tweak your HTML and add an id attribute to the cancel button:
<button id="cancel" type="submit" value="cancel">Cancel</button>
Example here: http://jsfiddle.net/wvFDy/
Hope that helps!
I believe you just
return false;
Let me know if this works.

Resources