我正在尝试制作一个"文本"类型的"表单",它将检查电子邮件是否有效。我首先尝试通过制作 if 语句来检查文本字段是否为空,但它不起作用,而是运行语句的"else"选项。如何修复它,以便它实际检查文本字段是否为空?这是代码。
索引.php:
<?php
include 'connection.php';
echo "<p> Example Text </p>";
?>
<h1> Submit email address </h1>
<form action = "create.php" method = "post">
<input type ="text" name = "email" value = "" />
<br />
<input type = "submit" name = "submit" />
</form>
创建.php :
<?php
include 'connection.php';
$email = $_POST['email'];
if(!isset($email)){
echo "please fill out the form";
header ('Location: index.php');
}
else{
echo "Successive login";
}
?>
执行时:
$email = $_POST['email'];
您实际上是在设置$email
因此!isset()
将返回false
(这意味着它不是不是 isset)
相反,您需要检查它是否不为空
if(!empty($email)){
}
else{
}
====
============额外以下是如何检查有效电子邮件地址的示例:
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
echo("$email is a valid email address");
} else {
echo("$email is not a valid email address");
}
另请查看我的PHP函数,该函数检查电子邮件是否有效,并且可以通过连接到邮件服务器并对其进行验证来接收电子邮件:
https://github.com/hbattat/verifyEmail
我做到了。当我删除标题代码时,它也确实有效,但是我 希望回显显示在索引中.php而不是在新页面中 (创建.php)。我该怎么做?– 汤姆浪涌 3 小时前
当您提交到create.php
时,基本上您会使用一些帖子参数加载create.php
页面。因此,您在此处显示的任何内容都不会显示在index.php
中,因为您不在同一页面中。
相反,您可以index.php
发布到同一页面并在那里执行创建过程:
<?php
include 'connection.php';
if($_SERVER['REQUEST_METHOD'] == 'post'){ //check if the page was requested via post method (that means the form was submitted)
if(!isset($email)){
echo "please fill out the form";
}
else{
echo "Successive login";
}
}
echo "<p> Example Text </p>";
?>
<h1> Submit email address </h1>
<form action = "index.php" method = "post">
<input type ="text" name = "email" value = "" />
<br />
<input type = "submit" name = "submit" />
</form>
或
您可以使用 AJAX 并且在将信息发布到create.php
时不加载页面,而只显示响应消息:
<?php
include 'connection.php';
echo "<p> Example Text </p>";
?>
<!-- include jQuery in the header of your html -->
<script src="LINK TO jQUERY .js file"></script>
<script type="text/javascript">
$(document).ready(function(){
//trigger this when the form is submitted
$('form').on('submit', function(e){
//prevent default submission and reload of the page
e.preventDefault();
//post the date to the create.php file using ajax
//get the form data
var formData = $(this).serialize();
$.post('create.php', fomrData, function(response){
var result = parseJSON(response);
$('#msg').text(result.msg);
}
});
});
});
</script>
<h1> Submit email address </h1>
<form action = "create.php" method = "post">
<input type ="text" name = "email" value = "" />
<br />
<input type = "submit" name = "submit" />
</form>
创建.php
<?php
include 'connection.php';
$email = $_POST['email'];
$result = array();
if(!isset($email)){
$result['status'] = 'fail';
$result['msg'] = "please fill out the form";
}
else{
$result['status'] = 'success';
$result['msg'] = "Successive login";
}
//echo the json
echo json_encode($result);
?>
isset()
只检查变量是否已设置。您需要改用empty($email)
。
将isset
替换为empty
,它应该可以根据需要工作。
如果我理解您的问题,您必须将语句更改为if(!empty($email))
另外,如果您想检查电子邮件的格式是否正确,请使用正则表达式来执行此操作。 isset() 只检查空
您还可以使用以下方法测试在已发布的电子邮件字段中提交某些内容(任何内容):
'' == $_POST['email']
正如其他人所建议的那样,empty($_POST['email'])
也有效,只是显示了另一种选择。
显示错误消息
如果希望错误消息显示在索引上.php则需要将消息或某种标志从创建.php页(发现错误的位置)传递到索引.php页(显示错误消息的位置)。
为了演示一个基本示例(实现此目的的众多方法之一),首先我在表单中添加了一个 PHP 变量。请注意紧跟在电子邮件输入元素之后包含echo $_GET['error'];
的行:
索引.php (窗体):
<form action="create.php" method="post">
<input type="text" name="email" value="" />
<?php if (isset($_GET['error'])) { echo $_GET['error']; } ?>
<br />
<input type="submit" name="submit" />
</form>
。并且,不要在创建.php脚本中echo
消息,而是将消息一起发送到 URL 查询中的索引.php脚本:
创建.php(电子邮件检查):
if ( '' == $_POST['email'] ) {
header('Location: index.php?error=please+fill+out+the+form');
}
此外,我建议您使用此(PHP 服务器端验证)作为 Javascript 客户端验证解决方案的备份。对您的网站访问者更好,使用更少的服务器资源。
另一种方法是在输入中添加required=""
下面是一个示例 https://jsfiddle.net/57s38s2e/