简体   繁体   English

PHP表单验证警报,无需重定向页面

[英]PHP form validation alerts without redirecting page

Basically whats happening is I have a php form to send an email. 基本上发生了什么事,我有一个php表格发送电子邮件。 There are several different validation steps all along the way, which all work great. 整个过程中有几个不同的验证步骤,都非常有效。 What i'd like to do is have a javascript alert pop up if there is a validation error when the form is submitted. 我想做的是,如果提交表单时出现验证错误,则会弹出一个javascript警报。 I have this working with the following php: 我使用以下php进行工作:

// Validate email
if(!filter_var($EmailFrom, FILTER_VALIDATE_EMAIL))
{
  echo "<script language=javascript>alert('Please Use a Valid Email Address')</script>";
  exit;
}

The alert pops up but the page redirects to domain.com/sendemail.php which leaves the user with a blank page. 警报弹出,但页面重定向到domain.com/sendemail.php,使用户留有空白页面。 I'd really like to have the alerts pop up without reloading the page. 我真的很想在不重新加载页面的情况下弹出警报。 How would I do this? 我该怎么做?

This is a script I used in some forms I created to validate them fast. 这是我以某种形式创建的脚本,用于快速验证它们。 It's very simple and effective, I hope it helps you. 它非常简单有效,希望对您有所帮助。

<script type="text/javascript">
function validate(){ 
    emailfilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    condition = 1;
    mensaje = "Complete: ";
    //Validate 1st input (Check if empty)
    if (document.formname.forminput1.value.length==0){ 
         condition = 0;
         msg = msg + "-Input 1 is empty "
    }
        //Validate 1nd input (Check email)
    if (!emailfilter.test(document.formname.forminput1.value)) {
         condition = 0;
         msg = msg + "-Input 1 has a invalid email adresss "
    }

    if (condition == 0){ 
         alert(msg) 
         document.formname.forminput1.focus() 
         return 0; 
    }

    //send
    alert("Form sended."); 
    document.formname.submit(); 
} 
</script>

You can use ajax to accomplish this. 您可以使用ajax完成此操作。 But if you don't want to use ajax, instead doing an exit on error, you could redirect back to the form page along with a query string parameter. 但是,如果您不想使用ajax,而是在出错时退出,则可以将其与查询字符串参数一起重定向回表单页面。

header("Location: http://www.example.com/form.php?error=1");

And on your form page you could put the script withing php if. 在您的表单页面上,您可以将脚本与php一起使用。 Something like 就像是

<?php if(isset($_GET['error']) && $_GET['error']==1): ?>
<script>
....
</script>
<?php endif; ?>

That would achieve what you are looking for. 那将实现您想要的。 In fact you can perform multiple checks and set error based on your checks. 实际上,您可以执行多项检查并根据检查设置错误。 But I would still suggest Ajax will give a better user experience. 但是我仍然建议Ajax将提供更好的用户体验。

Edit: Super easy solution, use jQuery form plugin : http://jquery.malsup.com/form/ 编辑:超级简单的解决方案,使用jQuery窗体插件: http : //jquery.malsup.com/form/

I do something similar in some of my web apps, you might find it useful. 我在某些Web应用程序中做了类似的操作,您可能会发现它很有用。

I do my validation server side and if I encounter an error I do this : 我在验证服务器端进行操作,如果遇到错误,请执行以下操作:

json_die(array(
  'status' => 'error',
  'message'=> 'Your error message'
));

and for success : 为了成功:

json_die(array(
  'status' => 'success',
  'message'=> 'Your success message'
));

The json_die is function is : json_die函数是:

function json_die($array) {
        header("content-type: application/json");
        die(json_encode($array, true));
}

Then on the front end I do something like this: 然后在前端我做这样的事情:

  $.post('/your_url', {
                        'your': vars

                    }, function (r) {

                      if(r.status == 'success') {

                           alert(r.message);

                        } else if (r.status == 'error') {

                            alert(r.message);
                             //handle error

                        } else {
                           alert('server exploded / no connection');
                         }

                    },'json');

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

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