How to call the HttpPost ActionResult in MVC5? - html

I have a contact view in my app, and it contains a form, which is defined as below:
<form role="form" style="width:100%; float:left;">
<div class="form-group">
<img src="~/Content/img/zarf.png" class="zarf-pos">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Name and Surname</label>
<input type="text" name="fullName" class="form-control" id="exampleInputEmail1" placeholder="Enter your full name" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">E-Mail</label>
<input type="email" name="emailAddress" class="form-control" id="exampleInputPassword1" placeholder="Enter your e-mail address" required>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Write to us</label><br>
<textarea name="message" class="form-control" rows="5" placeholder="How can we help you?" required></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default reg-position">Send</button>
</div>
</form>
Then, in my controller, I have two ActionResults, one HttpGet, and one HttpPost, defined like this:
[HttpGet]
public ActionResult Contact()
{
return View();
}
[HttpPost]
public ActionResult Contact(string fullName, string emailAddress, string message)
{ // some functionality }
But, when I fill out the form and click the Send button, it redirect to the empty Contact page. The point is that I need to have two Contact methods, while in my Post method I have some code that assumes that the strings are not empty. And if I have just one contact method, I get an error message saying the string is null, and the view is not displayed. So, I decided to have two methods, but don't know how to call the post method when the button is pressed.

Assuming you are using Razor to create your views, define the form like so:
#using( Html.BeginForm() )
{
<div class="form-group">
<img src="~/Content/img/zarf.png" class="zarf-pos">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Name and Surname</label>
<input type="text" name="fullName" class="form-control" id="exampleInputEmail1" placeholder="Enter your full name" required>
</div>
<div class="form-group">
<label for="exampleInputPassword1">E-Mail</label>
<input type="email" name="emailAddress" class="form-control" id="exampleInputPassword1" placeholder="Enter your e-mail address" required>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Write to us</label><br>
<textarea name="message" class="form-control" rows="5" placeholder="How can we help you?" required></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default reg-position">Send</button>
</div>
}
This will ensure the correct <form> element is generated to post back to your action.
P.S. Just some nomenclature housekeeping... You don't call an ActionResult, you call Actions and they return ActionResults. It'll help when you're talking to other developers if you're using the correct names for things.

Related

Iissue with formsubmit.co url

i'm using formsubmit to send my emails my problem is when i submits the form google recaptcha page will open with url like https://formsubmit.co/myemail#gmail.com i wanted to change this url to my website url . is that possible???
below is my form
<form class="app_form_wrapper" action="https://formsubmit.co/myemail#gmail.com" method="POST" >
<div class="col-lg-6 text-left">
<div class="form-group apps-pulldown-20">
<div class="apps_input">
<input type="text" class="form-control" name="name" placeholder="Name"required>
</div>
</div>
<div class="form-group apps-pulldown-20">
<div class="apps_input">
<input type="email" class="form-control" name="email" placeholder="Email"required>
</div>
</div>
<div class="form-group apps-pulldown-20">
<div class="apps_input">
<input type="tel" class="form-control" name="phone" placeholder="Phone Number" minlength="10" maxlength="15"required>
</div>
</div>
</div>
<div class="col-lg-6">
<div class="form-group apps-pulldown-20">
<div class="apps_input">
<input type="text" class="form-control" name="company" placeholder="Company"required>
</div>
</div>
<div class="form-group apps-pulldown-20">
<div class="apps_input">
<textarea rows="10" class="form-control" name="message" placeholder="Message"required></textarea>
</div>
</div>
</div>
<input type="hidden" name="_url" value="http://localhost/app/contact">
<input type="hidden" name="_next" value="http://localhost/app/mailSent.html">
<input type="text" name="_honey" style="display:none">
<input type="hidden" name="_template" value="table">
<div class="col-lg-12">
<p>
This site is protected by reCAPTCHA and the Google
Privacy Policy and
Terms of Service apply.
</p>
<button type="submit" class="btn btn-default btn-lg contact_btn">Send</button>
</div>
</form>
Thanks
Harshith
FormSubmit has gives you 2 different options, you can either skip the ReCaptcha page (It's not recommended but offered.) or you can replace your naked email address with a random string.
Option 1
You can use below hidden input field to skip the captcha page:
<input type="hidden" name="_captcha" value="false">
Option 2
Get a random-like string to replace your naked email address in the
action attribute of your form. Your email address will remain unknown
to spam-bots and human visitors.
This random string will send to you when you confirm your email
address.
Please also refer to their official documentation: https://formsubmit.co/documentation
I deconstructed my email address into a javascript variable so that the email address isn't hard coded into the html. This avoids spam.
//email
const email = 'youremail#youremail.com';
const encodedEmail = encodeURIComponent(email);
const url = `https://formsubmit.co/${encodedEmail}`;
<form
target="_blank"
action={url}
method="POST"
>
<input
type="email"
name="email"
className="form-control"
placeholder="Email Address"
required
/>
<button type="submit">
SUBMIT FORM
</button>
</form>

ngSubmit "myFunction is not a function" typescript, angular

I have an address management app, and I'm trying to save changes on ngSubmit. However, it says that my function is not a function.
my HTML:
<div class="form">
<h4>{{title}}</h4>
<div class="form-container">
<form (ngSubmit)="addFriend()">
<div class="form-group">
<label for="id">Id</label>
<input type="text" name="id" [(ngModel)]="friend.id"
[disabled]="friend.id" class="form-control" required />
</div>
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" [(ngModel)]="friend.name"
class="form-control" required />
</div>
<div class="form-group">
<label for="address">Address</label>
<input type="text" name="address" [(ngModel)]="friend.address"
class="form-control" required />
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input type="text" name="phone" [(ngModel)]="friend.phone"
class="form-control" required />
</div>
<div class="form-group">
<a class="btn btn-default" routerLink="">Cancel</a>
<button class="btn btn-primary" >Save</button>
</div>
</form>
</div>
</div>
am I doing something wrong in the html, or would the problem be elsewhere?
The addFriend function is defined in my service.ts file.
You have to define addFriend in your component ts file like
addFriend(){
//some work here
}
Your component template does not have access to your service, but only to your component class methods.
So you need to
1/ inject your service into your component
2/ define in your component a method that calls the service methods
for example in your component :
constructor(private friendsService: FriendsService) {}
....
addFriend(){
return this.friendsService.addFriend();
}

Binding json - formgroup

I have a form and I want to display data that comes from an API (JSon file) in mode readonly. Do you have tracks?
I take an example of a form:
Thank you
<div class="col-sm-8 col-sm-offset-2">
<form [formGroup]="userForm" (ngSubmit)="onSubmitForm()">
<div class="form-group">
<label for="firstName">Prénom</label>
<input type="text" id="firstName" class="form-control" formControlName="firstName">
</div>
<div class="form-group">
<label for="lastName">Nom</label>
<input type="text" id="lastName" class="form-control" formControlName="lastName">
</div>
<div class="form-group">
<label for="email">Adresse e-mail</label>
<input type="text" id="email" class="form-control" formControlName="email">
</div>
<div class="form-group">
<label for="email">Adresse address</label>
<input type="text" id="address" class="form-control" formControlName="address">
</div>
<button type="submit" class="btn btn-primary">Soumettre</button>
</form>
</div>
File JSON
User:
- User1:
firstName: toto
lastName: titi
email: toto#toto.com
address: 2 rue titi
In your component (.ts file), as soon as the data comes back (typically inside of your ngOnInit), you should be able to do this.userForm.setValue(data).
As for making them readonly, an easy way I know is to set the FormGroup as disabled when you create it. So userForm = new FormGroup({_property_names_}, { disabled: true }).

Form not submitting values inside <div>

I am working with Laravel 5.4 Framework and I get this issue when the form does not submit anything except the csrf_token().
If I put an input outside of those <div> it will work and submit it, otherwise the browser (Chrome) will only submit the token, as if everything inside the <div> is not part of the form.
How can I submit all the data while using Bootstrap's form-group divs? The layout is not the problem as it does not work even when removing it.
#extends('layouts.master')
#section('content')
<div class="container" style="margin-top: 1%">
<p>Welcome, {{$user->name}}, here you can submit a new company for our database.</p>
<form name="suggestCompanyForm" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="companyName">Company Name</label>
<input type="text" class="form-control" id="companyName" aria-describedby="companyNameHelp" placeholder="Enter company name">
<small id="companyNameHelp" class="form-text text-muted">Enter the company's name you would like to suggest</small>
</div>
<div class="form-group">
<label for="companyEmail">Email address</label>
<input type="email" class="form-control" id="companyEmail" aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">Enter the company contact email.</small>
</div>
<div class="form-group">
<label for="selectCategory">Select main category</label>
<select class="form-control" id="selectCategory">
<option>Food&Drink</option>
<option>Cosmetics</option>
<option>Electronics</option>
<option>Consumer Goods</option>
<option>Services</option>
</select>
</div>
<div class="form-group">
<label for="description">Company description.</label>
<textarea class="form-control" id="description" rows="5"></textarea>
</div>
<div class="form-group">
<label for="logo">File input</label>
<input type="file" class="form-control-file" id="logo" aria-describedby="logoHelp">
<small id="fileHelp" class="form-text text-muted">Add company logo.</small>
</div>
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" id="check" class="form-check-input">
I agree that my submission follows the website rules.
</label>
</div>
<button id="submitCompany" type="submit" class="btn btn-primary" >Submit</button>
</form>
</div>
#endsection
your form inputs are not being submitted because you didn't give them names ...
Try
<textarea class="form-control" name="description" id="description" rows="5"></textarea>
You have to add the action attribute to the form tag.
Edit this line
<form name="suggestCompanyForm" method="post">
to
<form name="suggestCompanyForm" action = "<url-for-the-submission>" method="post">
.You will also need to add this
name="name_of_input_to_submit"
to each input not only the textarea and you are also uploading a file you have to add this to the form tag too.
enctype="multipart/form-data"

Making the _next option work in Formspree

I'm working on a contact form for a static website at www.peek.solutions. The contact form works, in that I'm (after confirmation of my e-mail address) able to receive e-mails at the given address. Here is the code used:
<form id="contactform" action="//formspree.io/kurt.peek#gmail.com" method="POST">
<div class="col-sm-7 slideanim">
<div class="row">
<div class="col-sm-6 form-group">
<input class="form-control" id="name" name="name" placeholder="Name" type="text" required>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="email" name="_replyto" placeholder="Email" type="email" required>
</div>
</div>
<textarea class="form-control" id="comments" name="message" placeholder="Message" rows="5"></textarea><br>
<div class="row">
<div class="col-sm-12 form-group">
<button class="btn btn-default pull-right" type="submit">Send</button>
</div>
</div>
</div>
<input type="hidden" name="_next" value="//peek.solutions/confirmation.html" />
<input type="text" name="_gotcha" style="display:none" />
</form>
The problem is that when I submit the form by pressing the "Submit" button, I'm redirected to Formspree's default confirmation page:
However, through the line
<input type="hidden" name="_next" value="//peek.solutions/confirmation.html" />
I was trying to make this confirmation page default to www.peek.solutions/confirmation.html. As far as I can tell, this line follows the documentation at https://formspree.io/. Can anyone tell why this is not working?
Updating your URLs to include http: will fix the issue. so for the _next value it should be http://peek.solutions/confirmation.html,
To prevent exposing your email, and using the now legacy method of using your email in forms, you can sign up for a free Formspree account and generate a custom endpoint. More info on legacy forms can be found here.