在HTML中验证信息 - 代码定位 [英] Validating information in HTML - Code Positioning

查看:158
本文介绍了在HTML中验证信息 - 代码定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让我的代码验证生效。我已经为名称,姓氏和电子邮件地址写过验证,但是,我不知道要在我的主html中调用的PHP代码的插入位置。



 < body>>我认为我必须在此表单中添加一个动作。 
< div class =logo>< / div>
< div class =login-block>
< h1>创建帐户< / h1>
< form action =insert_data.phpmethod =post>
< form action =validate_data.php>
< input type =textvalue =placeholder =First Namename =first_name/>
< input type =textvalue =placeholder =Last Namename =last_name/>
< input type =emailvalue =placeholder =电子邮件地址name =email_address/>

然而,我不知道这是否正确。所有三个验证笔记都保存在一个名为'validate_data.php'的文件中。



我的姓名和姓氏验证代码几乎相同,主要的'名称'空格已更改:

 <?php 
$ first_name = test_input($ _ POST [first_name]) ;
if(!preg_match(/ ^ [a-zA-Z] * $ /,$ first_name)){
$ first_nameErr =不正确的名称格式。
}
?>

和我的电子邮件地址:

 <?php 
$ email_address = test_input($ _ POST [email_address]);
if(!filter_var($ email_address,FILTER_VALIDATE_EMAIL)){
$ email_addressErr =电子邮件格式无效。;
}
?>

我有什么特别的地方可以称之为?或者我只是在做一些愚蠢的错误而忽略它?

解决方案

除非发送,否则不要将其放入HTML中该页面本身,在这种情况下,通常最好将PHP放在页面顶部。它需要命名为 .php 页面,而不是 .html 然后。例如,如果您希望将表单的值保留为所提交的值,则可以在通过设置文本框值清除后回显它们。

  value =<?php echo $ first_name;?>例如,

。如果您要提交 insert_data.php ,那么所有的PHP都只是位于该页面的顶部。您似乎有太多<表单操作 - 您只能提交一次。最好把你的清理代码放在 insert_data.php 的顶部,然后提交。

如果它打开如果(isset($ _ POST [first_name])){$ b

  $ b //清理
}

或者您将收到空输入的消息在页面加载时没有机会提交。如果您随后要将它用于邮件表单的回复地址:地址(该地址是您绝对不想留空的电子邮件地址发件人:应始终是您服务器上的地址,否则您会回到这里发帖,想知道为什么它不起作用!)



您可以包含验证脚本,但这可能有点风险,并且对于代码的长度来说,将它作为包含可能没什么优势。包含不安全的风险: http://www.webhostingtalk.com/showthread.php?t= 199419



然后,您可以使用 - 假设您的包含位于名为 inc /

  includeinc / validate_data.php; 

位于 insert_data.php 页面 - 没有在该文章中显示的括号 - 它是一个声明,而不是一个函数。



另一篇关于的好文章包括

http:// green-beast.com/blog/?p=144



例如,如果您正在循环发布帖子,则执行该操作的代码将位于其中在你想让它们出现的div内的HTML。


I am having trouble getting my code validation to work. I have written validation for a name, surname and email address, however, I don't know where to insert a command for the php code to be called in my main html.

I was thinking I have to add an action into a form like this:

<body>
    <div class="logo"></div>
    <div class="login-block">
        <h1>Create Account</h1>
        <form action="insert_data.php" method="post">
        <form action="validate_data.php">
            <input type="text" value="" placeholder="First Name" name="first_name" />
            <input type="text" value="" placeholder="Last Name" name="last_name" />
            <input type="email" value="" placeholder="E-mail Address" name="email_address" />

However, I don't know if that is correct. All three of the validation notes are saved in a file called 'validate_data.php'.

My code for name and surname validation is pretty much the same, with the main 'name' spaces changed:

<?php
$first_name = test_input($_POST["first_name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {
$first_nameErr = "Incorrect name format."; 
}
?>

and for my email:

<?php
$email_address = test_input($_POST["email_address"]);
if (!filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
    $email_addressErr = "Invalid email format."; 
}
?>

Is there any particular place I will have to call this? Or am I just doing some stupid mistake and missing it?

解决方案

You don't put it in the HTML unless you are sending the page to itself, in which case it is usually best to put the PHP at the top of the page. It would need to be named as a .php page not .html then. That way if, for example, you wanted to make the values of the form stay as what was submitted you could echo them after they are cleaned up by setting the textbox value

 value="<?php echo $first_name; ?>"

for example. If you are submitting to insert_data.php all the PHP just lives on that page at the top. You seem to have too many <form actions - you can only submit once. Best to put your cleanup code at the top of insert_data.php and submit to that.

If it is on the same page you would need to wrap it in

    if( isset($_POST["first_name"])){
     // do the cleanup
    }

or you will get messages for the empty inputs which have not had the chance to be submitted as the page loads. And do the same for the email address which you definitely don't want to have blank if you are subsequently going to use it for a mail form's Reply-To: address (the From: should always be an address on your server or you will be back here posting wondering why it doesn't work!)

You could include the validation script but that is possibly a bit risky and for the length of the code there is probably little advantage in having it as an include. Risks of including unsafely: http://www.webhostingtalk.com/showthread.php?t=199419

You would then use - assuming your includes are in a folder called inc/

include "inc/validate_data.php";

at the top of your insert_data.php page - without the brackets shown in that article - it is a declaration, not a function.

Another good article on includes:

http://green-beast.com/blog/?p=144

If you were looping out posts, for example, the code to do that would be somewhere among the HTML inside the div where you wanted them to appear.

这篇关于在HTML中验证信息 - 代码定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆