Skip to main content
Known Participant
Jul 6, 2020 7:48 AM UTC
Answered

Help with PHP code for form with date & time variables

Hello, we have got an online form here, pretty simple one that needs a date and time filled in, it's based on an even simpler contact form that uses Captcha V3 and works fine – https://bit.ly/2NYwom7

 

when we fill in the form it just produces a page with "Error!" on it, as stated in the PHP code, which is as folows 

 

<?php

//set recaptcha url/secret-key/response
$recapture_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = 'xxxxxxxxxxxxxxxxxxxxx';
$recaptcha_response = $_POST['token'];
//send information to recaptcha url
$recaptcha = file_get_contents($recapture_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
$recaptcha = json_decode($recaptcha);
//check if recaptcha has been successful
if($recaptcha->success==true) {
if($recaptcha->score >= 0.1) {
// if recaptcha is successful and score is equal or more than 0.1 process the form information



$name = htmlspecialchars($_POST['name']);
$telephone = htmlspecialchars($_POST['telephone']);
$visitdate = htmlspecialchars($_POST['visitdate']);
$arrivaltime = htmlspecialchars($_POST['arrivaltime']);
$departuretime = htmlspecialchars($_POST['departuretime']);


if(empty($name) || empty($telephone) || empty($visitdate) || empty($arrivaltime) || empty($departuretime)) {
$error = true;
$status = "Failed";
}

// if no form errors are set then send the form information to the email address
if(!isset($error)) {
$formcontent=" From: $name \n telephone: $telephone \n visitdate: $visitdate \n arrivaltime: $arrivaltime \n departuretime: $departuretime";
$recipient = "e-mail removed by moderator";
$subject = "Covid 19 Contact Form";
$mailheader = "From: $name \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
$status = "Successful";
}


}
// if recaptcha fails set a status
else {
$status = "Failed";
}
}
// if status is successful then include the contact_sent.html page
if($status === "Successful") {
include('pages/covidform-sent.html');
}
else {
// if status is failed then include the contact_failed.html page
include('pages/covidform-failed.html');
}
?>

 

 

best guess is that part of the form that deals with the responses 

 

$name = htmlspecialchars($_POST['name']);
$telephone = htmlspecialchars($_POST['telephone']);
$visitdate = htmlspecialchars($_POST['visitdate']);
$arrivaltime = htmlspecialchars($_POST['arrivaltime']);
$departuretime = htmlspecialchars($_POST['departuretime']);

 

needs amending to take care of the date and time variables?

 

Thanks for any assistance!

This topic is closed to new replies. Start a new post to keep the conversation going.
osgood_Correct answer
Legend
Jul 7, 2020 10:10 AM UTC

It is albeit with a caveat, that you can leave some of the boxes, or one box, empty and it's still processing the form and sending it, when it should redirect to the error page and not send the form


That should not be happening and would not be associated with the $mailheader assuming you are now using the original script you posted.

 

The block of code below should not run IF any of the required input fields are left empty.

 

if(!isset($error)) {
$formcontent=" From: $name \n telephone: $telephone \n visitdate: $visitdate \n arrivaltime: $arrivaltime \n departuretime: $departuretime";
$recipient = "xxxxxxxxxxxxxxxxxxxxxxxxx";
$subject = "Covid 19 Contact Form";
$mailheader = "From: server@yourDomainName.co.uk \r\n";
mail($recipient, $subject, $formcontent, $mailheader);
$status = "Successful";
}

21 replies

Legend
Jul 6, 2020 5:47 PM UTC

Can a forum moderator remove the email address from the initial code posted - $recipient = "gxtixtoxch@mxxlxnsxakxrycxfx.cx.ux";

 

It looks like a live email address which unless removed or scrambled will attract spam.

 

Thanks

 

Os

Translate
Legend
Jul 6, 2020 6:10 PM UTC

Thanks moderator - I'm guessing Nancy.

Translate
Legend
Jul 6, 2020 11:31 AM UTC

Remove 'or die ("Error!")' and see if that results in a no error response:

 

So change this:

mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

 

To this:

mail($recipient, $subject, $formcontent, $mailheader);

Translate
Known Participant
Jul 6, 2020 4:43 PM UTC

Thanks for the reply  – that now works, in that it does re-direct to the success page (it also correctly identifies when the form is incorrectly filled in), but the email isn't being generated or sent as it should do for soem reason, they're not going into a spam folder either....

Translate
Legend
Jul 6, 2020 5:39 PM UTC

Have you checked your remote server supports the 'php mail function' plus you need to test the form by uploading it to the remote server, it probably won't send if you are testing on a local machine.

Translate