Email data from a Magento 2 Custom Contact form - magento2

I am working on Magento 2.
I have added an HTML contact form in a CMS page, but I am not able to make the submit button work the way I want it to. If I set it up as
<form action="mailto:email#yourdomain.com" method="post">
then hitting the submit button opens a new window. I've also tried a Javascript solution, but that's not working either.
I have also copied form.html and used the code in the CMS page to call the form, but clicking on the submit button then redirects me to the 'contact us' page and gives me a 'we can not process your request right now' error.
What I want to do is just to send the data to a prespecified email address when the submit button is clicked.

Related

Unable to hide content snippet on form submit in portals. Displays along with the success message after form submission

I am working on partner portal in dynamics 365 portals.
I included a content snippet(qrcode scanner) in the 'upload result' web page. This web page also has an entity form placed in it after the content snippet.
When I load the web page, the content snippet and the form display on the web page(as expected).
After filling the required details and submitting the form, the content snippet doesn't hide on form submission.
It still is displayed on the page along with the success message.
I added the following code in the Additional settings of the Entity form of the web page,
$(document).ready(function(){
$("#InsertButton").click(function(){ // onclick submit button
$('#snippet-scanner').parent().hide(); // hide the content snippet
});
});
it hides as soon as we click the submit button but then re-appears along with the success message.
Can you please suggest me a way to hide or remove the content snippet from the web page when the success message is displayed, after the form is submitted.
Your problem here is that the page reload on form submit. I see 3 possible solutions.
Quickfix - check if form exists on page load and hide content snippet dependently.
$(document).ready(function() {
if (!$(".entity-form").length) {
$("#snippet-scanner").hide();
}
})
My personal favourite - change success of form to redirect to the same page, append a custom query string such as success=true then use liquid to only render the content snippet and form if the success doesn't equal true (you would need to display your own success message)
Prevent default action of the form and submit it yourself with javscript. I have done this before and it's a bit fiddly, probably wouldn't recommend for a simple use case like this.

Send email when submit button is pressed

I have a really simple form that allows a user to input an email address here:
<form method="post" action="http://www.mydomain.com/page2/">
<input type="email" name="email">
<input type="submit" value="Submit">
</form>
This works correctly and it takes the visitor to www.mydomain.com/page2 when the submit button is clicked.
I am trying to get it to email me this input email address also when the submit button is clicked. I understand how to email using PHP but can the action have two urls?
Or is there a simpler way of doing this?
On /page2/ access the email in the global variable $_POST['email']. And then you can send it to yourself with PHP mail(). Example:
mail('myemail#domain.com', 'Someone submitted my form', 'Their email was: ' . $_POST['email']);
If you are stuck somewhere else, let me know and I can update the answer.
Once a form is submitted, you are no longer on that page. You've navigated away.
The other way you can do this is submit the first action via AJAX, then submit the form naturally to the second destination. I would suggest using jQuery to make your AJAX calls since most of the AJAX code is already there for you to use.
Another option is to have page2 be a php script, and have it perform the two actions once it receives the form data. See: Post to another page within a PHP script
I understand how to email using PHP
Then I would recommend writing some PHP code that sends the email to you.
but can the action have two urls?
No. A web browser can't make two requests at the same time. Which response would take precedence?
Nor does it need to. Now, you have a target already:
http://www.mydomain.com/page2/
Don't you control that page? That would be the page on which you'd put your PHP code for sending an email. If you don't control that page, then you would want an intermediary page. Something like:
sendmailandredirect.php
(Named solely to illustrate intent, you can call it what you like.) What this page would do is send the email, then issue a redirect to your final target. Something like:
header('Location: http://www.mydomain.com/page2/');
In effect, there would be "two urls" but they're invoked in serial instead of in parallel.
If you wanted to keep the code seperate and the action url as /page2/ you could fire off an ajax request on submit to your sendmail handler.

Wordpress cp-appointment-calendar redirection to paypal issues

I'm using cp-appointment-calendar for booking system on http://studioglamour.co.uk, but the problem is that when you enter date/time and information, you need to be redirected to paypal. I know that PayPal doesn't let ifames.
The question would be: how to do that when I press continue it would close the fancybox iframe, and redirect to PayPal in the main page?
Use target="_top" in the appropriate form / link.
When used in a form, the form will submit into the whole window.
When used in a link, the link will open in the whole window.

Submit button action

<FORM ID='htmlform' action="" onsubmit="return valforms(this)">
.....
<INPUT TYPE="submit" NAME="submit" VALUE="Submit">
When the user is done filling out the form, and clicks the submit button, I want all of the form information to be sent to me. How do I do this? This is only part of the code, the whole code is too long for me to type in here.
To reply on your question:
Oh, so like the page that emails the data is that an html page that I can have a message saying, "Thank you for your interest, you will hear back from us soon." and then have the page redirect to the home page again? I hope I'm not getting too complicated with this
You're almost right. Let me explain it to you:
the user gets a HTML page that was made in PHP, ASP,.. to fill in
some data in a form
the user fills in the data and clicks a button
The action on the button tells the server to process the page, or have another page to process it. Let's call this page PageX
PageX (written in PHP, ASP,...) will email the data to you
You can also have pageX return some text to the user's browser saying "thank you for your interest,....". You can also make a redirect to another page from this page
Does this answer your question?
The action part of the form element tells the browser what URL to post the information to. You would need to specify some page with some server-side code that would take that information and store it or send it in an email. The onsubmit part of your form element fires a JavaScript event that can be handled on the client's machine. You cannot do much on the client's machine without sending the data back to the server.

iPhone safari asks confirmation to submit form again when back button is pressed

I have a form. When I click on the submit button the form is submitted and result is loaded in the next page. In this page I have a hyperlink. On click of that hyperlink, I am going to another page. At this point if I click 'Back' button of the browser, I get a confirmation whether I should submit the form or not. How do I disable this ? I am storing results in sessionStorage so I do not need to submit form again.
Use the PRG pattern, which can be described as:
Never show pages in response to POST
Always load pages using GET
Navigate from POST to GET using REDIRECT
See http://en.wikipedia.org/wiki/Post/Redirect/Get and links from there