<div id="form_container">
<h1 align="justify"><font color="#4074BF">Reviews and Questions</font></h1>
<form action="send_email.php" method="post">
<p><font color="#4074BF">Question or Review<br />
<textarea cols="50" id="message" name="message" rows="5" required>Ask a Question or Submit a Review. </textarea></font></p>
<div class="inline_style">
<p><font color="#4074BF">Please type this number in the space below </font><span id="_challenge"></span></p>
<p><font color="#4074BF"><input id="testphrase" name="testphrase" size="10" type="text"/></font></p>
<input type="hidden" id="challenge" name="challenge" value="<?=$_challenge?>" type="text" />
<br />
</div>
<p><font color="#4074BF">
<input id="submit" type="submit" value="Send" />
<input id="reset" name="reset" type="reset" value="Clear" />
</font>
</p>
<br />
</form>
</div>
<?php
// send_email.php
$errors = [];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get POST data
$challenge = isset($_POST['challenge']) ? strip_tags(trim($_POST['challenge'])) : '';
$testphrase = isset($_POST['testphrase']) ? strip_tags(trim($_POST['testphrase'])) : '';
$message = isset($_POST['message']) ? strip_tags(trim($_POST['message'])) : '';
// Validate form fields
if ($challenge != $testphrase) {
echo "testphrase = $testphrase<br>";
echo "challenge = $challenge<br>";
exit();
}
if (empty($email)) {
$errors[] = 'Email is empty';
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Email is invalid';
}
// If no errors, send email
if (empty($errors)) {
$recipient = "[email protected]";
$subject = "Website Question / Review";
$message = "From: $name <$email> rn$message";
$message = wordwrap($message,70);
// Send email
if (mail($recipient, $subject, $message, $headers)) {
// Destroy data _POST
foreach ($_POST as $key => $value) {
unset($_POST[$key]);
}
// redirect user and terminate the script
header("Location: thank_you.html");
exit();
} else {
echo "Failed to send email. Please try again later.";
}
} else {
// Display errors
echo "The form contains the following errors:<br>";
foreach ($errors as $error) {
echo "- $error<br>";
echo "$snotmail<br>";
echo "$heard<br>";
}
}
} else {
// Not a POST request, display a 403 forbidden error
header("HTTP/1.1 403 Forbidden");
echo "You are not allowed to access this page.";
}
?>
window.onload = function() {
var d = new Date();
var seconds = d.getSeconds();
var thisday = d.getDay();
var rnumber = thisday + "" + seconds;
document.getElementById("_challenge").textContent = rnumber;
}
;
I have posted the form, php and javascript that I am using above. I am successfully assigning a value to the span id “_challenge” in javascript. Span displays the value as expected. I am then trying to pass that value to php using
<input type="hidden" id="challenge" name="challenge" value="<?=$_challenge?>" type="text" />
but it keeps showing as empty when I use echo in php. If I assign a constant value for example value=”240″ that is passed properly the echo statement displays it.
Is there something wrong with the syntax of my variable assignment to the value attribute or is there another method to pass this value which is assigned in the script to _challenge?
Thanks.
I have tried many different methods that are described in SO but all have yielded the same empty value.