I want to display a message something like: "Congrats! you have been registered".
But the message appears on the top left corner of the page. I want to display it in the same page below the "Submit Button".
Here's my code:
if($result)
{
//send the email
$to = "xyz#abc.com";
$subject = "New order for Weaving Hope";
//headers and subject
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: ".$name." <".$email.">\r\n";
$body = "New contact<br />";
$body .= "Name: ".$name."<br />";
$body .= "Email: ".$email."<br />";
$body .= "Contact No.: ".$contact."<br />";
$body .= "Item Id: ".$itemid."<br />";
$body .= "Quantity: ".$itemquantity."<br />";
$body .= "Comment: ".$comment."<br />";
$body .= "IP: ".$ip."<br />";
mail($to, $subject, $body, $headers);
//ok message
echo "Congrats!";
}
This page should be easy to follow, it tells you exactly what to do.
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
If you need to do this without AJAX, then you need to process the form data in the same PHP page that sends the form to the browser, and set a flag to determine whether or not the data submission was successful.
For example:
<?php // Call this page register.php
$success = false;
if( $result)
{
// .. Your code from above
$success = true;
}
?>
<form action="register.php" method="post">
<!-- Output the form for submission here -->
<input type="submit" name="register" value="Register" />
<?php if( $success): ?>
<span>Congrats, you are registered!</span>
<?php endif; ?>
</form>
Related
This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 6 years ago.
I've never worked with php before and am trying to make my contact form functional with the help of some php code I acquired through google.
First off do I need to link it to my html page somehow (like css/ javascript) or does it work fine if it shares the same folder?
I have modified the names of certain parts to match the names in my html but am unable to get it to work. When I click the submit button I get a page saying...
"; exit; } // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Company: "; $Body .= $Company; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Deadline: "; $Body .= $Dealine; $Body .= "\n"; $Body .= "Interested: "; $Body .= $Interested; $Body .= "\n"; $Body .= "Message: "; $Body .= $Message; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success){ print ""; } else{ print ""; } ?>
The php code is as follows.
<?php
$EmailFrom = "c#c.co.nz";
$EmailTo = "c#c.co.nz";
$Subject = "contact-form";
$Name = Trim(stripslashes($_POST['Name']));
$Company = Trim(stripslashes($_POST['Company']));
$Email = Trim(stripslashes($_POST['Email']));
$Deadline = Trim(stripslashes($_POST['Dealine']));
$Interested = Trim(stripslashes($_POST['Interested']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Deadline: ";
$Body .= $Dealine;
$Body .= "\n";
$Body .= "Interested: ";
$Body .= $Interested;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contact-thanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
Here is my html
<div role="form" class="form-wrapper">
<form name="contact-f" action="contact-form.php" method="post">
<div class="f-width">
<div class="f-col-1 f-group-1"><input type="text" name="Name" class="c-back input-t f-col-1" placeholder="Name" /></div>
<div class="f-col-1 f-group-2"><input type="email" name="Company" class="c-back input-t f-col-1" placeholder="Company Name" /></div>
<div class="f-col-1 f-group-1"><input type="text" name="Email" class="c-back input-t f-col-1" placeholder="Email Address" /></div>
<div class="f-col-1 f-group-2"><select name="Deadline" class="c-back input-t f-col-1"><option class="opt-f">Do you have a Deadline?</option><option value="Not yet" class="opt-f">Not yet</option><option value="Less than 1 Month" class="opt-f">Less than 1 Month</option><option value="2-3 Months" class="opt-f">2-3 Months</option><option value="3-6 Months" class="opt-f">3-6 Months</option><option value="6+ Months" class="opt-f">6+ Months</option></select></div>
<div class="f-col-2 f-group-3"><select name="Interested" class="c-back input-t f-col-2"><option class="opt-f">What are you interested in?</option><option value="Branding" class="opt-f">Branding</option><option value="Print Design" class="opt-f">Print Design</option><option value="Illustration" class="opt-f">Illustration</option><option value="Website / UI Design" class="opt-f">Website / UI Design</option><option value="Literature" class="opt-f">Literature</option><option value="Video Editing" class="opt-f">Video Editing</option><option value="Other" class="opt-f">Other</option></select></div>
<div class="f-col-3 f-group-4"><textarea name="Message" class="c-back input-t message-f f-col-3" placeholder="Describe your project..."></textarea></div>
<div class="f-submit f-group-5"><input type="Submit" name="Submit" value="Send" class="form-btn" /></div>
</div></form></div>
(And yes I have created the contact-thanks.php file it calls for at the end).
So many $Body , How About :
$Body = " Name : $Name \n Company : $Company \n Deadline : $Deadline \n Interested : $Interested \n Message : $Message";
Is This What You Looking For ?
<textarea rows="4" cols="70" name="filename" id="result" style="background:#B0D2D7;
width:100%;overflow:auto;resize:none"
readonly><?php echo $_POST['filename']; ?></textarea>
Hi all, this is a snippet of code I'm using. What I'm struggling with is how to send
the $_POST results that get displayed on the next page into an email using PHP email.
The results are not displayed in a text box as such on the next page but displayed more like a print_pr into a PHP form.
Any help would be great!
To move data across into another page via POST, the easiest way is to wrap your textarea in a form and add a submit button:
<form action="?" method="post">
<textarea name="filename"></textarea>
<input type="submit" />
</form>
<?php
if(isset($_POST["filename"]))
{
echo $_POST["filename"];
}
?>
You also made a vague reference to sending an email, which can be done using the mail() function.
<?
$g_mail = "mail#domain.com";
$s_name = "Some name";
$to = "Receiver <receiver#domain.com>";
$subject = "Some subject";
$message= "HTML codes here. Write anything you want including " . $_POST['data'];
$header = "From: $s_name <".$g_mail.">\n";
$header .= "Reply-To: $s_name <".$g_mail.">\n";
$header .= "Return-Path: $s_name <".$g_mail.">\n";
$header .= "Delivered-to: $s_name <".$g_mail.">\n";
$header .= "Date: ".date(r)."\n";
$header .= "Content-Type: text/html; charset=iso-8859-9\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Importance: Normal\n";
$header .= "X-Sender: $s_name <".$g_mail.">\n";
$header .= "X-Priority: 3\n";
$header .= "X-MSMail-Priority: Normal\n";
$header .= "X-Mailer: Microsoft Office Outlook, Build 11.0.5510\n";
$header .= "X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869\n";
mail($to, $subject, $message, $header);
?>
This is the mail script I generally use. It will not be marked as spam on most mail providers aswell.
Hi I have simple contact form for email on my site, and the form works including the success of a message sent,however, I am not receiving email to the designated webmail server. I am running the latest PHP. Do some web servers cache mail or is there some error in this code I am not seeing.
<?php
$EmailFrom = "email#mydomain.com";
$EmailTo = "email#mydomain.com";
$Subject = "Contacting Me";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=index\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
<div id="contact-area">
<form method="post" action="contactengine.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" />
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" />
<label for="Message">Thought:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message"></textarea>
<input type="submit" name="submit" value="Transmit" class="submit-button" />
</form>
<div style="clear: both;"></div></div>
Sending mail is more complicated than just running the mail() command, you need to take a look at the php.ini settings for the email configuration and continue troubleshooting from there.
If you are running on a host that does not configure the mail() function to run, you can try using the following opensource mailer:
PHPMailer
What you can try is to use the examples provided and run from your local development stack (XAMPP, MAMP, WAMP) connected from your home, you should be able to receive mail if configured correctly.
Once it is OK, you can try it on the server, it should send as well, if not, check that the outgoing port for sending mail is not blocked.
Alternative to using PHPMailer, you can look for a simple PHP mailer and try those out as well.
Don't use print "<meta http-equiv=\"refresh\"... because during testing, it caused an endless loop while sending multiple copies of the same Email.
Use header('Location: ...'); instead, along with exit;.
Plus, all your <meta http-equiv=\"refresh\" have generated a parse error.
Working example (tested with Email successfully received):
<?php
$EmailFrom = "email#mydomain.com";
$EmailTo = "email#mydomain.com";
$Subject = "Contacting Me";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
// echo "error 1";
header('Location: error.htm');
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
header('Location: http://www.example.com/');
exit;
}
else{
header('Location: error.htm');
exit;
}
?>
i think its the mail function headers issue.
http://php.net/manual/en/function.mail.php
http://php.net/manual/en/function.mail.php
I'm having some problems with my contact page.
Here are the parts:
<form method="post" action="mail.php">
<input name="nome" type="text" style="width: 265px;" placeholder="Nome e Cognome">
<input name="mail" type="email" style="width: 263px;" placeholder="E-mail">
<textarea name="messaggio" placeholder="Messaggio"></textarea>
<button type="submit" name="invia" style="margin-left: 0; margin-top: 10px;">Invia</button>
</form>
and..
<?php
$to = "mail";
$subject = "Modulo proveniente dal sito www.miosito.it";
$body = "Contenuto del modulo:\n\n";
$body .= "Nome: " . trim(stripslashes($_POST["nome"])) . "\n";
$body .= "Email: " . trim(stripslashes($_POST["mail"])) . "\n";
$body .= "Messaggio: " . trim(stripslashes($_POST["messaggio"])) . "\n";
$headers = "From: Valle srl <info#vallesrl.com>";
"Content-Type: text/html; charset=iso-8859-1\n";
if(#mail($to, $subject, $body, $headers)) {
header("Location: http://www.alessandrogiordano.me/test/valle02/sent.php");
} else {
header("Location: http://www.alessandrogiordano.me/test/valle02/nosent.php");
}
?>
First of all.. if I click on the submit button with all blank the email is sent blank.
Even if I make some errors and I get the else message the email is sent.. blank obviously.
I'm going crazy.. I'm making this website for free for a friend but I'm a graphic designer not a web developer. Never again! :D
Help!! Thank you very much.
put this line in your headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Before sending mail you have to validate all the fields whether they are empty or not
Like this
if( trim($_POST["nome"]) != "" )
{
// send mail
$isMailSend = mail($to, $subject, $body, $headers);
}
{
//show error
}
if( $isMailSend ) {
header("Location: http://www.alessandrogiordano.me/test/valle02/sent.php");
} else {
header("Location: http://www.alessandrogiordano.me/test/valle02/nosent.php");
}
This
$headers = "From: Valle srl <info#vallesrl.com>";
"Content-Type: text/html; charset=iso-8859-1\n";
Should be,
$headers = "From: Valle srl <info#vallesrl.com>";
$headers. = "Content-Type: text/html; charset=iso-8859-1\n";
you can't use type="email",there should be type="text"
I'm trying to send a small batch of emails in a loop using PHP mail(). The script to send the emails works fine. There is, however, a slight glitch. Whilst the recipients all receive only one email, the first person on the list receives the email body ($MESSAGE_BODY) once, the second person gets the body twice and the third person gets it 3 times (and on it goes). I cannot for the life of me work out why it's doing it.
The form from which the emails are sent is:
<p>Message Text:
<br />
<textarea name="thebody" id="thebody" cols="65" rows="12"><?php echo $row_email['emailtext'];?></textarea>
<script type="text/javascript">CKEDITOR.replace( 'thebody' );</script>
</p>
<table >
<tr>
<th>Site</th>
<th>Email Address</th>
<th colspan="2">Email Now?</th>
</tr>
<?php
$b = 0;
$q = 1;
while ($row_selfdo = mysql_fetch_assoc($selfdo)) { ?>
<tr>
<td><?php echo $row_seldo[‘sitename’];?></td>
<td><input type="text" name="emailto[]" style="font-size:9px;" size="20" value="<?php echo $row_selfdo['eaddress']; ?>"/></td>
<td valign="middle">Yes:<input type="radio" name="emailnow[<?php echo $b;?>]" value="Yes" <?php if (isset($mailed) && ($mailed=="Not Yet")) { echo ""; } else echo "disabled='disabled'"; ?> /></td>
<td>No:<input name="emailnow[<?php echo $b;?>]" type="radio" value="No" checked="checked" <?php if (isset($mailed) && ($mailed=="Not Yet")) { echo ""; } else echo "disabled='disabled'"; ?>? /></td>
</tr>
<?php $b++; $q++; } ?>
</table>
And here's the script to send the mail
$numb = count($_POST['emailto']);
$num = $numb -1;
$subject=$_POST['subject'];
$thisrecipient = $_POST['emailto'];
$sendtothemnow = $_POST['emailnow'];
for ($a=0;$a<=$num;$a++) {
$emailthemnow = $sendtothemnow[$a];
if ((isset($emailthemnow))&&(($emailthemnow)=="Yes")) {
$recipient = $thisrecipient[$a];
$ToEmail = $recipient;
$EmailSubject = $subject;
$mailheader = 'From: me#mydomain.com'."\r\n";
$mailheader .= 'Reply-To: me#mydomain.com'."\r\n";
$mailheader .= 'MIME-Version: 1.0'."\r\n";
$mailheader .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$MESSAGE_BODY .= '<p>'.$_POST['thebody'].'</p>';
$MESSAGE_BODY .= '<p>Kind Regards</p>';
$MESSAGE_BODY .= '<p>The Environment Team</p>';
$MESSAGE_BODY .= 'email footer bits here ';
$MESSAGE_BODY .='<p style="color:#0C0;">Please consider the environment - do you really need to print this email?';
$MESSAGE_BODY=wordwrap($MESSAGE_BODY,70);
$mailsent= mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Not Sent");
if($mailsent){
//update a table to record date email was sent
}
}//end email send loop
Any suggestions??
Many thanks in advance
On your first line where you use the message body, set it instead of appending it:
$MESSAGE_BODY = '<p>'.$_POST['thebody'].'</p>';
(The dot has been removed)
Only change the variable name which have conflict.
if ((isset($emailthemnow))&&(($emailthemnow)=="Yes")) {
$recipient = $thisrecipient[$a];
$ToEmail = $recipient;
$EmailSubject = $subject;
$mailheader = 'From: me#mydomain.com'."\r\n";
$mailheader .= 'Reply-To: me#mydomain.com'."\r\n";
$mailheader .= 'MIME-Version: 1.0'."\r\n";
$mailheader .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$MESSAGE_BODY .= '<p>'.$_POST['thebody'].'</p>';
$MESSAGE_BODY .= '<p>Kind Regards</p>';
$MESSAGE_BODY .= '<p>The Environment Team</p>';
$MESSAGE_BODY .= 'email footer bits here ';
$MESSAGE_BODY .='<p style="color:#0C0;">Please consider the environment - do you really need to print this email?';
$MESSAGE_BODY_FINAL=wordwrap($MESSAGE_BODY,70);
$mailsent= mail($ToEmail, $EmailSubject, $MESSAGE_BODY_FINAL, $mailheader) or die ("Not Sent");
if($mailsent){
//update a table to record date email was sent
}
}