$n1=$_POST['n1'];
$n2=$_POST['n2'];
$email=$_POST['email'];
We need to validate the data and reply with error message to the visitors so we will set a flag and check the data.
$status = "OK"; // Flag is set
$msg=""; // Error message to store
/////
// Data Validation starts ///////
// if first Name is less than 3 char then status is not ok
if( strlen($n1) < 3){
$msg=$msg."Please enter your name
";
$status= "NOTOK";}
if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
$msg=$msg." Please check your email address
";
$status= "NOTOK";}
If you want add more validation then you can further extend the above code to cover other data.
mail("user@example.com",$sub,$body_dtl,$headers); // Posting mail
Here is the complete code.
<?Php
@$feedback=$_POST['feedback'];
if(isset($feedback) and $feedback=="post_feedback"){
////////////////// Collecting all data ///////////
$n1=$_POST['n1'];
$n2=$_POST['n2'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$add1=$_POST['add1'];
$add2=$_POST['add2'];
$city=$_POST['city'];
$state=$_POST['state'];
$country=$_POST['country'];
$zip=$_POST['zip'];
$sum=$_POST['sum'];
$dtl=$_POST['dtl'];
///////////////// end of collecting data///
$status = "OK"; // Flag is set
$msg=""; // Error message to store
/////
// Data Validation starts ///////
// if first Name is less than 3 char then status is not ok
if( strlen($n1) < 3){
$msg=$msg."Please enter your name<BR>";
$status= "NOTOK";}
if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
$msg=$msg." Please check your email address<BR>";
$status= "NOTOK";}
if( $sum <> 3){
$msg=$msg."Please check the spam control Sum<BR>";
$status= "NOTOK";}
if( strlen($dtl) < 3){
$msg=$msg."Please enter your message details<BR>";
$status= "NOTOK";}
///// You can add more such validation for other data if required ///
///// End of data validation /////
if($status<>"OK"){
$msg = "<font face='Verdana' size='2' color=red>$msg</font>"; // post the error message
}else{ // if all validations are passed.
//// Sending mail ////
$headers="";
$headers4=$email; // Change this to change from address
$headers.="Reply-to: $headers4 n";
$headers .= "From: $headers4 n";
$headers .= "Errors-to: $headers4 n";
$browser=$_SERVER['HTTP_USER_AGENT'] . "nn"; // browser details of user
$ip=$_SERVER['REMOTE_ADDR']; // IP address of user
$body_dtl="This is the feedback from sitename.com \n\n First Name :$n1 \n Last Name :$n2
\n Email Addrss: $email \n Phone no:$phone \n Address1:$add1 \n Address2:$add2 \n City:$city
\n State:$state \n Country:$country \n Zip:$zip \n IP:$ip nn".$browser. "Message Details : $dtl";
//echo $body_dtl;
//echo $headers;
$sub="Feedback From example.com";
//mail("user@example.com",$sub,$body_dtl,$headers); // Posting mail
//// End of sending Mail /////
$msg= "<center><font face='Verdana' size='2' color=green>
Thank you <br> We will back to you shortly. </font></center>";
}
$str= "{ "value" : [{"msg" : "$msg","status" : "$status"}]}";
echo $str;
}// Checking of if condition if form is submittted
?>
Basic of Feedback from
Part 2 HTML part of Feedback from
Part 3 JavaScript code of Ajax Feedback from
part 4 PHP script of Feedback from
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.