PHP中的HTML表单验证。
我对php是全新的,在对它进行研究之后,我仍然无法找到合适的答案。当我试图用php验证我的表单,并用CSS定制错误消息时 我的HTML代码是:PHP中的HTML表单验证。,php,html,css,Php,Html,Css,我对php是全新的,在对它进行研究之后,我仍然无法找到合适的答案。当我试图用php验证我的表单,并用CSS定制错误消息时 我的HTML代码是: <div class="container"> <form action="/action_page.php"> <div class="flex-row"> <input type="text" id="name" name="name" placeholder="Name">
<div class="container">
<form action="/action_page.php">
<div class="flex-row">
<input type="text" id="name" name="name" placeholder="Name">
<input type="email" id="email" name="email" placeholder="Email Address">
<input type="number" id="age" name="age" placeholder="Age">
</div>
<div class="flex-row">
<input type="text" id="subject" name="subject" placeholder="Subject">
<select type="dropdown" name="contact" placeholder="Age">
<option placeholder="australia">Australia</option>
<option value="canada">Canada</option>
<option value="usa">USA</option>
</select>
</div>
<div class="flex-row">
<textarea id="message" name="message" placeholder="Talk To Us" style="height:200px"></textarea>
</div>
<input type="submit" value="Submit">
</form>
澳大利亚
加拿大
美国
我的CSS在下面的粘贴栏中:
另外,我还考虑将for验证代码放在一个单独的文件中,该文件将保存到includes文件夹中。php脚本中的数据将通过$\u GET全局变量作为数组提供。每个输入都是此数组中的一项,例如$\u GET[“name”]。 脚本
action\u page.php
可能看起来像这样(这只是一个开始…):
你能把代码贴在action\u page.php
上吗?我只是把它们放在上面,所以我知道它们是什么。实际上我还没有一个文件叫它,因为我不知道从哪里开始表单验证或处理你的问题太广泛了。遵循一些PHP教程并返回一个更具体的问题。未定义的变量,后跟文件的位置。这不是错误,而是来自PHP的通知。您需要在php.ini中将error_reporting设置为不太严格的值(在Linux上它位于/etc/php/7.2/-在windows上,您需要搜索internet以找到它所在的位置)。@KyleLogan$error需要设置为文件顶部的空字符串,否则如果不满足这两个条件,$error尚未初始化。
<?php
//Test the input, for example like this:
if(empty($_GET["name"])) $error="You need to provide a name";
if(strpos($_GET["email"],"@")==0) $error="The e-mail is not valid"; //Of course you could test for a lot more, for example using regular expressions
//Now you can build your response to the user
if($error) {
echo ("<h1>Error</h1>".$error);
}
include("form.html"); //If you want to show the same form again.