简体   繁体   English

仅当表单通过错误检查时,如何才能在表单提交时转到新页面?

[英]How to go to new page on form submit only if form passes error checks?

I have an HTML form with PHP error checks.我有一个带有 PHP 错误检查的 HTML 表单。 The form has several different types of fields (one for name, email, phone number, a checkbox, a drop down, etc.) and I have a PHP function written for each that runs when you hit the submit button, and checks that the form is filled in correctly and fully.表单有几种不同类型的字段(一个用于姓名、电子邮件、电话号码、复选框、下拉列表等),我为每个字段编写了一个 PHP 函数,当您点击提交按钮时运行,并检查表格填写正确且完整。 If a field is left empty, or is filled in incorrectly, an error message appears.如果某个字段留空或填写不正确,则会出现错误消息。 However, after I got that running, I tried to add a redirect, so that after the form is completed and submit is pressed, it brings the user to a confirmation page, if the errorchecks are passed.但是,在我运行之后,我尝试添加一个重定向,以便在表单完成并按下提交后,如果错误检查通过,它会将用户带到确认页面。 I wrote it like this:我是这样写的:

if(isset($_POST['submit']) ){                                                                                                                                                    
  header("Location:confirmed.php");}

It does what it's supposed to--bring the user to a new page--but it doesn't take into consideration any errorchecks.它会做它应该做的事情——将用户带到一个新页面——但它没有考虑任何错误检查。 So, when submit is pressed, rather than run through the error checks, it immediately goes to the new page.因此,当按下提交时,它不会运行错误检查,而是立即转到新页面。 I tried adding a variable named "errorcount" so each function so that the number of errors that occur when the form is submit will be either counted, or removed from the count, and then considered when changing the page...我尝试添加一个名为“errorcount”的变量,以便每个函数都可以计算提交表单时发生的错误数,或者从计数中删除,然后在更改页面时考虑...

if(isset($_POST['submit']) ){ 
  if ($errorcount == 0){                                                                                                                                                   
    header("Location:confirmed.php");}}

This didn't work either.这也不起作用。 I realized that $errorcount wasn't actually being updated at all;我意识到 $errorcount 实际上根本没有被更新; for what reason, I'm not sure.出于什么原因,我不确定。 I set it up so each function returns $errorcount, and then called each function in the snippet of code above before running the second if statement, but it still does nothing.我将它设置为每个函数都返回 $errorcount,然后在运行第二个 if 语句之前调用上面代码片段中的每个函数,但它仍然什么都不做。

I feel like I'm approaching this the wrong way, but I'm not really sure how else to do this.我觉得我以错误的方式接近这个问题,但我不确定如何做到这一点。 Please tell me if there's an easier way to achieve this, or maybe you have an idea what I'm doing wrong in the first place.请告诉我是否有更简单的方法来实现这一目标,或者您首先知道我做错了什么。

EDIT: I am passing the variable $errorcount as global in each function, like so:编辑:我在每个函数中将变量 $errorcount 作为全局传递,如下所示:

function validateName ($name, $submit){
  global $errorcount;
  if( empty( $submit )) {
    return '';}
  if (empty ($name)){
    return "You didn't enter your name!";
    $errorcount = $errorcount+1;
    }
  if (!preg_match("/^[a-zA-Z \-]*$/",$name, $matches)){
    return "Please enter a valid name";
    $errorcount = $errorcount+1;
  }
  else{
    $errorcount = $errorcount-1;
  }
  return $errorcount;
}

However, $errorcount still does not actually change with the if loop I posted above.然而, $errorcount 实际上仍然没有随着我上面发布的if循环而改变。 If I take that out (the section of code that causes the page to change) then the functions work as intended;如果我将其删除(导致页面更改的代码部分),则功能会按预期工作; once you click submit, the page refreshes, and error messages appear where the user did not fill out the form properly.单击提交后,页面将刷新,并在用户未正确填写表单的地方显示错误消息。 But once all the form areas are filled out properly, clicking submit does... nothing.但是一旦正确填写了所有表单区域,单击提交就可以了……什么也没有。

EDIT 2:编辑2:

I got it working.我让它工作了。 It's honestly not very efficient but it does what I need it to do.老实说,它的效率不是很高,但它可以完成我需要它做的事情。 Thanks to all who helped!感谢所有帮助过的人!

You don't really need to count the errors, and you don't need to use global .您实际上不需要计算错误,也不需要使用global Just write your validator functions so they return an error message if there is an error, or nothing if there is no error.只需编写您的验证器函数,以便它们在出现错误时返回错误消息,或者在没有错误时返回任何内容。 Like this, for example:像这样,例如:

function validateName($name) {
    if (!$name) {
        return 'name is required';
    }
    if (!preg_match("/^[a-zA-Z \-]*$/", $name)) {
        return "Please enter a valid name";
    }
}

Then when you run your validators, add any error messages you get to an array.然后当你运行你的验证器时,将你得到的任何错误消息添加到一个数组中。

if ($error = validateName($_POST['name'] ?? '')) {
    $errors['name'] = $error;
}

After you run all the validators, if the error array is empty, then there were no errors so you can redirect.运行所有验证器后,如果错误数组为空,则表示没有错误,因此您可以重定向。 And if it's not empty, then you have an array of errors keyed by field name, so you can display any errors next to the problematic fields, which your users will prefer rather than getting one error at a time in some generic location.如果它不为空,那么您有一组按字段名称键入的错误,因此您可以在有问题的字段旁边显示任何错误,您的用户更喜欢这样,而不是在某个通用位置一次得到一个错误。

if (empty($errors)) {
    // redirect
} else {
    // stay here and show the errors
}

You're needlessly complicating things.你不必要地把事情复杂化了。 Defining functions are only useful if you plan on re-using that code elsewhere.仅当您计划在其他地方重用该代码时,定义函数才有用。 Try this instead:试试这个:

if ( isset($_POST['submit']) )
{
    if ( empty($name) )
    {
        echo "You didn't enter a name!";
        sleep 3;
        // redirect or re-load the form
    }
    elseif ( !preg_match("/^[a-zA-Z \-]*$/", $name, $matches) )
    {
        echo "Enter a valid name!";
        sleep 3;
        // redirect or re-load the form
    }
    header("Location:confirmed.php");
}

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

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