improved formatting - javascript

I have an HTML form that is a div in a bigger page:
<form action="send_form_email.php" method="post">
<h4>E-mail</h4>
<div class="border-stripes">
<input type="email" class="textfield" name="email" placeholder="Your e-mail address" />
</div>
<h4>Message</h4>
<div class="border-stripes">
<textarea class="textarea" name="message" rows="3" placeholder="Your message"></textarea>
</div>
<br />
<br />
<input id="submit" name="submit" type="submit" value="Submit">
</form>
and I have this php code
if ($_POST['submit']) {
if ($email == false) {
// ***Insert <p>invalid email address</p> above <h4>email</h4>***
} else if (strlen(trim($_REQUEST['message'])) == 0) {
// ***Insert <p>Send a Message</p> above <h4>email</h4>***
} else if (mail($to, $subject, $message, $email)) {
// ***Insert <p>Successfully sent</p> above <h4>email</h4>***
}
}
My problem is that I don't know how to add that extra paragraph according to the if statement, and then I want the page to automatically scroll down to the form displaying the error.
For instance, if the email address was invalid, I want the page to be redirected to the form div and display above "Email" that the email address is invalid

Put your message into a variable for each if statement like this:
if ($_POST['submit']) {
if ($email == false) {
$msg = "<p>invalid email address</p>";
} else if (strlen(trim($_REQUEST['message'])) == 0) {
$msg = "<p>Send a Message</p>";
} else if (mail($to, $subject, $message, $email)) {
$msg = "<p>Successfully sent</p>";
}else{
$msg ="";
}
}else{
$msg = "";
}
And then echo your message above the email field:
<form action="send_form_email.php" method="post">
<?php echo $msg; ?>
<h4>E-mail</h4>
<div class="border-stripes">
<input type="email" class="textfield" name="email" placeholder="Your e-mail address" />
</div>
<h4>Message</h4>
<div class="border-stripes">
<textarea class="textarea" name="message" rows="3" placeholder="Your message"></textarea>
</div>
<br />
<br />
<input id="submit" name="submit" type="submit" value="Submit">

What I understand from the problem you stated here is that -- you want to display error or success message if the post is submitted successfully or not.
For that you you can write both the code in same file send_form_email.php like as below.
<?php
if (isset($_POST)) {
if ($_POST['email'] == false) {
$msg = "<p>invalid email address</p> above <h4>email</h4>";
} else if (strlen(trim($_POST['message'])) == 0) {
$msg = "Insert <p>Send a Message</p> above <h4>email</h4>";
} else if (mail($to, $subject, $message, $email)) {
$msg = "Insert <p>Successfully sent</p> above <h4>email</h4>";
}
}
?>
<html>
<head>
<title> My First Form </title>
</head>
<body>
<form action="send_form_email.php" method="post">
<h4>E-mail</h4>
<div class="border-stripes">
<input type="email" class="textfield" name="email" placeholder="Your e-mail address" />
</div>
<h4>Message</h4>
<div class="border-stripes">
<textarea class="textarea" name="message" rows="3" placeholder="Your message"></textarea>
</div>
<br />
<br />
<input id="submit" name="submit" type="submit" value="Submit">
</form>
<div class="msg-box">
<?php echo $msg ? $msg : ''; ?>
</div>
</body>
</html>

Related

Else statement being run even when if statement is true?

I'm trying to establish a verification system for a form, whereas one of the input elements is called verification. The form should only be submitted (as an email to someone else) if the value of verification is 108 — which works. However, when I run my code, I find that the code inside the else statement (of the embedded if-statement) also runs, and I get a popup window that asks me what 3+105 is. Why is this?
HTML
<form class="pb-5" action="" method="POST">
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputName4">Name</label>
<input
type="text"
class="form-control"
id="inputName"
name="name"
value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name'], ENT_QUOTES) : ''; ?>"
required
/>
</div>
<div class="form-group col-md-6">
<label for="inputCompany">Company</label>
<input
type="text"
class="form-control"
id="inputCompany"
name="company"
value="<?php echo isset($_POST['company']) ? htmlspecialchars($_POST['company'], ENT_QUOTES) : ''; ?>"
required
/>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="subject">Subject</label>
<input
type="text"
class="form-control"
id="subject"
name="subject"
value="<?php echo isset($_POST['subject']) ? htmlspecialchars($_POST['subject'], ENT_QUOTES) : ''; ?>"
required
/>
</div>
<div class="form-group col-md-6">
<label for="inputEmail">Email</label>
<input
type="email"
class="form-control"
id="inputEmail"
name="email"
value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email'], ENT_QUOTES) : ''; ?>"
required
/>
</div>
</div>
<div class="form-group">
<label for="verification">What is 5+103?</label>
<input
type="text"
class="form-control"
id="verification"
name="verification"
required
></input>
</div>
<div class="form-group">
<label for="textarea">Message</label>
<textarea
class="form-control"
id="textarea"
rows="3"
name="message"
required
><?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message'], ENT_QUOTES) : ''; ?>"</textarea>
</div>
<button type="submit" class="btn btn-primary my-3">
Send
</button>
</form>
PHP
$verification = $_POST["verification"];
settype($verification, "integer");
if($_SERVER["REQUEST_METHOD"] == "POST") {
if($verification == 108) {
mail($to, $subject, $message, $headers);
echo "<script> alert('Thank you for your mail!')</script>";
} elseif ($_SERVER["REQUEST_METHOD"] == "POST" && mail($to, $subject, $message, $headers) === false) {
echo "<script> alert('An error has occurred.')</script>";
} else {
echo "<script> alert('What is 3+105?')</script>";
}
} else {
return;
}
If I can clarify my issue in any way, please do not hesitate to let me know! Thanks in advance!
I suppose you have to rewrite your if ... else ...
$verification = $_POST["verification"];
settype($verification, "integer");
if($_SERVER["REQUEST_METHOD"] == "POST") {
if($verification == 108) {
//TEST IF MAIL IS CORRECTLY SEND
if( mail($to, $subject, $message, $headers) === false){
echo "<script> alert('An error has occurred.')</script>";
}
else{
echo "<script> alert('Thank you for your mail!')</script>";
}
}
else {
echo "<script> alert('What is 3+105?')</script>";
}
}

How do I creat a jquery form validator

I am trying to create a dynamic website using php. The site is uploaded to a free domain this is my first real world project so I want to put in as best as I can. I created a modal, in it there is a form and I want this from to validate and without refreshing the page. but I don't know why my mail() is not working please can someone tell me what I am doing wrong.
This is my fixed form inside the modal:
<div class="modal fade" id="myModal" tabindex="-1" role="form" aria-labelledby="#modalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h3 class="modal-title " id="modalLabel">Send
<?php echo
$brandName; ?> a message</h3>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<div class="thumbnail">
<form action="contact_process.php" method="POST" role="form" autocomplete="off" class="form-container">
<small>
<i class="text-center
text-danger">all fields are required</i>
</small>
<div class="form-group has-danger">
<label class="sr-only" for="name">Name
</label>
<input type="text" name="name" placeholder="Your Name" class="form-control" id="mail-name" tabindex="1">
</div>
<div class="form-group has-danger">
<label class="sr-only" for="email">Email
</label>
<input type="email" name="email" placeholder="Your E-mail" class="form-control" id="mail-email" tabindex="2">
</div>
<div class="form-group has-danger">
<label for="tel" class="sr-only">Tell
no.
</label>
<input type="text" name="phone" placeholder="Your Phone Number" class="form-control" id="mail-
phone" tabindex="3">
</div>
<div class="form-group has-danger">
<label for="message" class="sr-only">
</label>
<textarea class="form-control
form-textarea" placeholder="Message" rows="5" spellcheck="true" name="message" id="mail-message" tabindex="4"></textarea>
</div>
<input type="button" class="btn
btn-danger" data-dismiss="modal" aria-label="Close" value="Cancel" tabindex="5">
<input id="mail-submit" type="submit" class="btn btn-primary" value="Send Message" name="submit" data- submit="...Sending" tabindex="6">
<p class="form-msg"></p>
</form>
</div>
<!--END thumbnail-->
</div>
<!--END col-->
</div>
<!--END row-->
</div>
<!--modal-body-->
</div>
<!--modal-content-->
</div>
<!--modal-dialog-->
</div>
<!---modal-->
This is the php form validation in my includes folder:
<pre>
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$errorEmpty = false;
$errorEmail = false;
$errorPhone = false;
if (empty($name) || empty($email) || empty($phone) || empty($message)) {
echo "<span class='err'>Fill in all fields!</span>";
$errorEmpty = true;
}
elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<span class='err'>Invalid email please make sure you enter a
valid email address!</span>";
$errorEmail = true;
}
elseif (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?
\d{3}[\s-]?\d{4}$/i",$phone)) {
echo "<span class='err'>Please enter only a valid phone number!
</span>";
$errorPhone = true;
}
else{
// add resipeants
$to = "danjumashiwaju#gmail.com";
// create subject
$subject = "$name sent you a message from www.djshumzy.com";
// construct a message
$msg = "Name: $name\r\n";
$msg .= "Email: $email\r\n";
$msg .= "Tell: $phone\r\n";
$msg .= "Message: \r\n$message";
$msg = wordwrap($meg, 70);
//set mail header
$headers = "MINE-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=1so-8859-1\r\n";
$headers .= "From: $name <$email>\r\n";
$headers .= "X-priority: 1\r\n";
$headers .= "X-MSMail-priority: High1\r\n\r\n";
mail( $to, $subject, $msg, $headers);
} if (mail === 1) {
echo "<span class='suc'>Thank You!...Your Message has been Sent !
</span>";
}
}
?>
<script>
$("#mail-name, #mail-email, #mail-phone,
#mail-message").removeClass("input-error");
var errorEmpty = "<?php echo $errorEmpty; ?>";
var errorEmail = "<?php echo $errorEmail; ?>";
var errorPhone = "<?php echo $errorPhone; ?>";
if (errorEmpty == true) {
$("#mail-name, #mail-email, #mail-phone,
#mail-message").addClass("input-error");
}
if (errorEmail == true) {
$("#mail-email").addClass("input-error");
}
if (errorPhone == true) {
$("#mail-phone").addClass("input-error");
}
if (errorEmpty == false && errorEmail == false && errorPhone == false) {
$("#mail-name, #mail-email, #mail-phone, #mail-message").val("");
}
</script>
</pre>
Here is my jquery validation(.js)
$(document).ready(function() {
$("form").submit(function(event) {
event.preventDefault();
var name = $("#mail-name").val();
var email = $("#mail-email").val();
var phone = $("#mail-phone").val();
var message = $("#mail-message").val();
var submit = $("#mail-submit").val();
$(".form-msg").load("includes/contact_process.php", {
name: name,
email: email,
phone: phone,
message: message,
submit: submit
});
});
});// END FORM VALIDATING
Please what do I have to do to make this from submit to emails and validate at the same time.
Thus the form validates but it does not send the email and just gives an error message instead.
Use https://jqueryvalidation.org/
$( "#myform" ).validate({
rules: {
field: {
required: true
},
email: {
required: true,
email: true
}
},
messages: {
field: "Please Enter Your Name",
email: "Please Enter Your Email Address"
}
});
<link href="https://jqueryvalidation.org/files/demo/site-demos.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<form id="myform">
<label for="field">Enter Name : </label>
<input type="text" class="left" id="field" name="field">
<br/>
<label for="email">Email Address: </label>
<input type="text" id="email" name="email">
<br/>
<input type="submit" value="Validate!">
</form>
This jQuery plugin makes simple clientside form validation easy.

Captcha code used to sign up form is not working

I would to like to create a sign up form using Captcha in php with mysql data. Anytime I submit the form, the Captcha code shows an error. But when I check my database connecting code is working. The code is below.
signup.php
<?php
session_start();
?>
<!DOCTYPE html>
enter code here<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Registration Form with PHP Captcha Demo</title>
<meta name="title" content="Registration Form with PHP Captcha Demo"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<link href="css/style_demo.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-3.1.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script language="javascript">
$(document).ready(function(){
$(".refresh").click(function () {
$(".imgcaptcha").attr("src","demo_captcha.php?_="+((new Date()).getTime()));
});
$('#register').submit(function() {
if($('#password').val() != $('#cpassword').val()){
alert("Please re-enter confirm password");
$('#cpassword').val('');
$('#cpassword').focus();
return false;
}
$.post("submit_demo_captcha.php?"+$("#register").serialize(), { }, function(response){
if(response==0){
$(".imgcaptcha").attr("src","demo_captcha.php?_="+((new Date()).getTime()));
clear_form();
alert("Data Submitted Successfully.")
}else{
alert("wrong captcha code!");
}
});
return false;
});
function clear_form()
{
$("#fname").val('');
$("#lname").val('');
$("#username").val('');
$("#email").val('');
$("#dob").val('');
$("#gender").val('');
$("#password").val('');
$("#cpassword").val('');
$("#captcha").val('');
}
});
</script>
</head>
<body>
<div id="bodyfull">
<div id="bodyfull2">
<div id="center">
<div class="inner_right_demo">
<form name="register" action="#null" method="post" id="register">
<div class="form_box">
<div>
<label>First Name</label>
<input type="text" placeholder="Enter Your First Name" id="fname" name="fname" required="required">
</div>
<div>
<label>Last Name</label>
<input type="text" placeholder="Enter Your Last Name" id="lname" name="lname" required="required">
</div>
<div>
<label>User Name</label>
<input type="text" placeholder="Enter Your User Name" id="username" name="username" required="required">
</div>
<div>
<label>Email</label>
<input type="email" placeholder="Enter Your Email Address" id="email" name="email" required="required">
</div>
<div>
<label>Date of birth</label>
<input type="date" id="dob" name="dob">
</div>
<div>
<label>Gender</label>
<div class="otherinputs"><input type="radio" value="Male" checked name="gender"> <span>Male</span> <input type="radio" value="Female" name="gender"> <span>Female</span> </div>
</div>
<div>
<label>Password</label>
<input type="password" placeholder="Enter Your Password" id="password" name="password" required="required">
</div>
<div>
<label>Confirm Password</label>
<input type="password" placeholder="Enter Your Password Again" id="cpassword" name="cpassword" required="required">
</div>
<div>
<label>Captcha</label>
<input type="text" placeholder="Enter Code" id="captcha" name="captcha" class="inputcaptcha" required="required">
<img src="demo_captcha.php" class="imgcaptcha" alt="captcha" />
<img src="images/refresh.png" alt="reload" class="refresh" />
</div>
<div>
<label> </label>
<div class="otherinputs" ><input type="submit" value="Submit" name="B1" class="submit"> </div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
demo_captcha.php
<?php
session_start();
function getRandomWord($len = 5) {
$word = array_merge(range('0', '9'), range('A', 'Z'));
shuffle($word);
return substr(implode($word), 0, $len);
}
$ranStr = getRandomWord();
$height = 35; //CAPTCHA image height
$width = 150; //CAPTCHA image width
$font_size = 24;
$image_p = imagecreate($width, $height);
$graybg = imagecolorallocate($image_p, 245, 245, 245);
$textcolor = imagecolorallocate($image_p, 34, 34, 34);
imagettftext($image_p, $font_size, -2, 15, 26, $textcolor, 'fonts/mono.ttf', $ranStr);
//imagestring($image_p, $font_size, 5, 3, $ranStr, $white);
$_SESSION["vercode"] = $ranStr;
imagepng($image_p);
imagedestroy($image_p);
?>
submit_demo_captcha
<?php
session_start();
$servername="localhost";
$username="root";
$password="1234";
$dbname="form";
$con=mysqli_connect($servername,$username,$password,$dbname);
if(($_REQUEST['captcha'] == $_SESSION['vercode'])){
$lname =$_POST['lname'];
$fname = $_POST['fname'];
$gender = $_POST['gender'];
$username =$_POST['username'];
$email =$_POST['email'];
$dob =$_POST['dob'];
$password =$_POST['password'];
//Here you can write your sql insert statement.
$sql = "INSERT INTO form.form1 (fname,gender,lname,mname,email,dob,password) VALUES('$fname','$gender','$lname','$username','$email','$dob','$password')";
if(mysqli_query($con,$sql)){
alert("Data Submitted Successfully.");
}
else{
alert("wrong captcha code!");
}
}
mysqli_close($con);
?>
submit_demo_captcha.php
<?php
session_start();
$servername="localhost";
$username="root";
$password="1234";
$dbname="form";
$con=mysqli_connect($servername,$username,$password,$dbname);
if(($_REQUEST['captcha'] == $_SESSION['vercode'])){
$lname =$_REQUEST['lname'];
$fname = $_REQUEST['fname'];
$gender = $_REQUEST['gender'];
$username =$_REQUEST['username'];
$email =$_REQUEST['email'];
$dob =$_REQUEST['dob'];
$password =$_REQUEST['password'];
//Here you can write your sql insert statement.
$sql = "INSERT INTO form.form1 (fname,gender,lname,mname,email,dob,password) VALUES('$fname','$gender','$lname','$username','$email','$dob','$password')";
if(mysqli_query($con,$sql)){
echo 1; }
else{
echo 0; }
}
mysqli_close($con);
?>
and
signup.php
$.post("submit_demo_captcha.php?"+$("#register").serialize(), { }, function(response){
if(response==1){
$(".imgcaptcha").attr("src","demo_captcha.php?_="+((new Date()).getTime()));
clear_form();
alert("Data Submitted Successfully.")
}else{
alert("wrong captcha code!");
}
});

calling php from aspx

i have a contact form in aspx page that after clicking submit posts to a php page. but from what i see php is not being called. when i click submit no email is sent or when fileds are empty nothing happens.
this is the form in aspx file:
<form action="form-to-email.php" method="post">
<div class="contact-form margin-top">
<label>
<span>Full name</span>
<input type="text" class="input_text" name="name" id="name" />
</label>
<label>
<span>Email</span>
<input type="text" class="input_text" name="email" id="email" />
</label>
<label>
<span>Subject</span>
<input type="text" class="input_text" name="subject" id="subject" />
</label>
<label>
<span>Message</span>
<textarea class="message" name="feedback" id="feedback"></textarea>
<input type="submit" name="submit" id="submit" class="button" value="Submit Form" />
</label>
</div>
</form>
and this is my php page:
<?
if(!isset($_POST['submit']))
{
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['feedback'];
if(empty($name)||empty($visitor_email)||empty($subject)||empty($message))
{
echo "All fields are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from='someone#gmail.com';
$email_subject=$subject;
$email_body= "You have received a message from the user $name .\n Email address: $visitor_email\n Here is the message:\n $message";
$to = 'someone#gmail.com';
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
i'm using visual studio btw.

Ajax not posting form data

I recently asked a question about executing a Send Mail script without a page reload and worked out what my problem was with my AJAX, that's all resolved. I now have an issue with the following:
When my form posts to my AJAX the script is executed, however the post data doesn't seem to be come through to the PHP script.
The link to my question is: Contact Form same page success.
Here is my PHP:
<?php
$name = ($_POST['name']);
$email = ($_POST['email']);
$fsubject = ($_POST['subject']);
$message = ("Name: ". $name . "\nEmail Address: " . $email . "\n\nMessage: " . $_POST['message']);
// Set Mail
$to = "emailaddress#fakeone.com";
$header = 'From: contactus#fakeone.com' . "\r\n" .
'Reply-To: website#fakeone.com';
$subject = "{$fsubject}";
$body = "{$message}";
// Send Mail
if (mail($to, $subject, $body, $header))
{
echo("<p>Message successfully sent!</p>");
}
else
{
echo("<p>Message delivery failed...</p>");
}
?>
My HTML
<div id="success" style="color:red;"></div>
<form action="" id="contactform" method="post">
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Your name *</label>
<input type="text" value="" data-msg-required="Please enter your name." maxlength="100" class="form-control" name="name" id="name">
</div>
<div class="col-md-6">
<label>Your email address *</label>
<input type="email" value="" data-msg-required="Please enter your email address." data-msg-email="Please enter a valid email address." maxlength="100" class="form-control" name="email" id="email">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Subject</label>
<input type="text" value="" data-msg-required="Please enter the subject." maxlength="100" class="form-control" name="subject" id="subject">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Message *</label>
<textarea maxlength="5000" data-msg-required="Please enter your message." rows="10" class="form-control" name="message" id="message"></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" id="submit" value="Send Message" class="btn btn-info btn-lg" data-loading-text="Loading...">
</div>
</form>
My AJAX
<script>
$(document).ready(function () {
$('#submit').click(function () {
$.post("sendmail.php", $("#contactform").serialize(), function (response) {
$('#success').html(response);
});
return false;
});
});
</script>
When I fill out the forms, nothing comes back it sends an empty email.
Any idea's why the post isn't working on this would be greatly appreciated. If I am breaking any rules by posting another question please let me know!
Regards,
Dan
I think the problem is in your PHP code. You've got a variable named $headers, but in your mail function you're using $header. A typo.
Change the following lines from this -
$subject = "{$fsubject}";
$body = "{$message}";
to this -
$subject = $fsubject;
$body = $message;

Categories

Resources