0

I have created a form which takes the data and inserts it in the database. onlick of create button i want the function to check database whether the entry with employee_id already exists. if exists i want it to display the data already exists do you still want to insert, i am noob in this can anybody help me with this. the form is

<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>
Scott
  • 1,863
  • 2
  • 24
  • 43
Akshay Vasu
  • 445
  • 1
  • 12
  • 33

1 Answers1

0

You can use the Jquery validation

$(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

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

Model function to check with database

/**
     * 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;
    }

above code is follow the codeigniter MVC framework you can customize this in core PHP as well

Mukesh S
  • 367
  • 5
  • 19