So basically, I want to have someone complete a form, run a php script, and instead of pulling up the php file's web page, I want to append text at the end of the form.
Currently, I have a form:
<form action="scripts/email.php" method="POST">
<p>Name: <input type="text" name="name" placeholder="Name"></p>
<p>Email: <input type="text" name="email" placeholder="Email"></p>
<p>Subject: <input type="text" name="subject" placeholder="Subject"></p>
<p>Message: </p>
<p><textarea name="message"></textarea></p>
which looks like this:
When 'Submit' is clicked, it leads to my PHP script:
<?php
$from = $_POST['email'];
$to = "comp#c0mp.org";
$subject = $_POST['subject'];
$name = $_POST['name'];
$message = $_POST['message'];
mail($to, $subject, "Name: " . $name . "\nMessage: " . $message, "From:" . $from);
print "Your message has been sent!";
?>
which leads to a blank web page that just says this in plain text:
"Your message has been sent!"
Basically, what I want is text added onto the submit form after the script is ran, such as:
http://i.stack.imgur.com/zha4d.png
How can I achieve running a PHP script on a web page, and instead of loading a new web page of the PHP script's path, but append text on the current HTML web page?
TRY THIS : WRITE THIS CODE INTO A SINGLE PAGE.
<form action="<?php $PHP_SELF ;?>" method="POST">
<p>Name: <input type="text" name="name" placeholder="Name"></p>
<p>Email: <input type="text" name="email" placeholder="Email"></p>
<p>Subject: <input type="text" name="subject" placeholder="Subject"></p>
<p>Message: </p>
<p><textarea name="message"></textarea></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit'] ))
{
$from = $_POST['email'];
$to = "comp#c0mp.org";
$subject = $_POST['subject'];
$name = $_POST['name'];
$message = $_POST['message'];
mail($to, $subject, "Name: " . $name . "\nMessage: " . $message, "From:" . $from);
echo "Your message has been sent";
}
?>
you can use ajax for acheive more secure and fast execution of script.
HTML code:
<form method="POST">
<p>Name: <input type="text" id="name" name="name" placeholder="Name"></p>
<p>Email: <input type="text" id="email" name="email" placeholder="Email"></p>
<p>Subject: <input type="text" id="subject" name="subject" placeholder="Subject"></p>
<p>Message: </p>
<p><textarea id="area" name="message"></textarea></p>
<input type="submit" name="submit" value="Submit" onclick="mail_js();">
</form>
<div id="success_msg" style="display:none;"></div>
Just give proper style to this div.
JS code:
function mail_js()
{
var input_val=$('#name').val();
var email_val=$('#email').val();
var subject_val=$('#subject').val();
var message_val=$('#area').val();
jQuery.ajax({
type: 'post',
data:{
'input_val':input_val,
'email_val':email_val,
'subject_val':subject_val,
'message_val':message_val
},
url: 'email.php',
success:function(response){
$("#success_msg").append(response).show();
},
error: function(errorThrown){
alert('error');
console.log(errorThrown);
}
});
}
PHP script:
$from = $_POST['email_val'];
$to = "comp#c0mp.org";
$subject = $_POST['subject_val'];
$name = $_POST['input_val'];
$message = $_POST['message_val'];
mail($to, $subject, "Name: " . $name . "\nMessage: " . $message, "From:" . $from);
echo "Your message has been sent";
Related
I'm trying to make a simple contact form page with a combination of HTML and PHP with an upload image file section to it. I am 99% sure I got the HTML part correct but I am just lost with the PHP part.
So here is my HTML
<form class="" action="contactform.php" method="post">
<input type="text" name="firstname" placeholder="First Name" required>
<input type="text" name="lastname" placeholder="Last Name" required>
<input type="text" name="mail" placeholder="Your e-mail" required>
<input type="text" name="phone" placeholder="Phone">
<input type="file" id="myFile" name="filename">
<textarea name="message" placeholder="Message" required></textarea>
<button type="submit" name="submit">Send</button>
</form>
And here is my PHP
if (isset($_POST['submit'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "email#email.com";
$headers = "From: ".$mailFrom;
$subject = "You have received an e-mail from your web site";
$message = "First Name: " . $firstname . " Last Name: " . $lastname . "\r\nPhone: " . $phone . "\r\nemail: " . $mailFrom . "\r\nMessage: " . $message;
$retval = mail( $mailTo, $subject, $message, $headers );
if ( $retval == true ){
echo "<h1>Message Sent</h1>
<p>Thank you for contacting us your message has been sent successfully and we will get back to you a soon as possible.</p>";
}
else {
echo "<h1>Message Sent</h1><p>Message could not be sent Mail server did not accept mail.</p>";
}
}
Does anyone know how I can make the upload file part work?
try changing if($retval==true) to if($retval!=false) because I believe it only returns false if it fails and if it is successful it will return a hash value, so I believe making sure it's not true will work.
Also just you might want to take mail( $mailTo, $subject, $message, $headers ); and copy it into its own function, but I'm not sure. Kinda like this:
$retval = mail( $mailTo, $subject, $message, $headers );
mail( $mailTo, $subject, $message, $headers );
I'm not sure though, I hope this works, sorry if it doesn't!
I recently made a website using HTML, CSS, and JS. Since I don't know PHP, I am stuck at building the contact form where it is vital on the website. I learned a bit from YouTube tutorials and have the following HTML & PHP code:
<div class="contact_form">
<form action="/action_page.php">
<input type="text" id="name" name="name" placeholder="Name*">
<input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">
<input type="text" id="phone" name="phone" placeholder="Phone No.">
<input class="contact_even" type="text" id="city" name="city" placeholder="City">
<textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>
<input type="submit" value="Submit">
</form>
</div>
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$message = $_POST['message'];
$mailTo = 'example#something.in';
$headers = 'From: '.$mailFrom;
$txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;
$headers = "MIME-VERSION: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($mailTo, $headers, $txt);
header("Location: index.html?mailsent");
}
?>
Why do I need the MIME and content-type headers at the bottom as that bit I added from another tutorial.
When I use the form and try sending the message, I get the "?mailsent" after the URL but I receive no email which is a professional plan by GoDaddy.
They are also hosting my website. I contacted them to know whether the server allows me to make my contact form with the plan I have and they said yes. So, I must be missing something important here.
refer to the documentation of mail function
there are 3 required parameter : email destination (to), subject and the message, and two additional options are: headers and parameters.
your code didnt respect that, because you missing to add the subjuct as parameter.
and you get ?mailsent because you use header("Location: index.html?mailsent") without any test if the email send successfully or not.
i suggest you to replace the last two lines of your php code with this
$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]
$result = mail($mailTo, $subject , $txt,$headers);
if ($result){
// mail send successfully
header("Location: index.html?mailsent");
} else {
// error
}
EDIT:
you can get the error message with error_get_last() function.
thanks to https://stackoverflow.com/a/20203870/195835
$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]
$result = mail($mailTo, $subject , $txt,$headers);
if ($result){
// mail send successfully
header("Location: index.html?mailsent");
} else {
print_r(error_get_last());
}
You are missing the form action, So PHP doesn't know what to do with your variable data.Try adding method="post" inside <form> tag. Like this
<div class="contact_form">
<form action="/action_page.php" method="post">
<input type="text" id="name" name="name" placeholder="Name*">
<input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">
<input type="text" id="phone" name="phone" placeholder="Phone No.">
<input class="contact_even" type="text" id="city" name="city" placeholder="City">
<textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>
<input type="submit" value="Submit">
</form>
</div>
And also. If you use your computer as localhost(using xampp , wamp, or something without a hosting service) You have to make some changes to the config files.
Also try this modified php code
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$message = $_POST['message'];
$title = "replace this";
$mailTo = 'support#udichi.in';
$txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;
$headers = 'From: '.$mailFrom . PHP_EOL .'Reply-To:' .$mailFrom . PHP_EOL . 'X-Mailer: PHP/' . phpversion();
mail($mailTo,$title,$txt,$headers);
header("Location: index.html?mailsent");
}
?>
I am trying to send an email to user account using php mail() function.The mail is sent successfully but the issue is that it is sending me blank emails with no values in them! The code for the contact page that sends the email is as follows:-
<form class="contact-form" method="POST" action="sendemail.php">
<div class="row-fluid">
<div class="span5">
<label>First Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your First Name" name="name">
<label>Last Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your Last Name" name="lname">
<label>Email Address</label>
<input type="text" class="input-block-level" required="required" placeholder="Your email address" name="email">
</div>
<div class="span7">
<label>Message</label>
<textarea name="message" id="message" required="required" class="input-block-level" rows="8" name="message"></textarea>
</div>
</div>
<input type="submit" class="btn btn-primary btn-large pull-right" value="Send Message" />
<p> </p>
</form>
and sendemail.php page code is as follows:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = "An enquiry sir";
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'email#email.com';
echo $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
?>
Why is the output I get blank in my email id, for example:
Name:
Email:
Subject:
Message:
P.N: I am using here a nova template theme.
The form is being submitted via AJAX using the following JavaScript:
var form = $('.contact-form');
form.submit(function () {
$this = $(this);
$.post($(this).attr('action'), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
},'json');
return false;
});
the form submission code is not submitting the form data. Here is the code you provided:
var form = $('.contact-form');
form.submit(function () {
$this = $(this);
$.post($(this).attr('action'), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
},'json');
return false;
});
and this is what it should be:
var form = $('.contact-form');
form.submit(function () {
$this = $(this);
$.post($(this).attr('action'), $(this).serialize(), function(data) {
$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
},'json');
return false;
});
Remove the echo from the line that defines $body
From this...
echo $body = 'Name: ' . $name . "\n\n" . 'Email
To this...
$body = 'Name: ' . $name . "\n\n" . 'Email
I am trying to use jQuery to post data to an emailus.php script.
Here is the script part:
<script>
jQuery(document).ready(function(){
jQuery('#submitt').click(function(){
jQuery.post("/emailus.php", jQuery("#mycontactform").serialize(), function(response) {
jQuery('#success').html(response);
});
return false;
});
});
</script>
and here is the HTML used:
<form action="" method="get" id="mycontactform" >
<label for="name">Your Name:</label><br />
<input type="text" name="name" class="cleann" /><br />
<label for="email">Your Email:</label><br />
<input type="text" name="email" class="cleann" /><br />
<label for="message">Your Message:</label><br />
<textarea name="message" class="cleann" rows="7"></textarea><br />
<input type="button" value="send" id="submitt" class="cleannsubmit" /><div id="success" style="color:green;"></div>
</form>
and here is the php script:
<?php
// Here we get all the information from the fields sent over by the form.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'nohanada#gmail.com';
$subject = 'Fortrove Contact';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message.'\n\nItem:'.$itemname;
print_r($_POST);
if($name && $email && $message){
if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
mail($to, $subject, $message);
echo "Your email was sent!";
}
else echo "<span style='color:red'>Invalid email format.</span>";
}
else echo "<span style='color:red'>Please fill all fields</span>";
?>
Problem is that it does not POST the actual fields to the php script. What am i doing wrong?
you have placed the mycontactform inside product_addtocart_form that is the reason which is not allowed so the browser seems to be remocing the mycontactform
Hey I have a simple coding issue and I am hoping someone can spot the error of my ways.
I have a contact page http://mattmcdougall.ca/contact
this page has a form that is connected to a form PHP.
it is half working, I receive the email with ONLY the name subject and recipient (me). I do not get the other information in the form. (phone number and content do not get placed in the email) ALSO, I want the website to bring the person filling out the form to http://mattmcdougall.ca/contactdone currently it goes to a blank form.php page
here is the form code and PHP code, please feel free to clean up PHP as I used a template I found online.
<form action="form.php" method="post" class="formtext">
<input name="Name" type="text" value="enter name" maxlength="60"><br>
<input name="Email" value="enter email" type="text"><br>
<input name="Phone Number" value="enter phone#"type="text"><br><br>
<textarea name="Interest" cols="60" rows="10">Interest?</textarea><br><input name="Submit" type="submit" value="Submit"><form
and the PHP
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Matt McDougall Photography</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<?php
$name = $_POST['name']; //senders name
$email = $_POST['email']; //senders e-mail adress
$phone = $_POST['phone'];
$recipient = "matt#mattmcdougall.ca"; //recipient
$mail_body = $_POST['interest']; //mail body
$subject = "interested client"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n";
mail($recipient, $subject, $mail_body, $phone, $header); //mail command :)
if(mail)
'http://mattmcdougall.ca/contactdone.html';
else
echo "Sorry Something Went Wonky, Please Try Again!";
?>
</BODY>
</HTML>
You're going to need to change the name of your form elements to match the params that you're expecting to receive via $_POST.
Right now you have (for example):
<input name="Phone Number" value="enter phone#"type="text"><br><br>
But you're going to need to name it:
<input name="phone" value="enter phone#"type="text"><br><br>
The name attribute is what is going to be submitted via the form to your PHP script. You need to use the same name on both sides.
Also what is the $phone variable in your mail function call? Those areas are reserved for additional headers and not just $phone. You'll need to concatenate the phone number into the $mail_body like so (with additional formatting of course):
$mail_body = $mail_body . " " . $phone;
This is the correct signature:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Edit: In addition to all of this, your $header is also wrong as it's trying to use the $Name variable which does not exist. The $name variable does exist.
I think this below should solve your problems.
Form:
<form action="form.php" method="post" class="formtext">
<input type="text" name="name" placeholder="Enter Name" maxlength="60"><br>
<input type="text" name="email" placeholder="Enter Email"><br>
<input type="text" name="phone" placeholder="Enter Phone #"><br><br>
<textarea name="interest" cols="60" rows="10" placeholder="Interest?"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
form.php
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$interest = $_POST["interest"];
$to = "matt#mattmcdougall.ca";
$subject = "Interested Client";
$headers = "From: {$name} <{$email}>\r\n";
$mail_body = <<<EMAIL
Name: {$name}
Phone: {$phone}
Message:
{$interest}
EMAIL;
$mail_success = mail($to, $subject, $mail_body, $headers);
if($mail_success) {
echo "http://mattmcdougall.ca/contactdone.html"; // are you trying to redirect to here?
} else {
echo "Sorry, something went wonky! Please try again!";
}
?>
HTTP POST variables are case sensitive, and the names of the input fields do not match that of those in PHP. <input type="text" name="datanamehere" /> with $_POST["datanamehere"]. Like what other answers said:
HTML:
<form action="form.php" method="post" class="formtext">
<input name="name" placeholder="Full name" type="text" maxlength="60"><br>
<input name="email" placeholder="Email" type="email"><br>
<input name="phone" placeholder="Phone number" type="tel"><br><br>
<textarea name="interest" placeholder="Interested in?" cols="60" rows="10"></textarea><br>
<input type="submit" value="Submit">
</form>
Notice I changed the value to placeholder, and used type="email" and type="tel".
form.php
$name = $_POST['name']; //senders name
$email = $_POST['email']; //senders e-mail adress
$phone = $_POST['phone'];
$recipient = "matt#mattmcdougall.ca"; //recipient
$mail_body = $_POST['interest']; //mail body
$subject = "interested client"; //subject
$header = "From: ". $name . " <" . $email . ">\r\n";
if (mail($recipient, $subject, $mail_body, $header)) //mail command :)
'http://mattmcdougall.ca/contactdone.html';
else
echo "Sorry Something Went Wonky, Please Try Again!";
As they said, mail() is a function returning a boolean. Wrap it around if to check for success.
Your element names do not match the ones in the PHP code and your form is not properly closed using .
<form action="form.php" method="post" class="formtext">
<input name="name" type="text" value="enter name" maxlength="60"><br>
<input name="email" type="text" value="enter email"><br>
<input name="phone" type="text" value="enter phone#"><br><br>
<textarea name="Interest" cols="60" rows="10">Interest?</textarea><br>
<input name="submit" type="submit" value="Submit">
</form>
mail is a function, so checking it in the 'if' by just writing mail isn't going to work.
try this instead
if(mail()):
// go to contactdone.html
header('Location: http://www.google.com');
exit();
else:
echo "Sorry Something Went Wonky, Please Try Again!";
endif;
Couple of things you can try,
if(mail)'http://mattmcdougall.ca/contactdone.html' is not going to do
anything, try using Header('Location
http://mattmcdougall.ca/contactdone.html')
Whenever you use $_POST the parameter name inside the [''] need to
match your input names (Example: <input name="interest" type="text">
would be $_POST['interest'])
If you want to show text inside an input use placeholder="enter
phone#" instead of value.