如何在提交表单后检查数据库中存在的文本框值

时间:2017-06-08 06:01:09

标签: javascript php ajax database

我在表格中有这个HTML代码,我想检查用户输入的这个值是否已经存在于数据库中。

<tr height="50">
    <td valign="top"><label for="CINumber"><span style="color:red">*</span>CI Name:</label></td>
     <td><input type="text" id="CINumber" name="CINumber"></td>                     
</tr>

我有这个``JavaScript函数,用于sumbmit的形式值:

function submit(){
    var cNumber = document.getElementById("CINumber").value;
     //many form value within this function but I showed this value only cause this value need to check when click submit button.
    $.ajax({
        url: 'PHPMethodCalls_AL.php',
        type: 'post',
        data: {'action':'AddNewCI', 'cNumber' : cNumber },
        success: function(data,status) {
            //alert(data);                                                              
            if(status == "success"){                            
                alert("CI Name can be assigned");
                alert("Successfully Register New CI Record.");                              
            } else {
                alert("CI Name is already present!");
                return ;
            }   
        },                              
        error: function(xhr, desc, err) {
            console.log(xhr);
            console.log("Details: " + desc + "\nError:" + err);
        }
    });                                                                         
}

我在PHPMethodCalls_AL.php&#39;中调用了PHPMethod调用。 URL ..

if($_POST['action'] == "AddNewCI"){
    $cNumber = $_POST['cNumber'];
    $newCI = AddNewCI($cNumber);
    echo json_encode($newCI );
}

之后,我调用与数据库连接的PHP函数。

function AddNewCI($cNumber){
    global $DB;
    if(!empty($cNumber)){   
        $namequery="SELECT Name from Item where Name='$cNumber'";
        $namecheck=mysql_query($namequery);
        $row=mysql_num_rows($namecheck);

        if($row==0){                     
            echo "success";
            $strSql1 ="INSERT INTO Item (Name)VALUES('$cNumber')";
            $res1 = $DB->Query($strSql1, false, $err_mess.__LINE__);                    
        }
        else {  
            echo "!success";
        }
    }                                                       
}

点击提交后,我只得到(状态==&#34;成功&#34;)然后弹出2个警告框,即使数据已存在于数据库中。但是,重复值不会保存在数据库中,因为我为这些字段设置了UNIQUE。如果数据存在,请告诉我如何获得正确的警告框,显示&#34; Alredy存在&#34;如果不是&#34;显示成功提醒框&#34; ...

3 个答案:

答案 0 :(得分:1)

更改以下代码......

                            url: 'PHPMethodCalls_AL.php',
                            type: 'post',
                            data: {'action':'AddNewCI', 'cNumber' : cNumber },

                            success: function(data) {

                            if(data == "success"){                            
                                alert("CI Name can be assigned");
                                alert("Successfully Register New CI Record.");                              
                            }

无需使用json_encode,如果需要,可以直接回显任何值。

if($_POST['action'] == "AddNewCI"){
$cNumber = $_POST['cNumber'];
$newCI = AddNewCI($cNumber);
echo $newCI;
}

更改函数返回值...

function AddNewCI($cNumber){
    global $DB;

        if(!empty($cNumber)){   

            $namequery="SELECT Name from Item where Name='$cNumber'";
            $namecheck=mysql_query($namequery);
            $row=mysql_num_rows($namecheck);
        if($row==0){                     
                $strSql1 ="INSERT INTO Item (Name)VALUES('$cNumber')";
                $res1 = $DB->Query($strSql1, false, $err_mess.__LINE__); 
                return "success";                   
            }
        else
            {   
                 return "error";
            }

        }                                                       
   }

答案 1 :(得分:0)

在ajax请求中更改您的成功函数:

success:fucntion(data){
  if(data=="success"){
    alert("Your success message here.");
  }else{
    alert("Alert message whatever you want.");
  }
},

答案 2 :(得分:0)

  

尝试减少代码并运行良好
      如果使用jquery验证,则无需显示错误消息,您只需使用&#34; render&#34;和渲染函数显示错误消息,如jquery中所需                   //当浏览器准备就绪时......         $(function(){         //在#register-form元素上设置表单验证         $(&#34;#addfrm&#34)。验证({

   // Specify the validation rules
    rules: {
      CINumber: {
            required: true,
            remote: {
            url: "PHPMethodCalls_AL.php",
            type: "post",
            data: {
              CINumber: function() {
                return $( "#CINumber" ).val();
              }
            }
          }
        }
    },

    // Specify the validation error messages
    messages: {
        CINumber: {
            required: "Please enter your CINumber",
            remote:"Entered CINumber already exists please enter another one"
       }
    },

    submitHandler: function(form) {
        form.submit();
    }
});
});
</script>

 PHPMethodCalls_AL.php
   this file return true when cinumber not exits on database and when it`s return false that means cinumber already exits on database      


<?php
   //add database configuration 
   $CINumber=$_POST['CINumber'];
   $query="SELECT * FROM `table_name` where `CINumber`=$CINumber";
   $result=mysql_query($query);
   $number_of_rows = mysql_num_rows($result);
   if($number_of_rows){
      echo "false";
   }else{
     echo "true";
   }
   die();
   ?>