I apologise in advanced if there is an answer to this already. I have looked and I simply can't make head nor tail of answers I have looked at so please... be gentle.
I am really new to PHP and not exactly fluent with Javascript. Essentially I am just building a website for my business and what I would like to do is have a lead form on the homepage. The goal being to securely take a client's e-mail address and redirect them to the booking page with their e-mail appended to the url.
ie, the customer enters their email address on the homepage at
www.hygenius.co
and when they click submit it validates that they've entered a real address and that the field is not blank then takes them to
www.hygenius.co/book?email=customersemail
However, for the life of me, I can't seem to get anything I have tried to work.
So far my form looks like this;
<form action="lead.php" method="POST">
<input type="text" name="email" placeholder="Enter a valid E-mail Address">
<span class="error">* <?php echo $emailErr;?></span>
<input type="submit" name="submit" value="Submit">
</form>
My lead.php file looks like this.
<?php
// define variables and set to empty values
$emailErr = "";
$email = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
header( 'Location: http://www.hygenius.co/book.html' ) ;
?>
I haven't even manged to get the e-mail validation to work so far, I was trying to follow the W3schools guidance but nothing has worked. It simply allows you to click submit and go straight to the booking page without any validation and no error message appears either. I have no idea how to get to the stage where I can append information to the URL either.
I am sure I am just being incredibly dense here, coding is not my forte at all but any guidance as to what I can do to make this work and where I am going wrong would be incredibly appreciated.
Warm regards,
Jonathan Crow.