简体   繁体   English

PHP表格验证,过帐

[英]Php form validation, Post

Here is my header for my form. 这是表格的标题。

<form action="/page" id="landing_form" method="post" style="top:<?php echo get_post_meta($p->ID, 'top', true); ?>px; left:<?php echo get_post_meta($p->ID, 'left', true); ?>px;"  >

And a simple text field within my form, and submit button 在我的表单中有一个简单的文本字段,然后提交按钮

<label>Email</label><input type="text" value="" id="landing_email" name="email" >
<input type="submit" name="submit" value="<?php echo get_post_meta($p->ID, 'button', true); ?>" class="landing_submit" id="landing_submit">

Now how would I make a function that checks if the email has a "."/"@" in it? 现在,我将如何创建一个函数来检查电子邮件中是否包含“。” /“ @”? Or is a specific string. 还是一个特定的字符串。

I have tried in my header to put something like onSubmit="return function('email);" 我已尝试在标头中放置类似onSubmit="return function('email);" and then putting the function within the file. 然后将函数放入文件中。

However it doesn't even check the function, and just submits the form regardless. 但是,它甚至不检查功能,而只是提交表单。

Assuming you want client-side validation you will have to create a javascript function like in this post 假设你想客户端验证,你必须创建一个JavaScript函数就像这个岗位

Therefore you will need your method: 因此,您将需要您的方法:

<script>
function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
} 
function validate(email) {
    if (!validateEmail(email)) {
        return false;
    }

}
</script>

Then on the form you insert: 然后在表格上插入:

onSubmit="validate(email);"

This should not allow the form submission if the email is not valid. 如果电子邮件无效,则不应提交表单。 Please be aware that you will have to validate the email on the server side also. 请注意,您还必须在服务器端验证电子邮件。

the "onSubmit" attribute calls a piece of javascript when the submit button is pressed. 当按下“提交”按钮时, “ onSubmit”属性将调用一段javascript。 It doesn't interact with your PHP at all. 它根本不与您的PHP交互。

What you'll probably want in your "page" page is something like the following: 您可能需要在“页面”页面中执行以下操作:

<?php 
    if(isset($_POST['submit']))
    {
        $email = $_POST['email'];
        if(strpos($email, '@') && strpos($email, '.') && strpos($email, 'some specific string'))
        {
            // This code executes
            // only if there is an @
            // and a full stop and "some specific string"
            // inside the email
        }
    }
?>

Strpos is a function that searches for a substring within a string and returns the position of its first occurrence. Strpos是一个函数,它在字符串中搜索子字符串并返回其第一次出现的位置。 If it cannot find the substring, it returns false. 如果找不到子字符串,则返回false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM