I have a basic form, with checkboxes, that on submission is to email to the administrator (and a copy to the client).
The code I have echoes only the last checkbox (which therefore only displays one checkbox on the emails) - I can't get it to send all of the checkbox information in the email (even after trying several versions of code from on here).
<form method="POST" name="contactform" action="include/contactstationcode.php">
<h1>Quick Contact Form</h1>
<p>
<section id="nameLabel" class="labelClass"><label id="Name">Full Name: </label><input name="name" id="name" size="30" class="inputField" type="text"><br></section>
<section id="nameLabel" class="labelClass"><label id="Student ID">Student ID: </label><input name="studentid" id="studentid" size="8" class="inputField" type="text"><br></section>
<section id="nameLabel" class="labelClass"><label id="Email">Email: </label><input name="email" id="email" size="30" class="inputField" type="text"><br></section>
<section id="nameLabel" class="labelClass"><label id="ContactNumber">Contact Number: </label><input name="contactnumber" id="contactnumber" size="30" class="inputField" type="text"><br></section>
<section id="nameLabel" class="labelClass"><label id="interests">Interests: <input type="checkbox" name="check_list[]" value="Presenting " checked> Presenting<br><input type="checkbox" name="check_list[]" value="Producing "> Producing<br><input type="checkbox" name="check_list[]" value="Audio Production ">Audio Production<br><input type="checkbox" name="check_list[]" value="Marketing "> Marketing<br><br><input type="checkbox" name="check_list[]" value="Web "> Web<br>
<section id="messageLabel" class="labelClass"><label>Relevant Experience:</label></section>
<section id="messageInput" class="inputClass"><textarea name="experience" id="experience" cols="30" rows="3"></textarea><br></section><br>
<section id="buttonClass" class="buttonClass"><input src="images/submit.png" onmouseover="this.src='images/submit2.png'" onmouseout="this.src='images/submit.png'" alt="submit" value="submit" height="25" type="image" width="70"></section>
</p>
</form>
The PHP Script that I have is:
<?php date_default_timezone_set('Europe/London');
$from = 'From: company#company.com';
$today = date('l, F jS Y.');
$to = 'secret#secret.com';
//Form Fields
$name = $_POST['name'];
$studentid = $_POST['studentid'];
$email = $_POST['email'];
$contactnumber = $_POST['contactnumber'];
$experience = $_POST['experience'];
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
$check=$check.',';
}
}
//Admin Email Body
$subject = 'ClickTeesside.com Get Involved Request';
$bodyp1 = "You have received a Get Involved Request through the ClickTeesside website on $today \n\nFrom: $name ($studentid)\nEmail Address: $email\nContact Number: $contactnumber";
$bodyp2 = "\n\nInterests include: ".$check;
$bodyp3 = "\n\nPrevious Experience: $experience";
$bodyp4 = "\n\nIf suitable, please get in touch with this candidate as soon as possible - the candidate has also received an automated response.";
$body=$bodyp1.$bodyp2.$bodyp3.$bodyp4;
mail ($to, $subject, $body, $from);
//Candidate Email
$candidatesubject = 'ClickTeesside.com Get Involved: Automated Email';
$candidatefrom = 'From: ClickTeesside.com';
$cbody = "Thank you for emailing Click Radio about becoming involved with the station.\nWe've sent your details to our Station Management, who will review your application.\nIf successful, we will contact you in due course.\n\n";
$cbody2 = "Here is a copy of what you sent to us on $today:\n\nName: $name ($studentid)\nEmail Address: $email\nContact Number: $contactnumber\nSpecified Interested Areas: ".$check."\n\nPrevious Experience: $experience";
$candidatebody = $cbody.$cbody2;
mail ($email, $candidatesubject, $candidatebody, $from);
header("Location: http://www.google.co.uk/");
?>
Can't see where i am going wrong - so if you could point me in the right direction :)
Thanks!
The problem is here:
foreach($_POST['check_list'] as $check) {
$check=$check.',';
}
$check is set to the value of the current item on each iteration of the loop. On the last loop, $check will be set to the last item and then you will append ,.
To resolve the issue, use a different variable name in the loop:
$check = "";
foreach($_POST['check_list'] as $c) {
$check .= $c.',';
}
The above preserves more of your code and better illustrates the problem, but the way to do this is with the implode function.
$check = implode(",", $_POST['check_list']);
This will give you the same string without a trailing comma.
Edit this:
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
$check=$check.',';
}
}
with this:
$allChecks = '';
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
$allChecks .= $check.',';
}
}
then send $allChecks by mail:
$bodyp2 = "\n\nInterests include: ".$allChecks;
What you are doing in your foreach is replacing the value each time. use .= to pre pend the next value to your string.
Some other tweaks would be to check all fields are present in both PHP and client side. I tend to use the same file if im writing pure PHP, so an if(isset($_POST)): so the form action is referencing itself.
Good luck.
Related
I have an html from. When the user submits the form, I'm sending an email to the given email address using php. Emails to gmail accounts go through perfectly fine. However, mails to providers like yahoo, aol etc does not go through.There is no filter mechanism in my code to filter out email addresses. Some mails to yahoo mail addresses goes through rarely. But aol doesn't go at all. What is the issue here?
My html code:
<section class="contact-container">
<div class="container mtb">
<div class="row">
<div class="col-md-8 wow fadeInLeft">
<h4>Get in touch</h4>
<hr>
<p>Leave a comment, review, or general musings.
</p><br/>
<form method="post" id="captcha_form" name=
"captcha_form" action="mailform.php"><fieldset><ol>
</li><li><label class="solo" for="email">Email address:</label>
<span class="required">(required)</span><input type="text" class="solo
input" name="email" id="email" value="" />
</li><li><label class="solo" for="name">Name:</label><input type=
"text" class="solo input" name="name" id="name" value="" />
</li><li><label class="solo" for="subject">Subject:</label>
<span class="required">(required)</span> <input type="text" class=
"solo input" name="subject" id="subject" />
</li><li><label class="solo" for="message">Message:</label>
<span class="required">(required)</span>
<div class="solo input"><textarea class="solo input" name="message" id=
"message" ></textarea><br />
<p><input name="submit" id="submit" type="submit" value="Send" style=
"float: right;" /></p></div>
</li></ol>
</fieldset></form> <br /> <br />
</div>
And here is snippets of mailform.php:
<?php
$dontsendemail = 0;
$possiblespam = FALSE;
$strlenmessage = "";
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$subject = $_REQUEST['subject'];
$emailaddress = "email#gmail.com";
/ Check human test input box
if(isset($_REQUEST["htest"]) && $_REQUEST["htest"] != "") die
("Possible spam detected. Please hit your browser back button
and check your entries.");
// Check email address function
function checkemail($field) {
// checks proper syntax
if( !preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*#([a-zA-Z0-9_-])+
([a-zA-Z0- 9._-]+)+$/", $field))
{
die("Improper email address detected. Please hit your browser back
button and enter a proper email address.");
return 1;
}
}
// Spamcheck function
function spamcheck($field) {
if(preg_match("/to:/i",$field) || preg_match("/cc:/i",$field)
|| preg_match("/\r/i",$field) || preg_match("/i\n/i",$field)
|| preg_match("/%0A/i",$field)){
$possiblespam = TRUE;
}else $possiblespam = FALSE;
if ($possiblespam) {
die("Possible spam attempt detected. If this is not the case, please
editthe content of the contact form and try again.");
return 1;
}
}
if ($dontsendemail == 0) {
$message="";
$message.="Name: ".$name."\r\n";
$message.="Mailing Address: \r\nLine 1: ".$addressline1."\r\nLine
2: ".$addressline2."\r\nCity: ".$city."\r\nState: ".$state."
\r\nZip: ".$zip."\r\n";
$message=$message."\r\nMessage:\r\n".$_REQUEST['message'];
mail($emailaddress,"$subject",$message,"From: $email" );
include "email_sent.php";
echo "Thank you, I will respond shortly.";
}
Don't use a regex to confirm email addresses. The real regex is like a day long. Use http://www.w3schools.com/php/filter_validate_email.asp or https://code.google.com/p/php-smtp-email-validation/ or https://github.com/appskitchen/emailverifier/blob/master/class.emailverify.php . You'll notice they are much more complicated than a basic regex. The email spec is insane: https://www.rfc-editor.org/rfc/rfc2822
While I found something similar to this question on here it didn't answer my question outright.
I have set up this php script to validate the form data, which works, after its validated I want it to then pass the info onto another script page to let the user then verify their input data and then mail the data. Its at this state that I'm having trouble. I've spent the last few days trying to find a solution to this and unfortunately coming up short.
<?php
$name_error = '';
$email_error = '';
$comments_error = '';
$error = false;
if (!empty($_POST['submitted']))
{ //if submitted, the validate.
$name = trim($_POST['name']);
if (empty($name))
{
$name_error='Name is required';
$error = true;
}
$email = trim($_POST['email']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
$email_error='E-mail address not valid';
$error = true;
}
$comments = trim($_POST['comments']);
if (empty($comments))
{
$comments_error='Comments are required';
$error = true;
}
if ($error == false)
{
$name_send = $name;
$email_send = $email;
$comments_send = $comments;
/* Redirect visitor to the thank you page */
header('Location: /mail.php');
exit();
}
}
The form this is attached to:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
<label>Your Name</label><br />
<input type="text" name="name" style="width:95%" class="text" value='<?php echo htmlentities($name) ?>' />
<br/>
<span class='error'><?php echo $name_error ?></span>
<br />
<label>Email</label><br />
<input type="email" name="email" style="width:95%" class="text" value='<?php echo htmlentities($email) ?>' />
<br/>
<span class='error'><?php echo $email_error ?></span>
<br />
<label for="comments" style="font-size:16px;">Feedback Comments</label><br />
<textarea name="comments" style="width:95%;" rows="8" value='<?php echo htmlentities($comments) ?>'></textarea>
<br />
<span class='error'><?php echo $comments_error ?></span>
<br />
<input type="checkbox" name="allowCommentPublish" checked="checked" />
<label for="allowCommentPublish" style="font-size:10px;">Allow these comments to be used on our website</label>
<fieldset class="optional">
<h2>[ OPTIONAL ]</h2>
<label>Company Name</label><br />
<input type="text" name="companyName" style="width:95%" class="text" />
<br/>
<label>Phone</label><br />
<input type="text" name="phone" style="width:95%" class="text" /><br/>
<div style="margin:5px 0px;">
<input type="checkbox" name="incmarketing" />
<label style="font-size:10px;"> Yes, you can email me specials and promotions.</label>
<br/>
</div>
</fieldset>
<fieldset>
<input type="submit" name="submitted" value="Send" />
</fieldset>
I will point out im focusing on the main data inputs: Name E-mail and comments.
I need the info from this form to be sent onward but i dont know exactly how to do this and any help will be appreciated greatly.
For passing the values to next page you will have to use either of the three methods.
1. Set cookies with the data.
2. Use global variable session.
3.Pass the data in the url.
For cookies u can set cookies with the values like
setcookie('name',$name);
in ur next page read those cookie data
For sessions:
$_SESSION['name']= $name;
for reading data from cookies & session:
$name = $_COOKIE['name'];
$name = $_SESSION['name'];
For using sessions you must add the line
session_start();
at the start of both the pages that send or receive(use) the data
and for urls
header('Location: /mail.php?name=$name&email=$email&comment=$comments');
Read more on using session
If you need to pass values from one script to another you can use $_SESSION variables. To start a session use: (at the top of the php script)
session_start();
$_SESSION['somename'] = $somevariable;
To access or get that same variable you can use this:
session_start();
$some_other_variable = $_SESSION['somename'];
or you can use hidden input fields.
You can use hidden fields and javascript to submit the form. However as this is the same php page as the original form you will need an if statement
echo '<form name="newForm" action="newpage.php" method="POST">';
echo '<input type="hidden" name="name2" value"' . $name . '">;
echo '<input type="hidden" name="email2" value"' . $email . '">;
echo '<input type="hidden" name="comments2" value"' . $comments . '"></form>;
echo '<script> if (document.getElementById("name2").value != ""){window.onload = function(){ window.document.newForm.submit(); }} </script>';
On a membership site that I am developing, once the user has logged in, on his profile page there are two simple forms.
The first is diplayed only if the user has his "district" field NULL in the database. If the "district" field contains any information, the first form is replaced by a table displaying data from that district. The purpose of the form is for the user to select his district from a list, and update his account so he can read local information from his district. After he hits submit, the "district" field in the databsase is updated and then he never sees this form again.
The second one is a basic contact form, thru which the user can easily send an email message to the support team if he needs any help. Once the message has been sent, instead of the form, a success message is displayed until the refresh of the page, when the text field comes back to normal and the user can send another message.
I have tested them and they both work fine separately, but the error I am encountering is that when a user with NULL in his "district" field logs in, or whenever his page is refreshed, the second form automatically sends a blank email to the support team, and is allways showing the success message instead of the text input for the message to be written in. I have setup an error when the second form is submitted empty, but despite this, while the first form is showing, with every refresh, a new blank email is being sent...
After the user has submitted the first form an updated his "district", the second form returns to normal and works just fine... I know that there is a conflict, or I forgot to setup some conditions in the code below. Please be kind, take a look and tell me where do you think the error might be. Any help is welcomed.
<? if($row_user['district'] == NULL ):
if(!isset($_POST['submit2']))
{
foreach($row_user as $field => $value)
{
$_POST[$field] = $value;
}
}
$error2 = 0;
if(isset($_POST['submit2'])){
if(isset($_POST['distr']) && ($_POST['distr']==""))
{
$error2 = 1;
$msg_distr="<br /><span class='error'>Select your district</span>";
}
if($error2 == 0)
{
$update = 'UPDATE users SET ';
if(isset($_POST['distr'])) $update .= 'district = '.GetSQLValueString($_POST['distr'], 'text').', ';
$update = substr_replace($update,"",-2);
$update .= 'WHERE id_user = '.$id_user;
mysql_query($update, $conn) or die(mysql_error());
}
}
?>
The HTML code for the first form is
<form action="" method="post" enctype="multipart/form-data" >
<table width="465px" border="0" cellpadding="5" cellspacing="5">
<tr>
<td width="160" valign="middle" align="left">
<span class="style20"><b>Choose District</b> *</span>
</td>
<td valign="top" align="left">
<select name="distr" onChange="sel_distr(this.value)" class="select" >
<option value="">Choose</option>
<option value="District1" <? if($_POST['distr']=="District1") echo "'selected'"?>>District1</option>
<option value="District2" <? if($_POST['distr']=="District2") echo "'selected'"?>>District2</option>
<option value="District3" <? if($_POST['distr']=="District3") echo "'selected'"?>>District3</option>
<option value="District4" <? if($_POST['distr']=="District4") echo "'selected'"?>>District4</option>
<option value="District5" <? if($_POST['distr']=="District5") echo "'selected'"?>>District5</option>
</select>
<?=$msg_distr?>
</td>
</tr>
<tr>
<td >
<input type="submit" name="submit2" class="submit" value="Insert" />
</td>
</tr>
</table>
</form>
<?endif; ?>
The other form is for sending email messages from this page, just by inserting the message and sending it. the user will not need to insert his name or email.
<?php
$error_message=0;
if ($_POST["email"]<>'') {
if(isset($_POST['comment']) && ($_POST['comment']=="")){
$error_message=1;
$er_msg="<br /><span class='error'>You can't send a blank message</span>";
}
if($error_message == 0){
$ToEmail = 'mail#domain';
$EmailSubject = 'New message from '.$row_user['name'].'';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "<b>Name:</b> ".$_POST["name"]."<br/>";
$MESSAGE_BODY .= "<b>Email:</b> ".$_POST["email"]."<br/><br/>";
$MESSAGE_BODY .= " ".nl2br($_POST["comment"])."<br/>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
<div class="success" style="height:60px;">
<b><?=$row_user['name']?>, Your message was sent!</b> <br/>
A member of our team will contact you soon.
</div>
<?php
}
if($error_message == 1){ ?>
<form action="me.html" method="post">
<input name="name" type="hidden" value="<?=$row_user['name']?>" id="name" size="32">
<input name="email" type="hidden" value="<?=$row_user['email']?>" id="email" size="32"> <br/>
<textarea name="comment" cols="45" rows="6" id="comment" class="input" style="width:90%"></textarea><br/>
<input class="submit" type="submit" name="Submit" value="Send">
</form>
<?=$er_msg?>
<?
}
} else {
?>
<form action="me.html" method="post">
<input name="name" type="hidden" value="<?=$row_user['name']?>" id="name" size="32">
<input name="email" type="hidden" value="<?=$row_user['email']?>" id="email" size="32"> <br/>
<textarea name="comment" cols="45" rows="6" id="comment" class="input" style="width:90%"></textarea><br/>
<input class="submit" type="submit" name="Submit" value="Send">
</form>
<?php
};
?>
Alright, I think this is a minor mistake and you are getting confused because of the too many error counter flags.
Try changing this:
if($error_message == 0){
to:
if($error_message == 0 && $row_user['district'] != NULL){
This question already has an answer here:
How to validate form in php
(1 answer)
Closed 8 years ago.
Anyone please help me on how to validate a form? I have the code, it is working ok..but I found that after I hit the submit button & php shows the errors, there is an input field, asking the user to answer the math Ques..when I enter the answer, then hit submit button again, it just says mail successfully sent, but other fields are still empty. Any ideas how to fix it. Also, the php value in the answerbox input tags, is not saving the text in box, it disappears. by the way, I got some help from other users, i'm a noob in php, so if you don't mind please explain.
Thanks for your time!
<form name="contact" action="formtest.php" method="post">
<label for="YourName">Your Name:</label>
<input type="text" name="name" class="required" value="<?= isset($_POST['name']) ? $_POST['name'] : '' ?>" />
<label for="YourEmail">Your Email:</label>
<input type="text" name="email" class="required" value="<?= isset($_POST['email']) ? $_POST['email'] : '' ?>"/>
<label for="Subject">Subject:</label>
<input type="text" name="subject" class="required" value="<?= isset($_POST['subject']) ? $_POST['subject'] : '' ?>" />
<label for="YourMessage">Your Message:</label>
<textarea name="message" class="required"><?= isset($_POST['message']) ? $_POST['message'] : '' ?></textarea>
<p class="c3">10 + 5 =<input type="text" name="answerbox" id="answerbox" "<?= isset($_POST['answerbox']) ? $_POST['answerbox'] : '' ?>"/></p>
<fieldset>
<input type="submit" name="submit" id="submit" value="Send" class="required"/>
<input type="reset" id="reset" value="Reset"/>
</fieldset>
<?php
if(isset($_POST['submit'])){
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$subject = trim($_POST["subject"]);
$message = trim($_POST["message"]);
$answerbox = trim($_POST["answerbox"]);
if(empty($_POST['name'])){
print "<div class='formerrors'><li>Please type your name.</li></div>";
}
else {
if (ctype_alpha($name) === false) {
print "<div class='formerrors'><li>Your name only should be in letters!</li></div>";
}
}
if(empty($_POST['email'])){
print "<div class='formerrors'><li>You've forgot to type your email address.</li></div>";
} else{
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
print "<div class='formerrors'><li>Your email is not valid, please check.</li></div>";
}
}
if(empty($_POST['subject'])){
print "<div class='formerrors'><li>Please type a subject.</li></div>";
}
if(empty($_POST['message'])){
print "<div class='formerrors'><li>You've forgot to type your message.</li></div>";
}
if(empty($_POST['answerbox'])){
print "<div class='formerrors'><li>Please answer the math question.</li></div>";
}
else {
if($answerbox != 15){
print"<div class='formerrors'><li>Answer is not correct.</li></div>";
}
else{
$headers = 'From: '.$email. "\r\n" .
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail('me#mymail.me',$subject,$message,$headers);
print "<div class='formerrors'><li>mail succesuffully sent</li></div>";
}
}
}
?>
</form>
Try placing your php code above the html as it is php setting the values of the variables, and you are calling them before they are set.
It is best practice to have php code at the top of the file, and then html below it, or even have the php on a separate file, but include it at the top.
Also I find it better to create an array of errors, and then if the array count is 0, then perform the action (send email) else call the error of arrays within the html, not the php. Then you can choose where to display it on the page, and style it accordingly.
Change your form action to
<?php $PHP_SELF; ?>
So it has to be
<form name="contact" action="<?php $PHP_SELF; ?>" method="post">
For the other issue you must place your code above the form.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Every time i click the submit button the page re-directs.
how can i set it up so that the page does not re-direct and it runs the server side code staying on the current page and echoing above my form?
HTML Code:
<form name="contact_form" method="post" action="mail.php">
<table border="0">
<tbody class="Form">
<tr>
<td>
<label for="NameInput">Your Name:<font color="#FF0000">*</font></label>
<input autofocus="autofocus" placeholder="John Doe" id="NameInput" name="NameInput" required="required" />
</td>
</tr>
<tr>
<td>
<label for="CompanyName">Your Company:<font color="#FF0000">*</font></label>
<input required="required" placeholder="Warner bros." id="CompanyName" name="CompanyName" />
</td>
</tr>
<tr>
<td>
<label id="PhoneNumber" name="PhoneNumber">Phone: </label>
<input placeholder="888-888-8888" id="PhoneNumber" name="PhoneNumber"/>
</td>
</tr>
<tr>
<td>
<label id="Email" name="Email">Email:<font color="#FF0000">*</font></label>
<input required="required" placeholder="johndoe#gmail.com" id="Email" name="Email" type="email" />
</td>
</tr>
<tr>
<td>
<label for="Website">Website: </label>
<input placeholder="https://" id="Website" name="Website" />
</td>
</tr>
</tbody>
</table>
<br/>
<br/>
<br/>
<br/>
<p><strong>What interests you, broadly speaking?</strong></p>
<br/>
<p><input id="WebDesignCheckbox" name="WebDesignCheckbox" type="checkbox">Web Design/Development</input></p>
<input id="SocialMediaCheckbox" name="SocialMediaCheckbox" type="checkbox">Social Media</input>
>
<input id="MobilePresence" name="MobilePresence" type="checkbox">Mobile Presence</input>
<input id="OnlineAdvertising" name="OnlineAdvertising" type="checkbox">Online Advertising</input>
<input id="SEOCheckbox" name="SEOCheckbox" type="checkbox">Search Engine Optamization</input>
<p><input id="ecommerceCheckbox" name="ecommerceCheckbox" type="checkbox" >eCommerce</input></p>
<br/>
<label for="comments">Your Ideas to Life?</label><br/>
<textarea placeholder="How can we help you?" id="comments" name="comments" style="margin: 2px; height: 137px; width: 437px;"></textarea></td></tr><br/>
<tr>
<td><input type="submit" id="Submit" name="Submit" value="Submit" align="middle" /></td>
</tr>
<p id="req-field-desc"><span class="required">*</span> indicates a required field</p>
</form>
PHP Script mail.php:
<?php
if( isset($_POST) ){
//form validation vars
$formok = true;
$errors = array();
//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');
//form data
$NameInput = $_POST['NameInput'];
$CompanyName = $_POST['CompanyName'];
$PhoneNumber = $_POST['PhoneNumber'];
$Email = $_POST['Email'];
$Website = $_POST['Website'];
$WebDesignCheckbox = $_POST['WebDesignCheckbox'];
$SocialMediaCheckbox = $_POST['SocialMediaCheckbox'];
$MobilePresence = $_POST['MobilePresence'];
$OnlineAdvertising = $_POST['OnlineAdvertising'];
$SEOCheckbox = $_POST['SEOCheckbox'];
$ecommerceCheckbox = $_POST['ecommerceCheckbox'];
$comments = $_POST['comments'];
//form validation
if(empty($NameInput)){
$formok - false;
$errors[] = "You have not entered a name.";
}
if(empty($CompanyName)){
$formok = false;
$errors[] = "You have not entered a company name.";
}
if(empty($Email)){
$formok = false;
$errors[] = "You have not entered an email address.";
}
//send email if checks out
if($formok){
ini_set("sendmail_from","keeano#doodleinc.co");
/*$headers="From: Contact Page: {$Email}" . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";*/
$emailbody = "
<p>Time: {$time} \n {$date}</p>
<p>You are recieving this from your websites contact form.</p>
<p><strong>Contacts Name: </strong> {$NameInput}</p>
<p><strong>Companys Name: </strong> {$CompanyName}</p>
<p><strong>Phone Number: </strong> {$PhoneNumber}</p>
<p><strong>Email: </strong> {$Email}</p>
<p><strong>Website: </strong> {$Website}</p>
<p><strong>Web Design Checkbox: </strong> {$WebDesignCheckbox}</p>
<p><strong>Social Media Checkbox: </strong> {$SocialMediaCheckbox}</p>
<p><strong>Mobile Presence: </strong> {$MobilePresence}</p>
<p><strong>Online Advertising: </strong> {$OnlineAdvertising}</p>
<p><strong>SEO Checkbox: </strong> {$SEOCheckbox}</p>
<p><strong>eCommerce Checkbox: </strong> {$ecommerceCheckbox}</p>
<p><strong>Comments: </strong> {$comments}</p>
<p><strong>Extra Data Obtained:</strong>\n Users IPAddress: {$ipaddress}</p>
";
mail("keeano#doodleinc.co", "Contacts Page Enquiry", $emailbody);
echo('<p style="font-weight: bold; color: #709900"><em>Thank you for contacting doodle Inc., someone will contact you within 48 hours.</em></p>');
}
}
I would simply like to get this to create a label at the top of the Form saying "Thank you, someone will be in touch with you within 48 hours."
If someone can help me that would be great as well as detail the explanation so i never run into this problem again.
The thing is you have mail.php as your action for the form tag.
you could just leave it blank, then the form will post to the same page (contact.php)
To get the functionality working on that page you could include your mail.php file before the form, so that the errors are displayed on that same page.
just a require("mail.php"); at the top of the code will do.
You could look at doing a javascript event on the submit button. Then use json and ajax to process your form etc, return a success/fail message, and insert into where the form was. This will not 'redirect' the user to a different page, the form will get replaced by a success message.
<form method="get" action="" onsubmit="jsquickcomp(this); return false;" > // or something like this for your form heading.
Then you js function....
function jsquicksub(po_form) {
if (gb_ajaxbusy)
return false;
gb_ajaxbusy = true;
var lo_request = {};
lo_request.n = $(po_form).find('#sub-firstname').val();
lo_request.e = $(po_form).find('#sub-email').val();
// etc etc
$(po_form).fadeTo(200, 0.3);
$.getJSON('/mail.php', lo_request, function(po_result) { // this is your mail.php which performs all the php side of the form stuff
gb_ajaxbusy = false;
$(po_form).fadeTo(0, 1);
if (!po_result.success) {
alert(po_result.content); // has an alert box for error message. Can change it for anything
return false;
}else{
setTimeout(function() {
$.modal.close();
}, 2500);
}
$(po_form).replaceWith(po_result.content); // replaces the form with the success message
});
}
Then in your mail.php
// function gets passed values after validation and returns a message to the user on screen
function jsonreturn($pb_success, $ps_content = null) {
$lo_result = (object) array('success'=>$pb_success, 'content'=>$ps_content);
ob_clean();
echo json_encode($lo_result);
exit;
} // end jsonreturn()
// use $_GET to get all the values you have send through from your js file
// validate the data and do whatever you want to do with it
// these return the message that will replace the form
jsonreturn(true, 'Success!'); // for success
jsonreturn(false, 'Fail'); // for fail
Hope that makes some sense?