/    Sign up×
Community /Pin to ProfileBookmark

How do I add as a not mandatory field on PHPmailer?

Hello, I have problem with PHPMailer email sending, there are 5 input fields –
1. Name
2. Email
3. Phone
4. Website
5. Message
— Where I want Name, email & message as required other two – Phone & Website as not mandatory.
Here`s my code below, please someone help me –
This is “form-process.php” file —

[CODE]<?php
require(“class.phpmailer.php”);
$errorMSG = “”;
// NAME
if (empty($_POST[“name”])) {
$errorMSG = “Name is required “;
} else {
$name = $_POST[“name”];
}
// EMAIL
if (empty($_POST[“email”])) {
$errorMSG .= “Email is required “;
} else {
$email = $_POST[“email”];
}
// MESSAGE
if (empty($_POST[“message”])) {
$errorMSG .= “Message is required “;
} else {
$message = $_POST[“message”];
}
// prepare email body text
$MsgContent = “”;
$MsgContent .= “Name: “;
$MsgContent .= $name;
$MsgContent .= “<br> n”;
$MsgContent .= “Email: “;
$MsgContent .= $email;
$MsgContent .= “<br> n”;
$MsgContent .= “Message: “;
$MsgContent .= $message;
$MsgContent .= “<br> n”;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = ‘ssl’;
$mail->Host = “smtp.gmail.com”;
$mail->Port = 465;
$mail->SetLanguage(“tr”, “phpmailer/language”);
$mail->CharSet =”utf-8″;
$mail->Username = “[email protected]”;
$mail->Password = “password***”;
$mail->SetFrom(“[email protected]”, “Email Server”);
$mail->AddAddress(“[email protected]”);
$mail->Subject = “Contact us email from $name”;

$mail->Body = $MsgContent ;
$mail->IsHTML(true);
$success = $mail->Send();
if ($success && $errorMSG == “”){
echo “success”;
}else{
if($errorMSG == “”){
echo “Something went wrong :(“;
} else {
echo $errorMSG . “<br> Mailer Error: ” . $mail->ErrorInfo;
}
}
?>[/CODE]

And this is the javascript file for ajax loading with validation –

[CODE]$(“#contactForm”).validator().on(“submit”, function (event) {
if (event.isDefaultPrevented()) {
// handle the invalid form…
formError();
submitMSG(false, “Did you fill in the form properly?”);
} else {
// everything looks good!
event.preventDefault();
submitForm();
}
});
function submitForm(){
// Initiate Variables With Form Content
var name = $(“#name”).val();
var email = $(“#email”).val();
var message = $(“#message”).val();
$.ajax({
type: “POST”,
url: “php/form-process.php”,
data: “name=” + name + “&email=” + email + “&message=” + message,
success : function(text){
if (text == “success”){
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}
function formSuccess(){
$(“#contactForm”)[0].reset();
submitMSG(true, ” $name Message Submitted!”)
}
function formError(){
$(“#contactForm”).removeClass().addClass(‘shake animated’).one(‘webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend’, function(){
$(this).removeClass();
});
}
function submitMSG(valid, msg){
if(valid){
var msgClasses = “h3 text-center tada animated text-success”;
} else {
var msgClasses = “h3 text-center text-danger”;
}
$(“#msgSubmit”).removeClass().addClass(msgClasses).text(msg);
}[/CODE]

Please help me, someone.
Thanks

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmSep 28.2017 — So, ignoring the JS...

In your php you check the inputs for values/validity. If you find something wrong WHY do you continue on with the email process? Why aren't you outputting an error message or re-displaying your form so that the user can correct it? You just keep going on and you want help?
Copy linkTweet thisAlerts:
@rkalwaysauthorSep 28.2017 — So, ignoring the JS...

In your php you check the inputs for values/validity. If you find something wrong WHY do you continue on with the email process? Why aren't you outputting an error message or re-displaying your form so that the user can correct it? You just keep going on and you want help?[/QUOTE]


Hello, thanks for your reply. As you can see on the php code there are validations already set for three items

1. Name [Required]

2. Email [Required]

3. Message [Required]

Now i want to get two values from phone & website input field without validating them , I mean they are not required but if someone posts those values I wanna get them through the input form. So my question`s how do I gonna do that based on my codes?


  • * For more clerifications I have put my code there .
  • Copy linkTweet thisAlerts:
    @rkalwaysauthorSep 28.2017 — As you can see I cant get values from not mandatory input fields (phone, website) , <br/>
    So All I want is to get those value if someone puts it otherwise it
    ll pass the mail.

    I hope you understand based on my code.
    Copy linkTweet thisAlerts:
    @ginerjmSep 28.2017 — Why can't you get them EXACTLY as you got the mandatory fields? If they have to come thru the Ajax call then do whatever you are doing there just don't error them out if they are missing.
    Copy linkTweet thisAlerts:
    @rkalwaysauthorSep 29.2017 — Why can't you get them EXACTLY as you got the mandatory fields? If they have to come thru the Ajax call then do whatever you are doing there just don't error them out if they are missing.[/QUOTE]


    Because Im getting an error by doing that, thats why I want someone`s help who can tell me what to put exactly.

    Can you please tell me what to call there so that I can get those value without rising any error?
    Copy linkTweet thisAlerts:
    @rootSep 29.2017 — Use the HTML5 attribute of [B]required[/B] as one of the attributes of the <input> tag.

    As for the email side, using the $_POST arrays directly is a very bad thing, script injection can be used to break the page, Injection (SQL) methods could be used to attack the database or gain access to it.

    Sanitize the inputs in to an array that you then use and you know that it is safe, do not just accept any field names, use a whitelist of names on input tags that you allow and check the request method, if its not POST then it could ba abot using a PUSH method of sending data to your server to mimic a form being submitted.
    Copy linkTweet thisAlerts:
    @rootSep 29.2017 — Oh, and an error can be due to you missing an attribute off the web form, you should specify a

    .: name -- for identity and for easier DOM access client-side.

    .: action -- the script handler on the server

    .: method -- how to send the data (eg POST)

    .: enctype -- the type of data (eg multipart/form-data for post)

    My web host rejects web forms if they fail to specify the method and enctype attributes. I suggest that you check your form and the requirements from the web host, my web host insists that your email address for your emailer is a real world email address on your domain, again, check the conditions of the service provider you are using, each one has different ways of allowing web forms, you may require additional code in the emailer or you might need to have a token that the server (pop/imap) recognises as being a legitimate request...
    ×

    Success!

    Help @rkalways spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.8,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...