Form validation - required fields in php form - php

I've found a form that works well with multiple file uploads and am trying to work in some validation, i'm just trying to make some fields required for now.
Currently some validation is occuring with error messages however the form always posts regardless. My php knowledge is limited and it may look messy but I would be grateful if anybody could help me as to how to amend the following code..
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
// we'll begin by assigning the To address and message subject
$to="paul#thedesignbank.co.uk";
$subject="Parts Request";
$subject = trim($_POST['namefrom']);
$vehiclereg = trim($_POST['vehiclereg']);
$chassisnumber = trim($_POST['chassisnumber']);
$contactnumber = trim($_POST['contactnumber']);
$emailfrom = trim($_POST['emailfrom']);
$address = trim($_POST['address']);
$town = trim($_POST['town']);
$postcode = trim($_POST['postcode']);
$comments = trim($_POST['comments']);
// get the sender's name and email address
// we'll just plug them a variable to be used later$from = stripslashes($_POST['fromname'])." <".stripslashes($_POST['fromemail']).">";
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
// here, we'll start the message body.
// this is the text that will be displayed
// in the e-mail
$message.="From: ".$subject."\n";
$message.="Email: ".$emailfrom."\n";
$message.="Vehicle Registration: ".$vehiclereg."\n";
$message.="Chassis Number: ".$chassisnumber."\n";
$message.="Address: ".$address."\n";
$message.="Town: ".$town."\n";
$message.="Postcode: ".$postcode."\n";
$message.="Contact Number: ".$contactnumber."\n";
$message.="Comment: ".$comments."\n\n";
//my validation gives a message but email still posts
$errors = array();
if (!$_POST['namefrom'])
// if not, add that error to our array
$errors[] = "Name is required";
// check to see if a subject was entered
if (!$_POST['emailfrom'])
// if not, add that error to our array
$errors[] = "Email Address is required";
if (!$_POST['contactnumber'])
// if not, add that error to our array
$errors[] = "Contact Number is required";
// check to see if a message was entered
if (!$_POST['vehiclereg'])
// if not, add that error to our array
$errors[] = "vehiclereg is required";
// if there are any errors, display them
if (count($errors)>0){
echo "<strong>ERROR:<br>\n";
foreach($errors as $err)
echo "$err<br>\n";
} else {
}
// next, we'll build the invisible portion of the message body
// note that we insert two dashes in front of the MIME boundary
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// now we'll process our uploaded files
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
// if the upload succeded, the file will exist
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
if (#mail($to, $subject, $message, $headers))
echo "Message Sent Successfully";
else
echo "Failed to send";
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data" name="form1">
<h3>Vehicle Details</h3>
<p style="margin:17px 0"> <label style=" font-family: Helvetica, Arial, sans-serif;">Vehicle Registration: <span style="color:red">*</span></label>
<input class="form-input" name="vehiclereg" id="vehiclereg" type="text" tabindex="1" style="width:100%;"/></p>
<p style="margin:17px 0"> <label style=" font-family: Helvetica, Arial, sans-serif;">VIN/Chassis Number (Last 6 Digits):</label>
<input class="form-input" name="chassisnumber" id="chassisnumber" type="text" tabindex="1" style="width:100%;"/></p>
<h3>Customer Details</h3>
<p style="margin:17px 0"> <label style=" font-family: Helvetica, Arial, sans-serif;">Name: <span style="color:red">*</span></label>
<input class="form-input" name="namefrom" id="namefrom" type="text" tabindex="1" style="width:100%;"/></p>
<!--changed these-->
<p style="margin:17px 0"><label style=" font-family: Helvetica, Arial, sans-serif;">Address:</label>
<input class="form-input" name="address" id="address" type="text" tabindex="1" style="width:100%;"/></p>
<p style="margin:17px 0"> <label style=" font-family: Helvetica, Arial, sans-serif;">Town:</label>
<input class="form-input" name="town" id="town" type="text" tabindex="1" style="width:100%;"/></p>
<p style="margin:17px 0"> <label style=" font-family: Helvetica, Arial, sans-serif;">Postcode:</label>
<input class="form-input" name="postcode" id="postcode" type="text" tabindex="1" style="width:100%;"/></p>
<!--changed these-->
<p style="margin:17px 0"> <label style=" font-family: Helvetica, Arial, sans-serif;">Email: <span style="color:red">*</span></label>
<input class="form-input" name="emailfrom" id="emailfrom" type="text" tabindex="3" style="width:100%;"/></p>
<p style="margin:17px 0"> <label style="margin-right: 4px; font-family: Helvetica, Arial, sans-serif;">Contact No: <span style="color:red">*</span></label>
<input type="text" class="form-input" name="contactnumber" id="contactnumber" rows="2" cols="22" tabindex="6" style="width:100%;" /></p>
<p style="margin:17px 0"> <label style="margin-right: 4px; vertical-align:top; font-family: Helvetica, Arial, sans-serif; ">Brief Description of Request: </label>
<textarea class="form-input" name="comments" id="comments" rows="7" cols="22" tabindex="6" style="height:50px; width:100%;" ></textarea></p>
<p style="margin:17px 0">
<label style="margin-right: 4px; font-family: Helvetica, Arial, sans-serif;">Vehicle image/doc one:</label>
<input type="file" name="file1">
</p>
<p style="margin:17px 0">
<label style="margin-right: 4px; font-family: Helvetica, Arial, sans-serif;">Vehicle image/doc two:</label>
<input type="file" name="file2">
</p>
<p style="margin:17px 0">
<label style="margin-right: 4px; font-family: Helvetica, Arial, sans-serif;">Vehicle image/doc three:</label>
<input type="file" name="file3">
</p>
<div style="clear:both">
<p> I would like to subscribe to the newsletter
<input name="subscribe" type="checkbox" value="website_subscribers" name="mailinglist[]" checked="checked" /></p>
</div>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
<?php } ?>

Using if (!$_POST['emailfrom']) will only check if the posted field exists. Regardless if it's empty or not.
Instead you could use empty:
if (empty($_POST['emailfrom'])) {
echo 'error'
}
You also can add some additional checks to the fields like a valid email check or phonenumber validator:
if (empty($_POST['emailfrom']) || !isValidEmailCustomFunction($_POST['emailfrom]')) {
echo 'error'
}

something like
if(!isset($_POST['namefrom']) || $_POST['namefrom']=="" ){
$error[]="Please enter your name";
}

Related

Form Submission code check

The Contact page which has form submission, https://photos.app.goo.gl/GpbWvks2Y3SjwmX58 is not being executed. I tried various solutions from other questions that's already been asked but had no luck.
The .php file is in the Contacts folder along with the Contacts/index.html page.
Some expert help would be much appreciated.
Here's the Javascript on the HTML page.
<script type="text/javascript">document.getElementById('spc').value = '755fd9ccedf916b2cd08bb7be88691dd';</script>
<form name="enquiry" method="post" action="infosemantic-enquiryform1.php" class="form" id="form">
<p class="input_field">
<label class="field">First Name:</label>
<input name="name" type="text" class="name_box" id="name" value="" size="35">
</p>
<p class="input_field">
<label class="field">Last Name:</label>
<input name="lastname" type="text" class="name_box" id="lastname" value="" size="35">
</p>
<p class="input_field">
<label class="field">Title:</label>
<input name="title" type="text" class="name_box" id="title" value="" size="35">
</p>
<p class="input_field">
<label class="field">Company:</label>
<input value="" name="company" class="name_box" size="35" type="text">
</p>
<p class="input_field">
<label class="field">E-mail Address:</label>
<input name="email" type="text" class="name_box" id="email" value="" size="35">
</p>
<p class="input_field">
<label class="field">Message:</label>
<textarea name="Message" id="csinbr" cols="45" rows="5" class="name_box2" w></textarea>
</p>
<p class="btn"><input name="Subject" value="Enquiry from DSR Power" type="hidden">
<input name="Submit" value="Submit " class="sbm" type="submit">
<input name="Reset" value="Clear Form" class="sbm" type="reset">
</p>
<font face="Verdana" size="2">
<font face="Verdana" size="2">
<input name="form_name" value="rajforever2" type="hidden">
</font>
<input name="userid" value="webindia" type="hidden">
</font>
</form>
</div>
$message = "<html><body><table align='center' boarder='1' cellpadding='5' cellspacing='2' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold;background-color:#CCCCFF;color:#000000;;border:double'>";
$message .= "<tr><td align='left'><b> Name </b></td><td>:</td><td>".$_POST['name']."</td></tr>";
$message .= "<tr><td align='left'><b>Last Name </b></td><td>:</td><td>".$_POST['lastname']."</td></tr>";
$message .= "<tr><td align='left'><b>Title </b></td><td>:</td><td>".$_POST['title']."</td></tr>";
$message .= "<tr><td align='left'><b>Company </b></td><td>:</td><td>".$_POST['company']."</td></tr>";
$message .= "<tr><td align='left'><b>Email </b></td><td>:</td><td>".$_POST['email']."</td></tr>";
$message .= "<tr><td align='left'><b>Message</b></td><td>:</td><td>".$_POST['Message']."</td></tr>";
$message .= "</table></body></html>";
/*to avoid spam mails in contact form*/
// Select if you want to check form for standard spam text
$SpamCheck = "Y"; // Y or N
$SpamReplaceText = "*content removed*";
// Error message prited if spam form attack found
$SpamErrorMessage = "<p align=\"center\"><font color=\"red\">Malicious code content detected.</font><br><b>Your IP Number of <b>".
getenv("REMOTE_ADDR").
"</b> has been logged.</b></p>";
$name = $_POST['name'];
$email = $_POST['email'];
$msg = $_POST['comments'];
if ($SpamCheck == "Y") {
// Check for Website URL's in the form input boxes as if we block website URLs from the form,
// then this will stop the spammers wastignt ime sending emails
if (preg_match("/http/i", "$name")) {
echo "$SpamErrorMessage";
exit();
}
if (preg_match("/http/i", "$email")) {
echo "$SpamErrorMessage";
exit();
}
if (preg_match("/http/i", "$msg")) {
echo "$SpamErrorMessage";
exit();
}
// Patterm match search to strip out the invalid charcaters, this prevents the mail injection spammer
$pattern = '/(;|\||`|>|<|&|^|"|'."\n|\r|'".'|{|}|[|]|\)|\()/i'; // build the pattern match string
$name = preg_replace($pattern, "", $name);
$email = preg_replace($pattern, "", $email);
$msg = preg_replace($pattern, "", $msg);
// Check for the injected headers from the spammer attempt
// This will replace the injection attempt text with the string you have set in the above config section
$find = ["/bcc\:/i", "/Content\-Type\:/i", "/cc\:/i", "/to\:/i"];
$email = preg_replace($find, "$SpamReplaceText", $email);
$name = preg_replace($find, "$SpamReplaceText", $name);
$msg = preg_replace($find, "$SpamReplaceText", $msg);
// Check to see if the fields contain any content we want to ban
if (stristr($name, $SpamReplaceText) !== false) {
echo "$SpamErrorMessage";
exit();
}
if (stristr($msg, $SpamReplaceText) !== false) {
echo "$SpamErrorMessage";
exit();
}
// Do a check on the send email and subject text
if (stristr($to, $SpamReplaceText) !== false) {
echo "$SpamErrorMessage";
exit();
}
if (stristr($subject, $SpamReplaceText) !== false) {
echo "$SpamErrorMessage";
exit();
}
}
/*End*/
$headers = "From: $_POST[email]"."\r\n";
$headers .= 'Bcc:bharath#briofactors.com'."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1; format=flowed\n';
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "X-Mailer: PHP\n";
if (mail($to, $subject, $message, $headers)) {
header("location:thanks.html");
}

HTML/PHP Form sending emails with blank fields

I have written a contact form in Dreamweaver and embedded it in Adobe Muse. Problem is that the form sends an email OK, but with blank fields, just the headings. It's probably a problem with my code, as I'm a relative noob with HTML and even more so with PHP. The PHP script was originally downloaded from 123reg, as that is where the website is hosted. Obvs I've tweaked it to suit. I've looked at other posts with similar problems but nothing has worked.`
The form seems to work ok, so maybe I can start with the PHP.
<?php
$EmailFrom = "sales#trisomet.com";
$EmailTo = "sales#hairwraps.co.uk";
$Subject = "QuikQuote Web Enquiry";
$name = trim(stripslashes($_POST['Name']));
$email = trim(stripslashes($_POST['Email']));
$tel = trim(stripslashes($_POST['Tel']));
$textfield2 = trim(stripslashes($_POST['Postcode']));
$textarea = trim(stripslashes($_POST['Message']));
$RoofType = trim(stripslashes($_POST['Roof Type']));
$number = trim(stripslashes($_POST['Roof Length']));
$number2 = trim(stripslashes($_POST['Roof Width']));
$RadioGroup1 = trim(stripslashes($_POST['Fixings']));
$Flashing = Trim(stripslashes($_POST['Flashings']));
$panels = trim(stripslashes($_POST['Clear Panels']));
$number3 = trim(stripslashes($_POST['Quantity']));
$number4 = trim(stripslashes($_POST['Length']));
// 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 .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $tel;
$Body .= "\n";
$Body .= "Postcode: ";
$Body .= $textfield2;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $textarea;
$Body .= "\n";
$Body .= "Roof Type: ";
$Body .= $RoofType;
$Body .= "\n";
$Body .= "Roof Length: ";
$Body .= $number;
$Body .= "\n";
$Body .= "Roof Width: ";
$Body .= $number2;
$Body .= "\n";
$Body .= "Fixings: ";
$Body .= $RadioGroup1;
$Body .= "\n";
$Body .= "Flashing: ";
$Body .= $Flashing;
$Body .= "\n";
$Body .= "Clear Panels: ";
$Body .= $panels;
$Body .= "\n";
$Body .= "Quantity: ";
$Body .= $number3;
$Body .= "\n";
$Body .= "Length (m): ";
$Body .= $number4;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"1;URL=index.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"1;URL=error.html\">";
}
?>
Does anything look obviously wrong?
I think my HTML declaration is ok -
<form action="contactform.php" method="post" class="form">
Any help would be fantastic as it's doing my nut in!!
here's the html
<div id="formWrapper">
<form action="contactform.php" method="post" class="form">
<div class="container" style="width: 100%; ">
<div class="sidebar" style="width: 365px; height:255px; margin-left:10px; float: left; ">
<label for="textfield"><strong>Name</strong>:</label>
<input name="textfield" type="text" required id="textfield" size="40">
</p><br>
<p>
<label for="email"><strong>Email:</strong></label>
<input name="email" type="email" required id="email" size="40">
</p><br>
<p>
<label for="tel"><strong>Telephone: </strong></label>
<input name="tel" type="tel" required id="tel" size="40">
</p><br><p>
<label for="textfield2"><strong>Postcode:</strong></label>
<input name="textfield2" type="text" required id="textfield2">
</p>
</div>
<div class="content" style="width:365px; height: 255px; float:right; margin-right:10px;">
<label for="textarea"><strong>Message:</strong></label>
<strong>
<textarea name="textarea" rows="13" cols="40"></textarea>
</strong></p>
</div>
</div>
<div class="content3" style="width: 220px; height:110px; margin-left:10px; float: left;">
<label for="Roof Type"><strong>Roof Type: </label>
</strong>
<label>
<input name="Roof Type" type="radio" required id="RoofType_0" title="Roof Type" value="Single Slope">
Single Slope</label>
<label>
<input type="radio" name="Roof Type" value="Symmetrical Apex" id="RoofType_1">
Symmetrical Apex</label>
<label>
<input type="radio" name="Roof Type" value="Asymmetrical Apex" id="RoofType_2">
Asymmetrical Apex</label>
</div>
<div class="content4" style="width: 215px; height: 110px; float:left; ">
<label for="number"><strong>Roof Length (m):</strong></label>
<input name="number" type="number" required id="number">
<label for="number2"><strong>Roof Width (m):</strong></label>
<input name="number2" type="number" required id="number2">
</div>
<div class="content5" style="width:210px; height: 110px; float:left; ">
<label><strong>Fixings:</strong><br>
<input name="RadioGroup1" type="radio" required id="RadioGroup1_0" value="Timber">
Timber</label>
<label>
<input type="radio" name="RadioGroup1" value="Light Section Steel" id="RadioGroup1_1">
Light Section Steel</label>
<label>
<input type="radio" name="RadioGroup1" value="Heavy Section Steel" id="RadioGroup1_2">
Heavy Section Steel</label>
</div>
<div class="content6" style="width:85px; height: 110px;float:left; ">
<strong>Flashing:
</strong>
<label>
<input name="Flashing" type="radio" required id="Flashing_0" value="Yes">
Yes</label>
<label>
<input type="radio" name="Flashing" value="No" id="Flashing_1">
No</label>
</div>
<div class="content7" style="width:150px; margin-left: 10px; height: 90px; float:left; ">
<label for "panels" style="font-weight:bold">Clear Panels:</strong></label>
<label>
<input name="panels" type="radio" required id="panels_0" value="Yes">
Yes</label>
<label>
<input type="radio" name="panels" value="No" id="panels_1">
No</label>
</div>
<div class="content8" style="width:100px; height: 110px; float:left;">
<label for="number3"><strong>Quantity:</strong></label>
<input name="number3" type="number" required id="number3" style="width:80px" value="0">
</div>
<div class="content9" style="width:150px; height:110px;float:left;">
<label for="number4"><strong>Length (m):</label>
<input name="number4" type="number" required id="number4" style="width:80px" value="0">
</div>
<div class="submitb" style="width:190px; height:110px; float:left; ">
<p>
<input type="submit" name="submit" id="submit" value="Get A Quote >">
</p>
</div>
</form></div>
I seem to have solved my own problem. I thought the top $POST fields were the field headings for the emails, whereas they are actually the field names from my form. DUH!!!!!

PHP not working correctly?

I have this PHP code here, which should sent the Names, Email and comment of the user to an email. Although it doesn't seem to be working any more. I've been using this code as a basis for my contact form for many years. Now they're just not working.
Your message is being sent...
<?php
$emailSubject = 'A message from: '.$_POST['name'] .' '.'-'.' ' .'Email Adress: ' .$_POST['email'];
$webMaster = 'webmaster#gmail.com';
$name1 = $_POST['name1'];
$name2 = $_POST['name2'];
$email = $_POST['email'];
$sub = $_POST['sub'];
$comments = $_POST['comments'];
$body = <<<EOD
<br><hr><br>
<b>First Name: $name1 </b><br>
<b>Last Name: $name2 </b><br>
Email: $email <br>
Subject: $sub <br>
Comments: $comments <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body,
$headers);
$theResults = <<<EOD
<html>
<head>
<title>Your message is being sent...</title>
<meta http-equiv="refresh" content="4;URL=http://www.google.co.uk">
<style type="text/css">
<!--
body {
background-color: #25EA5F;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 20px;
font-style: normal;
line-height: normal;
font-weight: normal;
color: #30489E;
text-decoration: none;
padding-top: 350px;
margin-left: 550px;
width: 800px;
}
-->
</style>
</head>
</html>
</body>
EOD;
echo "$theResults";
?>
It's very confusing and annoying. Messages when sent to Hotmail don't even appear, so that'll force the clients to get a new email because of it, and when sent to Google... Does it not accept $_POST or something?
As you can see, the SCRIPT does work, it's just with G-mail none of the important information seems to show up, and on Hotmail the messages don't even send/appear!! Thanks for your help. Here is the HTML if needed.
<div class="contactwrap">
<div class="ContactInner">
<div class="ContactTitle">CONTACT US</div>
<form action="Scripts/contact.php" method="post">
<div class="containboxes">
<span id="sprytextfield1">
<label for="text1"></label>
<input name="text1" type="text" class="inputstyle" id="name1" value="First Name" size="5" maxlength="5" />
<span class="textfieldRequiredMsg"></span></span>
<span id="sprytextfield2">
<label for="text2"></label>
<input name="text2" type="text" class="inputright" id="name2" value="Last Name" />
<span class="textfieldRequiredMsg"></span></span>
<span id="sprytextfield3">
<label for="text3"></label>
<input name="text3" type="text" class="inputstyleBelow" id="email" value="E-mail Address" />
<span class="textfieldRequiredMsg"></span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span>
<span id="sprytextfield4">
<label for="text4"></label>
<input name="text4" type="text" class="inputrightBelow" id="sub" value="Message Subject" />
<span class="textfieldRequiredMsg"></span></span>
<span id="sprytextarea1">
<label for="textarea1"></label>
<textarea name="textarea1" cols="45" rows="5" class="inputstyleMsg" id="comments">Message Here</textarea>
<span id="countsprytextarea1"> </span><span class="textareaRequiredMsg"></span> <span class="textareaMinCharsMsg">Minimum number of characters not met.</span><span class="textareaMaxCharsMsg">Exceeded maximum number of characters.</span></span>
<div class="sendbtn"><input name="Submit message" type="submit" class="submitmail" value="" /></div>
</div>
</form>
</div>
</div>
Your form fields are incorrect. id is NOT used in form submission. the name parameter is. Since you've named your form fields as text1, text2, etc... but are looking for name1 and similar in your PHP code, you're trying to retrieve non-existent fields.
e.g. you have:
<input name="text1" type="text" class="inputstyle" id="name1" value="First Name" size="5" maxlength="5" />
$_POST['name1'];
no, that field will actually be $_POST['text1'].

Send.php not showing content of the forms

so i finally got my feedback form to be working and sending emails to my email address but i have another problem. The content of the forms are not showing, don't know what is wrong. I'm new to php tho so i'm very sure its simple.. posted the code below
<?php
$name = $_POST ['name'];
$mail = $_POST ['mail'];
$number = $_POST ['number'];
$feedback = $_POST ['feedback'];
$to = 'ATotallyRandom#Email.net';
$subject = 'Feedback from atetaconsult.com';
$msg = "Your name: $name\n" .
" Your email: $email\n" .
" Feedback: $feedback\n";
mail ($to, $subject, $msg, 'From:' . $email);
echo ' thank you <br/>';
echo ' your name ' .$name .'<br/>';
echo ' Your email ' . $email .'<br/>';
echo ' Your feedback ' . $feedback . '<br/>';
?>
The HTML code for the form is below too
<form method="post" action="send.php">
<p class="head" style="font-style: italic; color: #fff; line-height: 24px; font-size: 19px; margin-bottom: 47px; margin-top: 20px; font-family: Lato, sans-serif;">
We’d love to hear from you. Interested in working together? Fill out the form below with some info about your project and I will get back to you as soon as I can. Please allow a couple days for me to respond.</p>
<div class="row form">
<input id="name" class="name" type="text" placeholder="Name">
<input id="mail" class="mail" type="text" placeholder="Email">
<input id="number" class="phone" type="text" placeholder="Phone">
</div>
<div class="span6 box box_r">
<textarea id="feedback" rows="6" class="span6" placeholder="Type a message here..."></textarea>
</div>
<div class="btn" style="margin-top:5px;">
<input type="submit" value="Send your message">
</div>
</form><div style="clear: both;"></div>
Here:
$msg = "Your name: $name\n" .
" Your email: $email\n" .
" Feedback: $feedback\n";
you are asking for the variable $email
but you have specified it as $mail at the top.
See: http://markpetherbridge.co.uk/StackOverflow/phpmail.php?name=Mark&mail=test#test.com&feedback=testfeedback
I have changed it to $email and the type to $_GET for demonstration purposes, but it seems to be working. Try that.
Here is the Full code for the file:
<?php
$name = $_GET ['name'];
$email = $_GET ['mail'];
$number = $_GET ['number'];
$feedback = $_GET ['feedback'];
$to = 'ATotallyRandom#Email.net';
$subject = 'Test from Mark P';
$msg = "Your name: $name\n";
$msg .= " Your email: $email\n";
$msg .= " Feedback: $feedback\n";
mail ($to, $subject, $msg, 'From:' . $email);
echo ' thank you <br/>';
echo ' your name ' .$name .'<br/>';
echo ' Your email ' . $email .'<br/>';
echo ' Your feedback ' . $feedback . '<br/>';
?>
<br /><br />
Here is the result from $msg: <br /><br />
<?php
echo $msg;
?>
You need to add name into your html:
<input name="name" id="name" class="name" type="text" placeholder="Name">
<input name="mail" id="mail" class="mail" type="text" placeholder="Email">
<input name="number" id="number" class="phone" type="text" placeholder="Phone">
Put all these code in between:
if($_POST){
// Your coding
}
Also show me the Javascript/Jquery coding you are using on contact form.
You haven't set a name value on the input.
Change in:
<form method="post" action="send.php">
<p class="head" style="font-style: italic; color: #fff; line-height: 24px; font-size: 19px; margin-bottom: 47px; margin-top: 20px; font-family: Lato, sans-serif;">
We’d love to hear from you. Interested in working together? Fill out the form below with some info about your project and I will get back to you as soon as I can. Please allow a couple days for me to respond.</p>
<div class="row form">
<input name="name" id="name" class="name" type="text" placeholder="Name">
<input name="mail" id="mail" class="mail" type="text" placeholder="Email">
<input name="number" id="number" class="phone" type="text" placeholder="Phone">
</div>
<div class="span6 box box_r">
<textarea name="feedback" id="feedback" rows="6" class="span6" placeholder="Type a message here..."></textarea>
</div>
<div class="btn" style="margin-top:5px;">
<input type="submit" value="Send your message">
</div>
</form><div style="clear: both;"></div>

Wordpress giving me a 404 error for php form

Since the Wordpress form seems to not want to send me the password to my account for help on their forums I figured I would start here.
I have created a form for my website so that someone can contact us.
But when the person clicks the 'send' button it takes them to a 404 page, which the address reads:
website address - /working/mail.php
I know Wordpress supports .php files and I have mine correctly named and wasn't sure if I am missing something
<div id="FGSform">
<form action="mail.php" method="post" name="contactFGS" id="contactFGS">
<ul>
<li>
<label for="first-name">First Name</label>
<br>
<input type="text" id="firstname" name="firstname" required aria-required="true">
</li>
<br>
<li>
<label for="last-name">Last Name</label><br>
<input type="text" id="lastname" name="lastname" required aria-required="true">
</li>
<br>
<li>
<label for="email">Email</label>
<br>
<input type="email" id="email" name="email" required aria-required="true">
</li>
<br>
<li>
<label for="contact-reason" id="reason" name="reason">Reason for Contact</label>
<select required id="reason" name="reason">
<option value="3">Employment</option>
<option value="1">Print Services</option>
<option value="2">Design Services</option>
<option value="4">Questions</option>
<option value="5">Other</option>
</select>
</li>
<br>
<li>
<label for="comments">Comments</label>
<br>
<textarea name="comments" id="comments" cols="40" rows="10" required></textarea>
</li>
<br>
<li>
<input type="radio" id="newsletter" name="newsletter">
<label for="signmeup">Sign me up for newsletter, updates and other information about FGS</label>
</li>
<br>
<li>
<input type="submit" value="Send">
</li>
Here is my .php file:
<?php
/* Email Variables */
$emailSubject = 'mail!';
$webMaster = 'kmurray.1#frgraphicsolutions.com';
/* Data Variables */
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$email = $_POST['email'];
$body = <<<EOD
<br><hr><br>
Name: $name <br>
Last Name: $lastname <br>
Email: $email <br>
Comments: $comments <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body,
$headers);
/* Results rendered as HTML */
$theResults = <<<EOD
<html>
<head>
<title>sent message</title>
<meta http-equiv="refresh" content="3;URL=http://frgraphicsolutions.com/working/?page_id=8">
<style type="text/css">
<!--
body {
background-color: #444;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 20px;
font-style: normal;
line-height: normal;
font-weight: normal;
color: #fec001;
text-decoration: none;
padding-top: 200px;
margin-left: 150px;
width: 800px;
}
-->
</style>
</head>
<div align="center">Thank you!</div>
</div>
</body>
</html>
EOD;
echo "$theResults";
?>
I have never created a form before, but Wordpress plug-ins are terrible designed and/or over complicate the form giving me little control over what I want in it.
Any help is much appreciated.
I cannot post comments yet or I would add this as a comment but it seems there is no mail.php file. Can you check the full path to the file? It's not in / or in /working/.
Edit in response to your comment:
You should try two things in that order:
Output the result of the form just to check that you are reaching
the form processing file and that everything is working on that
side.
Send an email to yourself using basic parameters, in a file with no
link with the form. This way you'll know if you are able to send
emails properly and you'll be closer to a solution.
Let us know what you find.
After time spent with the OP to find out what the problem was, have determined the following solution:
Set your your form action to this
<form action="/working/wp-content/themes/NEW/mail.php" method="post" name="contactFGS" id="contactFGS">
Since the mail.php file (handler) was inside a themes folder, and not as previously thought.
Change the path in the form tag to /mail.php or whatever is the url of mail.php
EDIT:
Incase it's not clear, just change the form action to the url of your mail.php..

Categories