Email and a Download - php

My issue: I'm trying to make this form both email the form data and redirect to download page using php script (i.e.: One click = 2 actions). I searched the boards and didn't really find anything similar to what I am trying to do. I had tried several options code wise, but it won't send the email at all. What am I doing wrong?
code:
form:
<form id="myform">
<form method="get" action="action/php">
<fieldset><center>
<h3>DOWNLOAD DVD</h3>
<p> Enter your full name and email and then press Download DVD. </p>
<p><br>
<label>Enter Your Name *</label>
<input type="text" name="name" pattern="[a-zA-Z ]{5,}" maxlength="30" />
</p>
<p>
<label>Enter Your Email *</label>
<input type="email" name="email" required />
</p>
<button type="submit" id="submit-myform"; class="submit" value="Submit" name="myform_submit">Download DVD</button>
<button type="reset">Reset</button>
</fieldset>
</form>
php:
<?PHP
if(isset($_POST['myform_submit']) && $_POST['myform_submit'] == "Submit"){
echo "http://www.website.com";
}else {
mail( "info#website.com", "Landing Page Download",
$name, "From: $email" );
}
?>
Again ... The download content comes up nicely. But the email will not send.

I think you've got your if statement mixed up. Currently it's saying if the form is submitted, then print a URL to the screen, otherwise send an email but from what you've said you want to redirect and send an email. Try this:
if(isset($_POST['myform_submit'])) {
$send = mail( "info#website.com", "Landing Page Download", $_POST['name'], "From: " . $_POST['email'] );
if($send) {
header("Location: http://www.website.com");
} else {
echo 'Error sending email!';
}
}
Problem number 2 is you have nested forms. Not sure why you're doing this, but it's against HTML spec and will probably cause your form data not to get sent as it should. Remove the outer form. Here's line 3 of the HTML3(old!) spec:
Note you are not allowed to nest FORM elements!
Problem number 3, you're setting your form method as GET and then trying to access POST variables. Problem 3.5, your action is action/php - thats not a filename (unless you have an index.php file inside a folder called php, inside a folder called action). Change all this to:
<form method="post" id="myform" action="action.php">
Note: header("Location: [url]") sends a redirect header to your browser, so you are redirected to the target URL. If you simply want to display the URL (like in your question) then continue to just echo it.

Related

Sending an email using php

Given a standard html form such as the one below;
<form method="post" action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>>
<div class="row half">
<div class="6u"><input type="text" placeholder="Name" name="name" /></div>
<div class="6u"><input type="text" placeholder="Email" name="email" /></div>
</div>
<div class="row half">
<div class="12u"><textarea name="message" placeholder="Message" name="message"></textarea></div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li><input type="submit" class="button" value="Send Message" name="submit" /></li>
<li><input type="reset" class="button alt" value="Clear Form" name="clear"/></li>
</ul>
</div>
</div>
</form>
And the following php
if(isset($_POST['submit'])){
$to = "enquiries#appcloudkent.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$subject = "Website Enquiry";
$message = $name . " wrote the following:" . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
if(isValidString($from) && isValidString($name) && isValidString($message)){
mail($to,$subject,$message,$headers);
}
}
It is possible to send an email, however, this approach causes a couple of problems. Because my form is at the bottom of the page, when send is clicked the page is redirected back and the focus goes back to the top of the page.
What is the correct way to provide adequate user feedback to let the user know the email was sent, is it possible to navigate back to the page and automatically scroll to the bottom - allowing me to change the send button to green or something?
Failing that, is there a better approach to doing this?
Thanks
Add an anchor link before your form.
<a id="anchorName"></a>
Post your form to the anchor.
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>#anchorName">
The form action should point to the page itself (like it does in your example) so you can display error messages from validation (like you don't).
If the form is not at the top of your page you can add an anchor:
<h2><a name="theForm">The form</a></h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>#theForm">
If the form is processed properly (=mail sent), it is good practice to redirect to another page, showing a success message. So pressing F5 doesn't submit the form again (and send another mail).
if (mail($to,$subject,$message,$headers)) {
// redirect to page showing success message and exit script
} else {
// redirect to page showing error message and exit script
}
I usually redirect to the same page again, but attach an param ($_SERVER["PHP_SELF"].'?mail_success=1') and then inside the template I decide whether to show the success message, error message or the form:
if (isset($_REQUEST['mail_success'])) {
// show success message
} elseif (isset($_REQUEST['mail_error'])) {
// show error message, technical issues, try again bla bla
} else {
// show the form (including validation errors if necessary)
}
You could submit the form using an XMLHttpRequest, and cancel the default action of pressing submit. jQuery's .post() and event.preventDefault() methods are particularly good at that.

how do I get the message after submit to appear inside the pop up instead of on a different page

I have a contact form that I'm using Jquery .load to import a php file into any of the pages the nav will be on. Example below.
http://madaxedesign.co.uk/dev/index.html
I'm aware that the action form needs to be changed so it is connected to the right place. But how would I do that if it is on different pages and imported into a page. Because at the moment it is set to contact.php but after it is submitted it goes to that page and doesn't import the message into the pop up. So really I need it to be the file name depending on what page it is on.
So I suppose the question is how do I get the message after submit to appear inside the pop up instead of on a different page?
Code:
<?php
$your_email = "maxlynn#madaxedesign.co.uk";
$subject = "Email From Madaxe";
$empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>";
$thankyou_message = "<p>Thank you. Your message has been sent. We Will reply as soon as possible.</p>";
$name = stripslashes($_POST['txtName']);
$email = stripslashes($_POST['txtEmail']);
$message = stripslashes($_POST['txtMessage']);
if (!isset($_POST['txtName'])) {
?>
<form method="post" action="contact.php">
<div id="NameEmail">
<div>
<label for="txtName">Name*</label>
<input type="text" title="Enter your name" name="txtName" />
</div>
<div>
<label for="txtEmail">Email*</label>
<input type="text" title="Enter your email address" name="txtEmail" />
</div>
</div>
<div id="MessageSubmit">
<div>
<textarea maxlength="1200" title="Enter your message" name="txtMessage"></textarea>
<label for="txtMessage">Message</label>
</div>
<div>
<input type="submit" value="Submit" /></label>
</div>
</div>
</form>
<?php
}
elseif (empty($name) || empty($email) || empty($message)) {
echo $empty_fields_message;
}
else {
$referer = $_SERVER['HTTP_REFERER'];
$this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
if ($referer != $this_url) {
echo "You do not have permission to use this script from another URL, nice hacking attempt ;p.";
exit;
}
mail($your_email, $subject, $message, "From: $name <$email>");
echo $thankyou_message;
}
?>
You should use ajax, send the email without refreshing page.
What you want to do is only possible in javascript, this is a language that gets executed by the browser. Javascript self is a nasty language but there are many extensions/plugins to make this very easy like jQuery. i suggest you to learn this language, you will find a new world opening in web development ;-). eg: http://learn.jquery.com/
give your form an id:
<form method="post" id="test-form" action="contact.php">
so you can reference to it with jquery
now you can catch the form submit action with jQuery:
$('#test-form').submit(function() {
//send your data to your server and get the html data
$.post('contact.php', $(this).serialize(), function (data){
//here you can add the (html)data returned by the action to your page.
$('body').append(data); //append data to body of html page
})
return false; //stop form from going to the next page
});
this code is based on a javascript plugin: jQuery, if you want to do anything dynamic on your page without reloading the page, you need to use javascript.

Wordpress contact form

I've a very basic contact form on my Wordpress site (hard coded) and I can't get things to work. It works locally fine through XAMPP and I'm sure it's something I'm just missing but any help would be greatly appreciated. Thanks in advance!. I'm also using a template I created
<?php /* Template Name: contact */?>
<?php get_header(); ?>
<?php
//vars declared to store form input
$name=$email=$comment=$phone="";
//Error vars - to relay error message to the form
$nameError=$emailError=$commentError="";
$error_message="";
$sentMessage="";
$status=0; //Will monitor if all fields have no errors and increment if so.
function sanitise_var($string){
htmlentities($string);
strip_tags($string);
return stripslashes($string);
}
if(isset($_POST['submitted'])){
if($_POST['name']==""){
$nameError="Please enter a name";
$error_message="Oops, error in the form. Please check";
}
else {
$name=$_POST['name'];
++$status;
}
if($_POST['email'] == "" || !preg_match("/^[a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", $_POST['email'])){
$error_message="Oops, error in the form. Please check";
$emailError="Please enter a valid email";
}
else{
$email=$_POST['email'];
++$status;
}
if(!$_POST['phone']=="") $phone=$_POST['phone'];
if($_POST['comment']==""){
$error_message="Oops, error in the form. Please check";
$commentError="Please enter a message";
}
else{
$comment=$_POST['comment'];
++$status;
}
}//submitted if statement
if($status==3 && $_POST['submitted']){
$sentMessage="From: $name, email: $email, Phone: $phone, Comment: $comment";
wp_mail("mathornley#gmail.com", "From Android Scoop contact form", $sentMessage);
echo "Thanks, your email was sent successfully!";
}
else{
echo<<<SOQ
<div class="entry-content">
<h1 class="entry-title">Contact</h1>
<p class="contact">
If you have a query drop us a line using the form below. We're always happy to hear from people with ideas for posts and content they'd like to feature or maybe write about. Or maybe you just have some feedback you'd like to share with us. Why not just swing by and say hello.
</p>
<p class="requiring">* Denotes required fields</p>
<div class="form_left">
<form action="/contact/" method="POST">
<p><label>Name:</label><input type="text" name="name" value="$name"/></p>
<p class="error">$nameError</p>
<p><label>Email</label><input type="text" name="email" value="$email"/></p>
<p class="error">$emailError</p>
<p><label>Phone:</label><input type="text" name="phone" value="$phone"/></p>
<input type="hidden" name="submitted" value="yes"/>
<input type="submit" value="Send your message"/>
</div>
<div class="form_right">
<p><label>Message:</label><br/><textarea name="comment" rows="20" cols="20">$comment</textarea></p>
<p class="error">$commentError</p>
</form>
</div>
</div>
SOQ;
}
?>
<?php get_footer();?>
Try to use blank value for action like:
<form action="" method="POST">
If that doesn't work try renaming name parameter of the first input field to something else like:
<input type="text" name="myname" value="$name"/>
Am not aware about the wordpress but as the general PHP rules go, and according to what you replied me in the comment, the error lies here
<form action="/contact/" method="POST">
----^----
Why won't you use some out-of-the-box contact form for WordPress? For example, Contact Form 7 is pretty good.
Contact Form 7 is an open source software and can manage multiple contact forms, plus you can customize the form and the mail contents flexibly with simple markup. The form supports Ajax-powered submitting, CAPTCHA, Akismet spam filtering and so on.
Installation
Upload the entire contact-form-7 folder to the /wp-content/plugins/
directory.
Activate the plugin through the ‘Plugins’ menu in
WordPress.
You will find ‘Contact’ menu in your WordPress admin
panel.
Create a contact form, copy it's url, paste it everywhere you want.
Contact form will appear exactly in it's location.
If you want an example, you can check out this site to see it's look & feel.

Form redirection problem [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to stop form from sending email more times after initial success.
First of all, i have structured my website using directories. So basically every page is a directory and in that directory, i have a file called index.php.
I have four contact forms on my site, and at the moment, the seem to all work using the hnadler.php file. The handler file validates the data, checks the form-id posted and based on that, it routes the email appropraitely. A success message is displayed if successfully sent. However, my current implimentation is flawed in that if the user refreshes, another mail is sent. How can i solve this with my existing code? Thank you
//handler.php
<?php
if(isset($_POST['submit'])) {
//carry out validation
if(!isset($hasError)) {
//check the form id posted and set email address in $emailTo accordingly
$body = "Name: $name \n\nEmail: $email \n\nEnquiry: $enquiry";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
//index.php
<?php if(isset($hasError)) { ?>
<p class="error">Please make sure you have filled all fields with valid information. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { ?>
<p><strong>Your enquiry was sent successfully.</strong></p>
<p>Thank you for your enquiry! Your email was successfully sent and we will be in touch with you promptly.</p>
<?php }; ?>
<form id="contactform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Enquiry form</legend>
<label for="name">Name:</label><input type="text" size="50" name="name" id="name" value="" class="required" />
<label for="email">Email:</label><input type="text" size="50" name="email" id="email" value="" class="required email" />
<label for="enquiry">Enquiry:</label><textarea rows="5" cols="20" name="enquiry" id="enquiry" class="required"></textarea>
<input type="submit" name="submit" value="Submit enquiry" class="curved-btn"></input>
<input type="hidden" id="form-id" name="form-id" value="general"></input>
</fieldset>
</form>
?>
The problem is that you aren't actually submitting to handler.php because of this:
<form id="contactform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Change it to this:
<form id="contactform" method="post" action="handler.php">
And put your send e-mail code inside of handler.php. You will also need to put a redirect in there to get them back to a page. There are other ways to go about this, but this is how I would do it.
The browser will/can repost the data on refresh, so it will look like a new request.
A quick fix is to redirect after the form submission:
header("Location: success.php");
That way if the refresh they refresh the success page, not the page you posted to.

POST without redirect with PHP

I have a simple form for a mailing list that I found at http://www.notonebit.com/projects/mailing-list/
The problem is when I click submit all I want it to do is display a message under the current form saying "Thanks for subscribing" without any redirect. Instead, it directs me to a completely new page.
<form method="POST" action="mlml/process.php">
<input type="text" name="address" id="email" maxlength="30" size="23">
<input type="submit" value="" id="submit"name="submit" >
</form>
You will need AJAX to post the data to your server. The best solution is to implement the regular posting, so that will at least work. Then, you can hook into that using Javascript. That way, posting will work (with a refresh) when someone doesn't have Javascript.
If found a good article on posting forms with AJAX using JQuery .
In addition, you can choose to post the data to the same url. The JQuery library will add the HTTP_X_REQUESTED_WITH header, of which you can check the value in your server side script. That will allow you to post to the same url but return a different value (entire page, or just a specific response, depending on being an AJAX request or not).
So you can actually get the url from your form and won't need to code it in your Javascript too. That allows you to write a more maintanable script, and may even lead to a generic form handling method that you can reuse for all forms you want to post using Ajax.
Quite simple with jQuery:
<form id="mail_subscribe">
<input type="text" name="address" id="email" maxlength="30" size="23">
<input type="hidden" name="action" value="subscribe" />
<input type="submit" value="" id="submit"name="submit" >
</form>
<p style="display: none;" id="notification">Thank You!</p>
<script>
$('#mail_subscribe').submit(function() {
var post_data = $('#mail_subscribe').serialize();
$.post('mlml/process.php', post_data, function(data) {
$('#notification').show();
});
});
</script>
and in your process.php:
<?php
if(isset($_POST['action'])) {
switch($_POST['action']) {
case 'subscribe' :
$email_address = $_POST['address'];
//do some db stuff...
//if you echo out something, it will be available in the data-argument of the
//ajax-post-callback-function and can be displayed on the html-site
break;
}
}
?>
It redirects to a different page because of your action attribute.
Try:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input type="text" name="address" id="email" maxlength="30" size="23" />
<input type="submit" value="" id="submit" name="submit" />
</form>
<?php if (isset($_POST['submit'])) : ?>
<p>Thank you for subscribing!</p>
<?php endif; ?>
The page will show your "Thank You" message after the user clicks your submit button.
Also, since I don't know the name of the page your code is on, I inserted a superglobal variable that will insert the the filename of the currently executing script, relative to the document root. So, this page will submit to itself.
You have to use AJAX. But that requires JavaScript to be active at the users Brwoser.
In my opinion it's the only way to do without redirect.
to send a form request without redirecting is impossible in php but there is a way you can work around it.
<form method="post" action="http://yoururl.com/recv.php" target="_self">
<input type="text" name="somedata" id="somedata" />
<input type="submit" name="submit" value="Submit!" />
</form>
then for the php page its sending to have it do something but DO NOT echo back a result, instead simply redirect using
header( 'Location: http://yourotherurl.com/formpage' );
if you want it to send back a success message simply do
$success = "true";
header( 'Location: http://yourotherurl.com/formpage?success='.$success);
and on the formpage add
$success = $_GET['success'];
if($success == "true"){ echo 'Your success message'; } else { echo
'Your failure message';
Return and print the contents of another page on the current page.
index.php
<html>
<body>
<p>index.php</p>
<form name="form1" method="post" action="">
Name: <input type="text" name="search">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$_POST['search'];
include 'test.php';
}
?>
</body>
</html>
test.php
<?php
echo 'test.php <br/>';
echo 'data posted is: ' . $_POST['search'];
?>
Result:
Just an idea that might work for you assuming you have no control over the page you are posting to:
Create your own "proxy php target" for action and then reply with the message you want. The data that was posted to your php file can then be forwarded with http_post_data (Perform POST request with pre-encoded data). You might need to parse it a bit.
ENGLISH Version
It seems that no one has solved this problem without javascript or ajax
You can also do the following.
Save a php file with the functions and then send them to the index of your page
Example
INDEX.PHP
<div>
<?php include 'tools/edit.php';?>
<form method="post">
<input type="submit" name="disable" value="Disable" />
<input type="submit" name="enable" value="Enable" />
</form>
</div>
Tools.php (It can be any name, note that it is kept in a folder lame tools)
<?php
if(isset($_POST['enable'])) {
echo "Enable";
} else {
}
if(isset($_POST['disable'])) {
echo "Disable";
} else {
}
?>
Use
form onsubmit="takeActions();return false;"
function takeAction(){
var value1 = document.getElementById('name').innerHTML;
// make an AJAX call and send all the values to it
// Once , you are done with AJAX, time to say Thanks :)
document.getElementById('reqDiv').innerHTML = "Thank You for subscribing";
}

Categories