我使用单选按钮显示表单的文本框。我添加了一个PHP脚本来验证电子邮件,并在完成后打开另一个页面。但似乎没有任何效果。它显示文本框为空时的错误消息,即使没有按下提交按钮,如果电子邮件格式不正确,也会刷新页面。
这是我的代码:
<?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";
}
&#13;
<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>
&#13;
答案 0 :(得分:0)
在html中更改此行
<button style="display: none" id="btn1" class="btn third" type="submit" name="submit" value="Submit">CONTINUE</button>
到
<input type="submit" style="display: none" id="btn1" class="btn third" name="my_form_submit" value="CONTINUE">
现在你的php更改如下:
<?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;
}
}
?>
答案 1 :(得分:0)