Help - Search - Members - Calendar
Full Version: Submit form dont work
HTMLHelp Forums > Web Authoring > General Web Design
hpscorpion
I'm using a template website that is practically finished, but the form submit dont work.
Pls anyone can help me?

*file - form-process.php

<?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"];
}

// SUBJECT
if (empty($_POST["subject"])) {
$errorMSG .= "Subject is required ";
} else {
$subject = $_POST["subject"];
}

// MESSAGE
if (empty($_POST["message"])) {
$errorMSG .= "Message is required ";
} else {
$message = $_POST["message"];
}


$EmailTo = "emailaddress@test.com";
$Subject = $subject;

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);

// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Something went wrong sad.gif";
} else {
echo $errorMSG;
}
}

?>

--------------------------------------
*file - form-scripts.js

$("#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 = $("#p_name").val();
var email = $("#p_email").val();
var subject = $("#p_subject").val();
var message = $("#p_message").val();

$.ajax({
type: "POST",
url: "php/form-process.php",
data: "name=" + name + "&email=" + email + "&subject=" + subject + "&message=" + message,
success : function(text){
if (text == "success"){
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}

function formSuccess(){
$("#contactForm")[0].reset();
submitMSG(true, "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";
}
$("#success").removeClass().addClass(msgClasses).text(msg);
}
pandy
What does the HTML look like? Do you get an error message or doesn't anything happen at all?
hpscorpion
QUOTE(pandy @ Sep 30 2020, 01:30 PM) *

What does the HTML look like? Do you get an error message or doesn't anything happen at all?


I get always message "Did you fill in the form properly?".. html code is:

<form action="#" class="form-contact" id="contactForm">
<div class="row">
<div class="col-sm-6 col-md-6">
<div class="form-group">
<input type="text" class="form-control" id="p_name" placeholder="* Nome" required="">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-sm-6 col-md-6">
<div class="form-group">
<input type="email" class="form-control" id="p_email" placeholder="* Email" required="">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-sm-6 col-md-6">
<div class="form-group">
<input type="text" class="form-control" id="p_phone" placeholder="* Telefone">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-sm-6 col-md-6">
<div class="form-group">
<input type="text" class="form-control" id="p_subject" placeholder="* Assunto">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-group">
<textarea id="p_message" class="form-control" rows="6" placeholder="* Mensagem"></textarea>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<div id="success"></div>
<button type="submit" class="btn btn-primary">ENVIAR</button>
</div>
</form>
Christian J
QUOTE(hpscorpion @ Sep 30 2020, 09:11 PM) *

I get always message "Did you fill in the form properly?"

That error message seems to come from "form-scripts.js", alas I don't do jQuery...
pandy
If you temporarily remove the JavaScript, does the form submit as it should and in that case does what's submitted look correct?

Just trying to find out if the error is with the JS/jQuery or with the form itself.
CharlesEF
It looks like it won't work without the Javascript/JQuery. The submit is done with ajax. So, the error is most likely in the JS.
hpscorpion
ohh I find the problem is my Host.. I try on free host and work perfect. no problem with code. my isp host said dont use PHP mail().. and advised me to change to framework SMTP, like PHPMailer() or SwiftMailer(). No ideia how to change this.
hpscorpion
I try this one, but still dont work i think its close. any help?

<?php
$errorMSG = "";

// NAME

if (empty($_POST["name"])) {

$errorMSG = "Name is required ";

} else {

$name = $_POST["name"];

}

// Phone

if (empty($_POST["phone"])) {

$errorMSG = "Phone is required ";

} else {

$phone = $_POST["phone"];
}


// 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"];

}

/* Contact Form Setup Begin */

$send_name = "No Name"; // Replace your name
$send_title = "ZAKAZ"; // Replace email sent title
$send_address = "email@gmail.com"; // Replace your email address

$smtp_address = "email@gmail.com"; // Replace your email GMail address
$smtp_password = "password"; // Replace your email password
$smtp_server = "smtp.gmail.com"; // Replace your email server address


/* Contact Form Setup End */

date_default_timezone_set('Etc/UTC');
require '../inc/phpmailer/PHPMailerAutoload.php';

$mail = new phpmailer(true);

try{

// $mail->SMTPDebug = 2;

$mail->isSMTP();

$mail->Host = $smtp_server;

$mail->SMTPAuth = true;

$mail->Username = $smtp_address;

$mail->Password = $smtp_password;

$mail->SMTPSecure = 'tls';

$mail->Port = 587;

// Recipients

$mail->setFrom($smtp_address, $send_title);

$mail->addAddress($send_address);

$mail->addReplyTo($send_address);

// Content

$mail->isHTML(true);

$mail->Subject = $send_title;

$Body = "";

$Body .= "Name: ";

$Body .= $name;

$Body .= "<br>";

$Body .= "Email: ";

$Body .= $email;

$Body .= "<br>";

$Body .= "Message: ";

$Body .= $message;

$Body .= "<br>";

$Body .= "Phone: ";

$Body .= $phone;

$mail->Body = $Body;

$mail->send();

echo 'Message has been send!';

} catch (Exception $e){

// echo 'Message could not be send. Error: ', $mail->ErrorInfo;

echo 'Message could not be send. Error: ';

}
?>
Christian J
The HTML form fields need NAME attributes that correspond to the PHP script's POST variables, see https://www.php.net/manual/en/tutorial.forms.php

To comment on the actual PHP script we'd also need to see the external file that's included here:

CODE
require '../inc/phpmailer/PHPMailerAutoload.php';
pandy
You are right, there are no name attributes. How could it work with the free host? ohmy.gif
CharlesEF
Also, did you fill in the required information between:
/* Contact Form Setup Begin */
*****
/* Contact Form Setup End */
CharlesEF
QUOTE(pandy @ Oct 1 2020, 11:11 PM) *

You are right, there are no name attributes. How could it work with the free host? ohmy.gif

In this case the ajax call supplies the name attribute. It is hard coded in the data array sent to the server. So it is possible that it did indeed work before.
hpscorpion
QUOTE(pandy @ Oct 1 2020, 11:11 PM) *

You are right, there are no name attributes. How could it work with the free host? ohmy.gif


yap its work. did you see ajax script?
hpscorpion
QUOTE(CharlesEF @ Oct 2 2020, 12:24 AM) *

Also, did you fill in the required information between:
/* Contact Form Setup Begin */
*****
/* Contact Form Setup End */


yes i put my information about gmail. smtp password its same right?
pandy
QUOTE(CharlesEF @ Oct 2 2020, 07:34 AM) *

QUOTE(pandy @ Oct 1 2020, 11:11 PM) *

You are right, there are no name attributes. How could it work with the free host? ohmy.gif

In this case the ajax call supplies the name attribute. It is hard coded in the data array sent to the server. So it is possible that it did indeed work before.


Ah! OK.
CharlesEF
QUOTE(hpscorpion @ Oct 2 2020, 06:18 AM) *

QUOTE(CharlesEF @ Oct 2 2020, 12:24 AM) *

Also, did you fill in the required information between:
/* Contact Form Setup Begin */
*****
/* Contact Form Setup End */


yes i put my information about gmail. smtp password its same right?

I thought so but I wanted to be sure. I don't use any PHP mail framework so I can't help.
Christian J
OT: Never heard of using an SMTP server with an email form before. Is it a good idea? unsure.gif

https://help.dreamhost.com/hc/en-us/article...-mail-via-SMTP-
https://dcodemania.com/post/send-mail-conta...iler-gmail-smtp

This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2021 Invision Power Services, Inc.