0

i have created a form which is submitting data through ajax to the database.this works fine.how can i prevent entering sitename is already available in database.if that sitename already in database it should give error message.

Controller

public function user_add()
    {

        $data_save = array(
            "Mnumber" => $this->input->post("Mnumber"),
            "email" => $this->input->post("email"),
            "fname" => $this->input->post("fname"),
            "address" =>$this->input->post("address"),
            "sitename" =>$this->input->post("sitename"),
           /* "reqnum" => $this->input->post("reqnum"),*/
            "title" => $this->input->post("title"),
            "descr" => $this->input->post("descr"),
            /*"payment" => $this->input->post("payment"),*/
            "uniquekey" => $this->input->post("uniquekey")
            /*"subscription" => $this->input->post("subscription"),
            "email_sent" => $this->input->post("email_sent"),*/
        );

        if ($this->user_mod->AddUser($data_save)) {
                    echo "Successfully Saved";
                }
                else {
                    echo "error";
                }

    }

view

<script>

    function save_user_new() {

        var Mnumber = $('#Mnumber').val();
        var email = $('#email').val();
        var fname = $('#fname').val();
        var address = $('#address').val();
        var sitename = $('#sitename').val();
        /*var reqnum = $('#reqnum').val();*/
        var title = $('#title').val();
        var descr = $('#descr').val();
        var uniquekey = $('#uniquekey').val();
        /*var subscription = $('#subscription').val();
        var email_sent = $('#email_sent').val();
        var payment = $('#payment').val();*/

        if (sitename != "" && email != "") {
            $.ajax({
                type: "post",
                async: false,
                url: "<?php echo site_url('form_con/user_add'); ?>",
                data: {
                    "Mnumber": Mnumber,
                    "email": email,
                    "fname": fname,
                    "address": address,
                    "sitename": sitename,
                    /*"reqnum": reqnum,*/
                    "title": title,
                    "descr": descr,
                    "uniquekey": uniquekey
                    /*"subscription": subscription,
                    "email_sent": email_sent,
                    "payment":payment*/
                },
                dataType: "html",
                success: function (data) {
                    alert(data);
                    if (data == 'error') {
                        $('#success_msg').hide();
                        $('#error_msg1').show();
                        $('#error_msg1').html("Error : Something wrong.");

                    } else if (data == 'have') {
                        $('#success_msg').hide();
                        $('#error_msg1').show();
                        $('#error_msg1').html("Error : This Sitename is already exists.");
                    } else {
                        $('#error_msg1').hide();
                        $('#success_msg').show();
                        $('#success_msg').html("User details successfully saved.");
                        /*setTimeout(function() { location="features.php"},2000);*/
                        location.href = 'freecreate';


                    }

                }

            });
        } else {
            $('#success_msg').hide();
            $('#error_msg1').show();
            $('#error_msg1').html("Error : Please enter User Details.");
        }
    }
</script>


<form action="#" id="form_sample_1">


                    <div class="form-body">
                        <div class="col-md-6">

                            <div class="form-group">
                                <label class="control-label">Your First Name</label>
                                <input type="text" class="form-control" id="fname" name="fname" placeholder="Enter text" required="">
                            </div>


                            <div class="form-group">
                                <label class="control-label">Email Address</label>
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="fa fa-envelope"></i></span>
                                    <input type="email" class="form-control" id="email" name="email" placeholder="Email Address" required="">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="control-label">Your Mobile Number</label>
                                <input type="text" class="form-control" id="Mnumber" name="Mnumber" placeholder="Enter text" required="">
                                <!--<span class="help-block"> A block of help text. </span>-->
                            </div>

                            <div class="form-group">
                                <label class="control-label">Your Address</label>
                                <input type="text" class="form-control" id="address" name="address" placeholder="Enter text" required="">
                            </div>
                        </div>

                        <div class="col-md-6">

                            <div class="form-group">
                                <label class="control-label">Your Site Name</label>
                                <input type="text" class="form-control" id="sitename" name="sitename"  placeholder="Enter text" required="">
                                <span class="help-block"> please enter the sitename only.if you wish to cretae site as john just type john.it will create the site automatically as john.site.mobi </span><br>
                            </div>

                            <div class="form-group">
                                <label class="control-label">Title of Your Web site</label>
                                <input type="text" class="form-control" id="title" name="title" placeholder="Enter text" required="">
                            </div>

                            <div class="form-group">
                                <label class="control-label">Description of Your Web Site</label>
                                <input type="text" class="form-control" id="descr" name="descr" placeholder="Enter text" required="">
                                <!--<input type="hidden" class="form-control" id="req_num" name="req_num" value="1" placeholder="Enter text">-->
                                <?php $uniquekey = md5(uniqid(rand(), true)); ?>
                                <input type="hidden" class="form-control" id="uniquekey" name="uniquekey" value="<?php echo $uniquekey ?>" placeholder="Enter text">
                                <!--<input type="hidden" class="form-control" id="subscription" name="subscription" value="1" placeholder="Enter text">
                                <input type="hidden" class="form-control" id="email_sent" name="email_sent" value="1" placeholder="Enter text">-->
                                <!--<input type="hidden" class="form-control" id="payment" name="payment" value="1" placeholder="Enter text">-->

                            </div>
                        </div>


                        <div class="form-actions right">
                            <!--<a class="btn green" onclick="save_user_new()">Submit</a>-->
                            <button type="submit" id="save_btn" class="btn green" onclick="save_user_new()">Submit</button>
                            <button type="button" class="btn default">Cancel</button>

                        </div>

                    </div>
                </form>

Model

public function AddUser($data_save)
    {
        if ($this->db->insert('users', $data_save)) {
            return true;
        } else {
            return false;
        }

    }
2
  • You should always add validation in the back-end as well. Anyone can modify the JS, using dev tools, or post invalid data directly to your enpoint. Never trust user data. Commented Jun 9, 2016 at 5:36
  • Prevent the default behavior of submit button. Commented Jun 9, 2016 at 5:36

4 Answers 4

1

Write a validate function which actually checks for empty value in textbox and returns true or false. Consider below example.

function ValidateForm(form){
   var valid=true;
   $(form).find('input[type="text"]').each(function(){
           if($(this).val()=="")
                valid=false;
   });
   return valid;
}

function save_user_new() {
       //getting all the values
       //change if condition to
       var form=$("#form_sample_1");
       if(ValidateForm(form))
       {
            //continue with ajax
       }
       else
       {
           alert('Please fill all the fields'); 
           return false;
       }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Add jQuery required in save_user_new function

   $("input").prop('required',true);

Comments

0

There is a couple of ways to do this.

  1. As previously mentioned, write a validate method and check if the record already exists in the database.

  2. Set the field to 'unique', and catch the error when you run the query,

also if you want to save yourself some time and coding write a helper function which will load all your post variables for your.

eg.

function LoadPostVariables($variables = [])
{
    $CI = & get_instance();
    $return_array = [];
    foreach($variables as $key){
        $p_val = $CI->input->post($key);
        //you could perform some basic validation here

        $return_array[$key]=$p_val;
    }
}

$values = LoadPostVariables(['MNumber', 'email', 'fname', 'address', 'sitename', 'title', 'descr', 'uniquekey']);

Comments

0
public function AddUser($data_save)
{
    $check = false;
    $query = "SELECT * FROM users WHERE email='$data_save' LIMIT 1";
    $result = mysqli_query($dbc, $query);

        if($result){
            while($row = mysqli_fetch_assoc($result)){
                $check = true;
            }
        }

    if($check == false){
        // add data
    }else{
        echo 'Email already found'; exit();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.