Check if input field value exist in array, if so return to page with an error

前端 未结 2 515
终归单人心
终归单人心 2021-01-27 04:21

I have a form where user insert their names, once they submit the form, the page should check if the name provided already exists, if so, should return with an error, other wise

2条回答
  •  走了就别回头了
    2021-01-27 05:15

    I got it from your comments that you have the following:

    $_POST["user-submitted-name"]; // name from the form
    $names = get_meta_values('user_submit_name'); // array with names not allowed
    

    Then it looks like the easy way:

    if (in_array($_POST["user-submitted-name"], $names)) {
        // posting not allowed
        echo '
  • Name already used'; } else { // posting is allowed // save posting here... // and then go to success page: header("Location: success.html"); // redirect exit(); // and stop here }
提交回复
热议问题