English 中文(简体)
PHP在不改用网页的情况下形成验证警报
原标题:PHP form validation alerts without redirecting page

从本质上讲,我有一套送电子邮件的网址。 沿途有几条不同的验证步骤,所有步骤都发挥了巨大作用。 如果在提交表格时出现验证错误,那就会出现 j印警报。 我与以下机构合作:

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

报警登上台,但网页上向域.com/sendemail.php转方向,使用户有一个空白页。 我真的希望在没有上下页的情况下使警报扩散。 我如何这样做?

问题回答

你们可以利用神学来实现这一目标。 但是,如果你不希望使用麻风机,而是在错误上退出,那么你可以将直线带回到形式上,同时将直线伸缩。

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

在您的表格上,如果是的话,你可以把文字带上“php”。 类似于

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

这将实现你所期待的目标。 事实上,你可以进行多种检查,并根据你的检查确定错误。 但是,我仍然建议阿贾克斯提供更好的用户经验。

Edit: Super easy solution, use jQuery form plugin : http://jquery.malsup.com/form/

在我的一些网站上,我做了类似的事情,你可能认为这样做有用。

我确实是我的验证服务器,如果我遇到错误,我就是这样做的:

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:

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

然后,我就这样做了:

  $.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 );

这是我以某种形式使用的一种文字,用来迅速验证这些文字。 我希望它能够帮助你。

<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>




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签