检查数组中是否存在输入字段值,如果存在则返回错误页面 [英] Check if input field value exist in array, if so return to page with an error

查看:24
本文介绍了检查数组中是否存在输入字段值,如果存在则返回错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,用户在其中插入他们的姓名,一旦他们提交表单,页面应该检查提供的名称是否已经存在,如果存在,应该返回错误,否则,正常进行.

如何检查我的输入值是否存在并在存在时停止表单?

 0){//将键添加到临时数组中,使其不再被找到:$temp[] = $key;//做点什么...echo '
  • 名称已被使用';}}}?>
  • html

    <div class="form-group"><label class="col-lg-2 control-label" for="user-submitted-name">Il tuo nome</label><div class="col-lg-10"><input name="user-submitted-name" type="text" value="" data-required="true" required placeholder="Your name" class="form-control input-lg usp-input">

    <div class="form-group"><div class="col-lg-offset-2 col-lg-10"><input class="submit btn btn-lg btn-primary" name="user-submitted-post" id="user-submitted-post" type="submit" value="<?php _e('Submit', 'usp'); ?>">

    解决方案

    我从你的评论中了解到你有以下几点:

    $_POST["user-submitted-name"];//表单中的名称$names = get_meta_values('user_submit_name');//不允许带名字的数组

    那么看起来很简单:

    if (in_array($_POST["user-submitted-name"], $names)) {//不允许发帖echo '
  • 名称已被使用';} 别的 {//允许发帖//在这里保存帖子...//然后进入成功页面:header("位置:success.html");//重定向出口();//然后停在这里}
  • 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, proceed normally.

    How do I check if my input value exists and stop the form if this exists?

    <?php
    
    $array = get_meta_values( 'user_submit_name' );
    // Array to search:
    // Temp array so we don't find the same key multipule times:
    $temp = array();
    // Iterate through the array:
    foreach ($array as $key)
    {
    // Check the key hasn't already been found:
    if (!in_array($key, $temp))
    {
        // Get an array of all the positions of the key:
        $keys = array_keys($array, $key);
        // Check if there is more than one position:
        if (count($keys)>0)
        {
            // Add the key to the temp array so its not found again:
            $temp[] = $key;
            // Do something...
            echo '<li>Name already used';                        
        }
    }
    }
    ?>
    

    html

    <form class="form-horizontal" role="form" id="usp_form" method="post" data-validate="parsley" enctype="multipart/form-data" novalidate>
    
            <div class="form-group">
                <label  class="col-lg-2 control-label" for="user-submitted-name">Il tuo nome</label>
                 <div class="col-lg-10">
                    <input name="user-submitted-name" type="text" value="" data-required="true" required placeholder="Your name" class="form-control input-lg usp-input">
                </div>
            </div>
    
        <div class="form-group">
             <div class="col-lg-offset-2 col-lg-10">
                <input class="submit btn btn-lg btn-primary" name="user-submitted-post" id="user-submitted-post" type="submit" value="<?php _e('Submit', 'usp'); ?>">
            </div>
        </div>
    

    解决方案

    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 '<li>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
    }
    

    这篇关于检查数组中是否存在输入字段值,如果存在则返回错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    PHP最新文章
    热门教程
    热门工具
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆