php
  • jquery
  • mysql
  • 2015-07-06 84 views 0 likes 
    0

    我已經創建了一個表單,它接受數據並將其插入到數據庫中。 onlick創建按鈕我希望函數檢查數據庫employee_id中的條目是否已經存在。如果存在,我希望它顯示的數據已經存在,你還是想插入,我在這可以任何人幫助我這個。形式是如何創建一個jquery函數來防止重複條目到數據庫

    <form id="myForm" name="myForm" action='insert.php' method='post' > 
        <input type='hidden' name='st' value=0> 
        <table style="text-align:center; width:100%"> 
         <tr> 
          <td style="text-align:right"><label>Select SE/AE:</label></td> 
          <td style="text-align:left"> 
           <?php include("configs.php"); 
           $sql = "SELECT DISTINCT seae FROM se_ae ";?> 
           <select name="seae"> 
            <option value="" selected></option> 
            <?php foreach ($dbo->query($sql) as $row) { ?> 
             <option value="<?php echo $row['seae']; ?>"> 
             <?php echo $row['seae']; ?></option> 
            <?php }?> 
           </select> 
          </td> 
         </tr> 
         <tr> 
          <td style="text-align:right"><label>Select Brand:</label></td> 
          <td style="text-align:left"> 
           <?php //include("configs.php"); 
           $sql = "SELECT DISTINCT `brand` FROM se_ae ";?> 
           <select name="brand"> 
            <option value="" selected> </option> 
            <?php foreach ($dbo->query($sql) as $row) { ?> 
             <option value="<?php echo $row['brand']; ?>"> 
             <?php echo $row['brand']; ?></option> 
            <?php }?> 
           </select> 
          </td> 
         </tr> 
         <tr> 
          <td style="text-align:right"><label>Select Territory:</label></td> 
          <td style="text-align:left"> 
           <?php //include("configs.php"); 
           $sql = "SELECT DISTINCT `territory` FROM se_ae ";?> 
           <select name="territory"> 
            <option value="" selected></option> 
            <?php foreach ($dbo->query($sql) as $row) { ?> 
             <option value="<?php echo $row['territory']; ?>"> 
             <?php echo $row['territory']; ?></option> 
            <?php }?> 
           </select> 
          </td> 
         </tr> 
         <tr> 
          <td style="text-align:right"><label for="name">Employee Name:</label></td> 
          <td style="text-align:left"> 
           <input type="text" id="name" name="name"/> 
          </td> 
         </tr> 
         <tr> 
          <td style="text-align:right"><label for="number">Employee ID:</label></td> 
          <td style="text-align:left"> 
           <input type="text" id="number" name="number" /> 
          </td> 
         </tr> 
         <tr> 
          <td style="text-align:right"><label for="email"> Email:</label></td> 
          <td style="text-align:left"> 
           <input type="text" id="email" name="email" /> 
          </td> 
         </tr> 
         <tr> 
          <td style="text-align:right"><label for="contact"> Contact:</label></td> 
          <td style="text-align:left"> 
           <input type="text" id="contact" name="contact" /> 
          </td> 
         </tr> 
         <tr> 
          <td style="text-align:right"><label for="exist"> Exist:</label></td> 
          <td style="text-align:left"> 
           <input type="radio" id="exist" name="exist" value="Active"/>Active 
           <input type="radio" id="exist" name="exist" value="NA"/>NA 
          </td> 
         </tr> 
         <tr> 
          <td style="text-align:right" class='swMntTopMenu'> 
           <input style="background-color:rgb(255,213,32)" name="Reset" type="reset" value="Reset"> 
          </td> 
          <td style="text-align:left" class='swMntTopMenu'> 
           <input style="background-color:rgb(255,213,32)" name="submit" type="submit" value="Create"> 
          </td> 
         </tr> 
        </table> 
    </form> 
    
    +0

    它可以達到使用jQuery的ajax.post .. –

    +0

    @ user3113490如果你不介意你能解釋我我真的noob在這 –

    +0

    jquery ajax後..就像一個mediator ....首先是你得到的輸入值,並將其傳遞到一個PHP文件,將做驗證...阿賈克斯是誰將通過的價值...像我說的像一箇中介。 –

    回答

    0

    您可以使用jQuery驗證

    $(function() { 
    $("#resturantRegistration").validate({ 
        rules: { 
          number: { 
          required: true, 
            remote:"user/checkEmployee" 
         } 
        }, 
        messages: { 
         number: { 
          required: "Please enter Employee id", 
          remote: $.validator.format("Employee id already in use") 
         } 
        } 
    }); 
    }); 
    

    PHP函數

    function checkEmployee(){ 
          $emailid = $_GET['number']; 
          if(!empty($emailid)){ 
           $emailRes = $this->users->is_email_available($emailid); 
           if ($emailRes == ''){       
            echo 'false'; 
           } else {    
            echo 'true'; 
           } 
          } 
         } 
    

    型號功能檢查與數據庫

    /** 
        * Check if email available for registering 
        * 
        * @param string 
        * @return bool 
        */ 
        function is_email_available($email) 
        { 
         $this->db->select('1', FALSE); 
         $this->db->where('LOWER(email)=', strtolower($email)); 
         $this->db->or_where('LOWER(new_email)=', strtolower($email)); 
    
         $query = $this->db->get($this->table_name); 
         return $query->num_rows() == 0; 
        } 
    

    上面的代碼是按照codeigniter MVC framew ork你可以在覈心PHP中自定義此

    +0

    我使用這個電子郵件ID,您可以使用員工ID –

    相關問題