禁用提交如果用户名不可用 PHP

huangapple go评论70阅读模式
英文:

Disable submit if the username is not available php

问题

这是您的代码的翻译部分:

我正在尝试开发一个表单来创建一个类别。
我已经编写了一个代码来在用户输入类别名称时检查类别名称的可用性。
这是我在表单中使用的HTML代码。

<div class="form-group">
 <label>类别名称<span class="text-danger">*</span></label>
 <input type="text" name="category_name" class="form-control" id="category_name">
 <div id="cat_name_status" class="mt-1"></div>
</div>

<button class="btn btn-primary" name="create" id="create" type="submit">创建</button>

如果类别名称不可用(已经被使用),则提交按钮应该被禁用。
这是我正在使用的jQuery脚本。

<script>
    $(document).ready(function(){
    
       $("#category_name").keyup(function(){
    
          var cat_name = $(this).val();
          var usernameRegex = /^[a-zA-Z0-9]+$/;
    
          if(cat_name != ''){
    
             $.ajax({
                url: 'cat_name_status.php',
                type: 'post',
                data: {cat_name: cat_name},
                success: function(response){               
                    $('#cat_name_status').html(response);                    
                 }
             });
          }else{
             $("#cat_name_status").html("<span style='color: red;'> <i class='fas fa-times-circle'></i> 请输入一个有效的类别名称</span>");
             $("#create").attr("disabled", true);
          }
    
        });
    
     });
</script>

这是cat_name_status.php中的PHP代码:

<?php
     require('../../dbc/dbconn.php');
     include('../../includes/scripts.php');
     include('../../includes/functions.php');
     if(isset($_POST['cat_name'])){
        $cat_name = clearInput($condb, $_POST['cat_name']);
        $check = mysqli_query($condb, "
          select cat_name 
          from pos_categories 
          where cat_name = '$cat_name'");
        if(mysqli_num_rows($check)>0){
            echo '<p class="text-danger"><strong><i class="fas fa-times-circle"></i> 类别名称已经存在</strong></p>';
        }
    }
?>

此代码目前可以成功在cat_name_status div中显示类别名称的可用性,但需要您的支持来修改它以在类别名称不可用时禁用提交按钮。

英文:

I am trying to develop a form to create a category.
<br>I have written a code to check the availability of the Category Name while user is typing the category name.
<br>This is the html code I have used in the form.

&lt;div class=&quot;form-group&quot;&gt;
 &lt;label&gt;Category Name&lt;span class=&quot;text-danger&quot;&gt;*&lt;/span&gt;&lt;/label&gt;
 &lt;input type=&quot;text&quot; name=&quot;category_name&quot; class=&quot;form-control&quot; id=&quot;category_name&quot;&gt;
 &lt;div id=&quot;cat_name_status&quot; class=&quot;mt-1&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;button class=&quot;btn btn-primary&quot; name=&quot;create&quot; id=&quot;create&quot; type=&quot;submit&quot;&gt;Create&lt;/button&gt;

What i need is, if the Category name is not available (already taken) the submit button should be disable.
<br>This is the jquery script I am using.

&lt;script&gt;
    $(document).ready(function(){
    
       $(&quot;#category_name&quot;).keyup(function(){
    
          var cat_name = $(this).val();
          var usernameRegex = /^[a-zA-Z0-9]+$/;
    
          if(cat_name != &#39;&#39;){
    
             $.ajax({
                url: &#39;cat_name_status.php&#39;,
                type: &#39;post&#39;,
                data: {cat_name: cat_name},
                success: function(response){               
                    $(&#39;#cat_name_status&#39;).html(response);                    
                 }
             });
          }else{
             $(&quot;#cat_name_status&quot;).html(&quot;&lt;span style=&#39;color: red;&#39;&gt; &lt;i class=&#39;fas fa-times-circle&#39;&gt;&lt;/i&gt; Enter a valid category name&lt;/span&gt;&quot;);
             $(&quot;#create&quot;).attr(&quot;disabled&quot;, true);
          }
    
        });
    
     });
    &lt;/script&gt;

and this is the php code in cat_name_status.php

&lt;?php
     require(&#39;../../dbc/dbconn.php&#39;);
     include(&#39;../../includes/scripts.php&#39;);
     include(&#39;../../includes/functions.php&#39;);
     if(isset($_POST[&#39;cat_name&#39;])){
        $cat_name = clearInput($condb, $_POST[&#39;cat_name&#39;]);
        $check = mysqli_query($condb, &quot;
          select cat_name 
          from pos_categories 
          where cat_name = &#39;$cat_name&#39;&quot;);
        if(mysqli_num_rows($check)&gt;0){
            echo &#39;&lt;p class=&quot;text-danger&quot;&gt;&lt;strong&gt;&lt;i class=&quot;fas fa-times-circle&quot;&gt;&lt;/i&gt; Category Name already exists&lt;/strong&gt;&lt;/p&gt;&#39;;
        }
    }
?&gt;

At this moment, this code successfully prints the availability of the category name in cat_name_status div but need your support to modify it to disable the submit button if the category name is not available.

At this moment, this code successfully prints the availability of the category name in cat_name_status div but need your support to modify it to disable the submit button if the category name is not available.

答案1

得分: 1

Here's the translated content:

"而不是将错误消息作为字符串打印,您可以使用对象并将其编码为JSON格式,以更好地与客户端通信:

如果(mysqli_num_rows($ check)> 0){
$ response =(object)[
'code' => 1,
'message' => '

类别名称已存在

'
];
echo json_encode($ response);
}
然后,您可以解析JSON并根据返回对象的“code”属性采取行动:

success:function(response){
var result = $ .parseJSON(response);
if(result.code ==“1”){
$('#cat_name_status').html(result.message);
$(“#create”).prop(“禁用”,true);
}
}

为什么以JSON格式打印对象使客户端-服务器通信更好:因为它提供了一种清晰和灵活的方式来接收AJAX调用中的多种数据类型结构。在您的特定情况下,您需要从服务器获取两个信息:一个整数代码,指示发生的情景,以及包含要显示给用户的相应错误消息的字符串消息。通过使用JSON,您可以轻松地以清晰和组织良好的格式接收这两个数据片段,客户端代码可以高效处理它们。"

英文:

rather than printing the error message as a string, you could use an object and encode it to JSON format to better communicate with client:

if (mysqli_num_rows($check) &gt; 0) {
        $response = (object)[
            &#39;code&#39; =&gt; 1,
            &#39;message&#39; =&gt; &#39;&lt;p class=&quot;text-danger&quot;&gt;&lt;strong&gt;&lt;i class=&quot;fas fa-times-circle&quot;&gt;&lt;/i&gt; Category Name already exists&lt;/strong&gt;&lt;/p&gt;&#39;
        ];
        echo json_encode($response);
    }

then you can parse the JSON and take action based on the "code" property of the returned object:

success: function(response){             
            var result = $.parseJSON(response);
            if(result.code == &quot;1&quot;){
                $(&#39;#cat_name_status&#39;).html(result.message);      
                $(&quot;#create&quot;).prop(&quot;disabled&quot;, true);              
            }
}

Why printing an object in JSON format makes the client-server communication better: because it provides a clean and flexible way to receive multiple data type structures in an AJAX call. In your specific case, you required two pieces of information from the server: an integer code indicating the scenario that occurred, and a string message containing the corresponding error message to be displayed to the user. By using JSON, you can easily receive both pieces of data in a clear and organized format that can be efficiently processed by the client-side code.

huangapple
  • 本文由 发表于 2023年4月19日 15:24:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定