Skip to main content
Known Participant
July 6, 2020
Answered

Help with PHP code for form with date & time variables

  • July 6, 2020
  • 2 replies
  • 1146 views

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 has been closed for replies.
Correct answer osgood_

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

2 replies

Legend
July 6, 2020

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

Legend
July 6, 2020

Thanks moderator - I'm guessing Nancy.

Legend
July 6, 2020

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);

Known Participant
July 6, 2020

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....

Legend
July 6, 2020

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.