jquery submit contact form - php

I'm trying to hide the form and show the success alert after the mail is mailed.
But everything I do, is ignored by the code, I put header before the mail function, and the mail still sends, it should redirect before the mail sends, tried to break the code before the mail function, and the break functions works.
at list the mail doesn't send.
contact.php:
<div id="innerWrapper">
<h1 class="pageheading"><?=translate('contact_us') ?></h1>
<form class="form-horizontal" action="/ajax/contact.php" method="POST" id="contactForm">
<?php $email = $db->get_row("SELECT email,full_name FROM users WHERE userID = 1");?>
<label><?=translate('your_name') ?>:</label>
<input type="text" name="name" <?php if($usersObject->ID()) {echo 'value = "'. $email->full_name. '"';} else {echo 'placeholder = "First / Last"';}?> class="required input-xlarge"/>
<br/><br/>
<label><?=translate('email') ?>:</label>
<input type="email" name="email" <?php if($usersObject->ID()) {echo 'value = "'. $email->email . '"';} else {echo 'placeholder = "email#example.com"';}?> class="required input-xlarge"/>
<br/><br/>
<label>Subject:</label>
<input type="text" name="<?=translate('subject') ?>" placeholder="Hi" class="required input-xlarge"/>
<br/><br/>
<label><?=translate('message') ?>:</label>
<textarea name="message_to_us" class="input-xlarge required" rows="5" placeholder=""></textarea>
<br/><br/>
<input class="btn btn-large btn-warning" value="<?=translate('contact_btn') ?>" type="submit" name="sbc"/>
</form>
</div>
ajax/contact.php :
if(isset($_POST['sbc'])) {
foreach($_POST as $K=>$V) $_POST[$K] = trim(strip_tags($V));
extract($_POST);
foreach($_POST as $k=>$v) {
if(trim($v) == "")
{
print '<div class="alert alert-error">All fields are required</div>';
exit;
}
}
$body = 'New contact form message on ' . $_SERVER['SERVER_NAME'];
$body .= '<br/>';
$body .= 'Subject : ' . $Subject;
$body .= '<br/>';
$body .= 'Name : '.$name .' / Email : ' . $email;
$body .= '<br/>';
$body .= $message_to_us;
$headers = "From: contact#" . str_replace("www.", "", $_SERVER['SERVER_NAME']) . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
mail(PAYPAL_EMAIL, str_replace("www.", '', $_SERVER["SERVER_NAME"]), $body, $headers);
?>
<script type="text/javascript">
$("#sbc").click(function() {
$("#contactForm").hide('slow');
});
</script>
<?php
print '<div class="alert alert-success">'.translate('thankyou_contact').'</div>';
}else{
print 'Emtpy request';
}
edit:
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
header('Location: join');
mail(PAYPAL_EMAIL, str_replace("www.", '', $_SERVER["SERVER_NAME"]), $body, $headers);
tried to redirect before the mail just to check, but it just ignores the header and still mail the mail.
edit2 :
<script type="text/javascript">
$(document).ready (function() {
$("#sbc").click(function() {
$("#contactForm").hide('slow');
});
});
</script>
not working.

add id= sbc to
<input class="btn btn-large btn-warning" value="<?=translate('contact_btn') ?>" type="submit" name="sbc" id="sbc"/>

Related

PHP - Email form is sending over 50 mails after form submit

I have a problem that after I fill out the contact form on my HTML website I receive over 50 same E-mails. Its a HTML form connected to contact.php file which code is shown bellow. I have set everything but maybe there is a problem in my code or somewhere else.
My code is over here
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+ (ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$verify = $_POST['verify'];
if(trim($name) == '') {
echo '<div class="error_message">Vyplnte meno.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="error_message">Vyplnte email.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="error_message">Zadali ste nesprávny e-mail, skúste to znovu.</div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="error_message">Vyplnte text správy.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "joe.doe#yourdomain.com";
$address = "noreply#marcelaskolenia.sk";
$address = "lubosmasura#gmail.com";
$toCustomer = $email;
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
$e_subject = 'Mate novu spravu od ' . $name . '.';
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
$e_body = "Mate novu spravu od $name." . PHP_EOL . PHP_EOL;
$e_content = "\"$subject\"" . "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "Kontaktujte $name cez email, $email alebo cez mobil $phone";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers .= 'To: Test <noreply#marcelaskolenia.sk>' . "\r\n";
$headers .= 'From: Testk <noreply#marcelaskolenia.sk>' . "\r\n";
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h3 class'mark'>Sprava bola odoslana.</h3>";
echo "<p>Dakujeme <strong>$name</strong>, Vasa sprava nam bude dorucena.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
HTML code
<div class="contact_form">
<div id="message"></div>
<form id="contactform" class="row" action="contact.php" name="contactform" method="post">
<div class="col-md-12">
<input type="text" name="name" id="name" class="form-control" placeholder="Meno">
<input type="text" name="email" id="email" class="form-control" placeholder="Email">
<input type="text" name="phone" id="phone" class="form-control" placeholder="Telefónne číslo">
<input type="text" name="subject" id="subject" class="form-control" placeholder="Predmet">
<textarea class="form-control" name="comments" id="comments" rows="6" placeholder="Text správy"></textarea>
<button type="submit" value="SEND" id="submit" class="btn btn-primary"> ODOSLAŤ</button>
</div>
</form>
</div>
</div><!-- end col -->
Any Ideas why is this happening?
Thank you.

Email field is empty on PhpMail

I have a simple form which sending an email with some fields and also it saves the fields in .csv file.
Here is my code (HTML, jQuery, PHP)
The Form:
<form id="request-size">
<input name="email" id="email" type="email" class="cmesgmail" placeholder="email">
<input type="hidden" name="size" id="size" value="52">
<input type="hidden" name="sku" id="sku" value="315">
<input type="hidden" name="pridpr" id="pridpr" value="849">
<input class="request-size-btn btn" value="SEND">
</form>
The jQuery:
jQuery('.request-size-btn').live('click', function() {
jQuery.ajax({
type: "POST",
url: "https://example.com/sendmail.php",
data: jQuery('form#request-size').serializeArray(),
success: function( data ) {
jQuery('.cmesg').empty();
jQuery('.cmesg').append(data);
}
});
});
});
The PHP Sendmail:
$EmailFrom = $_REQUEST['email'];
$SKU = $_REQUEST['sku'];
$SIZE = $_REQUEST['size'];
$ID = $_REQUEST['pridpr'];
$EmailTo = "info#example.com";
$Subject = "Contact for Size";
$DATENOW = date('d/m/Y h:i:s a', time());
$Body = "";
$Body .= "<b>Email:</b> ";
$Body .= $EmailFrom . "\r\n";
$Body .= "<br>";
$Body .= "<b>Product SKU:</b> ";
$Body .= $SKU . "\n";
$Body .= "<br>";
$Body .= "<b>Product ID:</b> ";
$Body .= $ID . "\n";
$Body .= "<br>";
$Body .= "<b>Product Size:</b> ";
$Body .= $SIZE;
$Body .= "<br>";
$Body .= "<b>Date:</b> ";
$Body .= $DATENOW;
$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\n";
$headers .= "From: ". $EmailFrom ."" . "\n";
$success = mail($EmailTo, $Subject, $Body, $headers);
if ($success){
echo '<div class="alert alert-success">Done!</div>';
$file = fopen('request-size.csv', 'a');
$data = array(
array($EmailFrom, $SKU, $ID, $SIZE, $DATENOW)
);
foreach ($data as $row)
{
fputcsv($file, $row);
}
fclose($file);
}
else{
echo '<div class="alert alert-danger">Error!</div>';
}
I've got all the fields in my csv file and email, except the "email" field. It's always empty. I have also try to change the name of email field (email to emailreq) on both sides (html, php), but didn't work.
Any suggestions? Thank you!
If updating jQuery is an option for you, do that. Version 1.6.2 and up should be working (possibly also before, but i'm not sure).
Otherwise change the
<input name="email" id="email" type="email" class="cmesgmail" placeholder="email">
to
<input name="email" id="email" type="text" class="cmesgmail" placeholder="email">
Older versions of jQuery did not include support for the HTML5 input types.

html showing in php mail

I'm trying to send a php mail but it seems that I have a error in my foreach because the mail shows html..
This is my code:
<form method="post">
<fieldset>
<legend>Contact Form</legend>
<label for="fullname">Votre Nom :
<input id="fullname" name="fullname" type="text" value="nelson" />
</label>
<label for="emailaddress" class="margin">Votre e-mail:
<input id="email" name="email" type="text" value="" />
</label>
<label for="message">Message:<br />
<textarea id="message" name="message" cols="40" rows="8"></textarea>
</label>
<p>
<input id="submit-button" class="button gray stripe" type="submit" name="submit" value="Envoyer le message" />
</p>
</fieldset>
</form>
<?php
foreach ($_POST as $value) {
$value = strip_tags($value);
$value = htmlspecialchars($value);
}
$name = $_POST["fullname"];
$email = "email:" .$_POST["email"];
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .$_POST["message"];
$to="email#hotmail.com";
$suject="site internet";
if (isset($_POST['submit'])) {
mail($to, $suject, $message);
echo"mail had been sent";
}
?>
Can anyone help me please
You need to set the Content-type header in your email message:
$name = $_POST["fullname"];
$email = "email:" .$_POST["email"];
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .$_POST["message"];
$to="email#hotmail.com";
$suject="site internet";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if (isset($_POST['submit'])) {
mail($to, $suject, $message, $headers);
echo"mail had been sent";
Your message body also needs to be contained in <html> tags.
Your foreach is kind of useless, just do that. More fast than a loops
$name = strip_tags(htmlspecialchars($_POST["fullname"]));
$email = "email:" .strip_tags(htmlspecialchars($_POST["email"]));
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .strip_tags(htmlspecialchars($_POST["message";));
To send email containing HTML you must set the header so that the email client knows that the email contains HTML. You also have to make the body of your email an HTML document.
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset: utf8\r\n";
mail($to, $suject, $message, $header);
And then in the message itself:
<html>
<head></head>
<body>
Content here
</body>
</html>

PHP attachment in contact form

I have a form. With the following:
HTML:
<form name="feedback_form" method="post" enctype="multipart/form-data" action="" class="feedback_form">
<input type="text" name="field-name" value="İsim" title="İsim" class="field-name form_field">
<input type="text" name="field-email" value="Email" title="Email" class="field-email form_field">
<input type="text" name="field-subject" value="Başlık" title="Başlık" class="field-subject form_field">
<textarea name="field-message" cols="45" rows="5" title="Mesajınız..." class="field-message form_field">Mesajınız...</textarea>
<label for='uploaded_file'>Fotoğrafınızı Yükleyin:</label>
<input type="file" name="field-file" value="File">
<br>
<input type="reset" name="reset" id="reset2" value="Temizle" class="feedback_reset">
<input type="button" name="submit" class="feedback_go" id="submit2" value="Gönder">
</form>
PHP:
<?php
header('Content-Type: text/html; charset=utf-8');
function sendFeedback($feedback_email, $feedback_msg, $feedback_name, $feedback_subject, $feedback_file) {
/* EDIT THIS */
$admin_email = "mymail#gmail.com";
if ($feedback_subject == "Subject" || empty($feedback_subject) ) {
$subj = "Email from your site";
} else {
$subj = $feedback_subject;
}
/* //EDIT THIS */
$message = "
<html>
<head>
<title>Websitenizin emaili</title>
</head>
<body>
<p><a href='mailto:".$feedback_email."'>".$feedback_name."</a> send this message:</p>
<p>".$feedback_msg."</p>
<p>".$subject."</p>
</body>
</html>
";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
if ($feedback_name!=="Name" && $feedback_email!=="Email" && !empty($feedback_email) && !empty($feedback_msg) && !empty($feedback_name) ) {
if ($feedback_email == "mail_error") {
echo "<span class='ajaxok'>Geçersiz email adresi.</span>";
} else {
mail($admin_email, $subj, $message, $headers);
echo "<span class='ajaxok'>Teşekkürler. Mesajınız gönderilmiştir.</span>";
}
} else {
echo "<span class='ajaxalert'>Lütfen zorunlu alanları doldurunuz.</span>";
}
}
sendFeedback($_POST['email'], $_POST['message'], $_POST['name'], $_POST['subject'], $_FILES['file']);
?>
When send a message on this form, send email, working. But only subject, message, name and email. I want add image upload in this php code. But i don't know how can i do it? Please help me.
Look here: http://pastebin.com/nAErtHgt
I write this for Mail is sent without the attachment but it seems was not able to use it.
Add this function to your PHP file
function addattachment($file){
$fname = substr(strrchr($file, "/"), 1);
$data = file_get_contents($file);
$i = count($this->parts);
$content_id = "part$i." . sprintf("%09d", crc32($fname)) . strrchr($this->to_address, "#");
$this->parts[$i] = "Content-Type: ".mime_content_type($file)."; name=\"$fname\"\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-ID: <$content_id>\r\n" .
"Content-Disposition: inline;\n" .
" filename=\"$fname\"\r\n" .
"\n" .
chunk_split( base64_encode($data), 68, "\n");
return $content_id;
}
and between $ message and $headers add
$message .= addattachment($feedback_file);
and should work.
Let me know if is working for you.
I don't recommend using PHP's mail function, use something like PHPMailer instead, it is a reasonable class for sending mails with multipart, attachments etc.
http://sourceforge.net/projects/phpmailer/ (Should be a lot of alternatives to this, but it works for me)
Then simply add the uploaded tmpfile as an attachment.

Hide web page if no variable present in the url

I have created a form that pulls a variable from a url using the following code:
<?php $id = $_GET['id']; echo $id; ?>
Using this is link as an example (http://www.mydomain.co.uk/customerform.php?id=12) it would display 12 on the webpage.
What I would like to know, is it possible to stop the web page from loading if there is no varible in the URL e.g ?id=12 ?
Either in php, javascript?
So If you visited the following link, the web page would display:
http://www.mydomain.co.uk/customerform.php?id=12
and if you visited this link (without the variable), the page would not display:
http://www.mydomain.co.uk/customerform.php
Thank you for any help.
Update below...
I have added Krishnanunni's code and it successfully prevents the web page from displaying if the is no vartiable in the url. But adding this code prevents the form submitting...
Is there a way of keeping the url variable when the form is submitted?
Can anyone help? Thanks
<body id="main_body" >
<div id="form_container">
<?
/* only load page is variable is presnt in the URL */
if (!isset($_GET['id']) || empty($_GET['id']))
{
die("Bad Request");
}
?>
<?
/* Mailer with Attachments */
$action = $_REQUEST['action'];
global $action;
function showForm() {
?>
<form id="contact" class="appnitro" name="idform" enctype="multipart/form-data" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="hidden" name="action" value="send" />
<input type="hidden" name="MAX_FILE_SIZE" value="10000000"/>
<input name="to_email" type="hidden" id="to_email" value="myemail#domain.co.uk"/>
<div>
<input id="invoice" name="invoice" class="element text medium" type="hidden" maxlength="255" value="<?php $id = $_GET['id']; echo $id; ?>"/>
</div>
<div>
<input id="subject" name="subject" class="element text medium" type="hidden" maxlength="255" value="Enquiry Form - (Invoice: <?php $id = $_GET['id']; echo $id; ?>)"/>
</div>
<label class="description" for="from_name">Name </label>
<div>
<input id="from_name" name="from_name" class="element text medium" type="text" maxlength="255" value=""/>
</div>
<label class="description" for="position">Position </label>
<div>
<input id="position" name="position" class="element text medium" type="text" maxlength="255" value=""/>
</div>
<label class="description" for="uemail"> Email </label>
<div>
<input id="email" name="email" class="element text medium" type="text" maxlength="255" value=""/>
</div>
<label class="description" for="phone"> Phone </label>
<div>
<input id="phone" name="phone" class="element text medium" type="text" maxlength="255" value=""/>
</div>
<input name="Reset" type="reset" class="contactform_small" value="Reset" />
<input name="Submit2" type="submit" class="contactform_small" id="Submit" value="Submit" />
</form>
<?
}
function sendMail() {
if (!isset ($_POST['to_email'])) { //Oops, forgot your email addy!
die ("<p>Oops! You forgot to fill out the email address! Click on the back arrow to go back</p>");
}
else {
$to_name = "MY BUSINESS";
$from_name = stripslashes($_POST['from_name']);
$subject = Trim(stripslashes($_POST['subject']));
$to_email = $_POST['to_email'];
$EmailFrom = Trim(stripslashes($_POST['email']));
// get posted data into local variables
$position = Trim(stripslashes($_POST['position']));
$position2 = Trim(stripslashes($_POST['position2']));
$phone = Trim(stripslashes($_POST['phone']));
$invoice = Trim(stripslashes($_POST['invoice']));
//Let's start our headers
$headers = "From: $EmailFrom<" . $_POST['email'] . ">\n";
$headers .= "Reply-To: <" . $_POST['email'] . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: $from_name<" . $_POST['email'] . ">\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
$headers .= "Return-Path: <" . $_POST['email'] . ">\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
$message = "------=MIME_BOUNDRY_message_parts\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
/* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
$message .= "Invoice: ";
$message .= $invoice;
$message .= "\n";
$message .= "\n";
$message .= "My Business Enquiry Form";
$message .= "\n";
$message .= "\n";
$message .= "Contact Details: ";
$message .= "\n";
$message .= "\n";
$message .= "Name: ";
$message .= $from_name;
$message .= "\n";
$message .= "Position: ";
$message .= $position;
$message .= "\n";
$message .= "Email: ";
$message .= $EmailFrom;
$message .= "\n";
$message .= "Phone: ";
$message .= $phone;
$message .= "\n";
$message .= "\n";
// send the message
mail("$to_name<$to_email>", $subject, $message, $headers);
print "Form sent successfully";
}
}
?>
<?
switch ($action) {
case "send":
sendMail();
showForm();
break;
default:
showForm();
}
?>
<div id="footer">
</div>
</div>
</body>
Start your php page with this code
if (!isset($_GET['id']) || empty($_GET['id']))
{
die("Bad Request");
}
This should solve your problem

Categories