The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Submit form dont work
hpscorpion
post Sep 30 2020, 12:34 PM
Post #1





Group: Members
Posts: 6
Joined: 30-September 20
Member No.: 27,566



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);
}
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Sep 30 2020, 01:30 PM
Post #2


Computer says no.
********

Group: WDG Moderators
Posts: 19,182
Joined: 9-August 06
Member No.: 6



What does the HTML look like? Do you get an error message or doesn't anything happen at all?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
hpscorpion
post Sep 30 2020, 02:11 PM
Post #3





Group: Members
Posts: 6
Joined: 30-September 20
Member No.: 27,566



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>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Sep 30 2020, 07:15 PM
Post #4


.
********

Group: WDG Moderators
Posts: 8,584
Joined: 10-August 06
Member No.: 7



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...
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Sep 30 2020, 10:31 PM
Post #5


Computer says no.
********

Group: WDG Moderators
Posts: 19,182
Joined: 9-August 06
Member No.: 6



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Oct 1 2020, 12:10 AM
Post #6


Programming Fanatic
********

Group: Members
Posts: 1,786
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



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.

This post has been edited by CharlesEF: Oct 1 2020, 12:11 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
hpscorpion
post Oct 1 2020, 06:57 AM
Post #7





Group: Members
Posts: 6
Joined: 30-September 20
Member No.: 27,566



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
hpscorpion
post Oct 1 2020, 09:01 AM
Post #8





Group: Members
Posts: 6
Joined: 30-September 20
Member No.: 27,566



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: ';

}
?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Oct 1 2020, 04:56 PM
Post #9


.
********

Group: WDG Moderators
Posts: 8,584
Joined: 10-August 06
Member No.: 7



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';
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Oct 1 2020, 11:11 PM
Post #10


Computer says no.
********

Group: WDG Moderators
Posts: 19,182
Joined: 9-August 06
Member No.: 6



You are right, there are no name attributes. How could it work with the free host? ohmy.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Oct 2 2020, 12:24 AM
Post #11


Programming Fanatic
********

Group: Members
Posts: 1,786
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



Also, did you fill in the required information between:
/* Contact Form Setup Begin */
*****
/* Contact Form Setup End */
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Oct 2 2020, 12:34 AM
Post #12


Programming Fanatic
********

Group: Members
Posts: 1,786
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
hpscorpion
post Oct 2 2020, 06:17 AM
Post #13





Group: Members
Posts: 6
Joined: 30-September 20
Member No.: 27,566



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?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
hpscorpion
post Oct 2 2020, 06:18 AM
Post #14





Group: Members
Posts: 6
Joined: 30-September 20
Member No.: 27,566



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?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Oct 2 2020, 01:56 PM
Post #15


Computer says no.
********

Group: WDG Moderators
Posts: 19,182
Joined: 9-August 06
Member No.: 6



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Oct 2 2020, 09:30 PM
Post #16


Programming Fanatic
********

Group: Members
Posts: 1,786
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Oct 3 2020, 06:32 AM
Post #17


.
********

Group: WDG Moderators
Posts: 8,584
Joined: 10-August 06
Member No.: 7



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

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 24th October 2020 - 07:57 AM