I'm using radio buttons to display my form's textbox. I added a PHP script to validate the email and on completion, open another page. But nothing seems to be working. It shows the error message for when the textbox is empty, even when the submit button hasn't been pressed, and it refreshes the page if the email format isn't correct.
Here's my code:
<?php
// define variables and set to empty values
$emailErr = "";
$email = "";
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";
}
else{
header("Location:create.php")
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
function ShowHideDiv() { var chkYes1 = document.getElementById("chkYes1"); var dvtext1 = document.getElementById("dvtext1"); var chkYes2 = document.getElementById("chkYes2"); var dvtext2 = document.getElementById("dvtext2"); var button = document.getElementById("button"); dvtext1.style.display = chkYes1.checked || chkYes2.checked ? "block" : "none"; dvtext2.style.display = chkYes2.checked ? "block" : "none"; btn1.style.display = chkYes1.checked ? "block" : "none"; btn2.style.display = chkYes2.checked ? "block" : "none"; }
<div class="content login-container"> <div class="form-login"> <label for="chkYes1"> <input type="radio" id="chkYes1" name="chk" onclick="ShowHideDiv()" /> I am new on Swadonline </label> <br> <br> <div id="dvtext1" style="display: none" class="lgntxt"> Please enter your Email Address * <br> <input type="text" id="txtBox" name="email" /> </div> <button style="display: none" id="btn1" class="btn third" type="submit" name="submit" value="Submit">CONTINUE</button> </div> </div>
In html change this line
<button style="display: none" id="btn1" class="btn third" type="submit" name="submit" value="Submit">CONTINUE</button>
to
<input type="submit" style="display: none" id="btn1" class="btn third" name="my_form_submit" value="CONTINUE">
Now in your php change as below:
<?php
// define variables and set to empty values
$emailErr = "";
$email = "";
if(isset($_POST['my_form_submit'])){ // only check validation if form has been submitted, else don't validate
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";
}
else{
header("Location:create.php");
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}
?>
I've taken this route for form submissions once upon a time, since then I would highly recommend setting up PHPMailer for any project.
Hope this in any way helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.