Question

I have a form :

   <?php
        $attr = array('id'=>'urlSubmit');
        $urlInputAttr = array('name'=>'urlInput','value'=>'yourdomain.com','maxlength'=>'50','size'=>'25');
        echo form_open('urlSubmission',$attr);
        echo form_input($urlInputAttr);
        #echo form_submit('urlInput', '');
        echo form_close();
    ?>

a controller called urlsubmission

       $this->load->model('domaincheckmodel');

            $this->domaincheckmodel->verifyduplicates($this->input->post('urlInput'));   

and a function within a model(domaincheckmodel) which basically checks for duplicate records and inserts a new domain:

   function verifyduplicates($tldEntered){
    # $_POSTed value of urlInput
    ## Gather if the domain exists in db
    $DupDomains = $this->db->get_where('ClientDomain', array('tld'=>$tldEntered));

        if($DupDomains->num_rows() > 0 ){
            $this->load->view('err/domainexists'); ##domain already used
        }

        # else, no domain present, insert.
        else{
            #array of insert values:
            $insertNewDomain = array('tld'=>$this->input->post('urlInput',TRUE));
            $this->db->insert('ClientDomain', $insertNewDomain); 
            $this->load->view('success/domainfree'); ##domain is free and has been entered.
        }
    }
Was it helpful?

Solution

$this->domaincheckmodel->verifyduplicates($this->input->post('urlInput')); 



function verifyduplicates($tldEntered){
    # $_POSTed value of urlInput
    ## Gather if the domain exists in db
    $DupDomains = $this->db->get_where('ClientDomain', array('tld'=>$tldEntered));

You're passing from the form to the controller to the model, are you sure the post variable is staying populated? Try the above, capture the post variable in the controller and pass it to the model instead of trying to read it in the model itself?

A little clarification on passing parameters to functions. You can do this via whatever is inside the brackets as follows.

Controller:

$myVariable = $this->someModel->someFunction($someParameter)

Model:

function someFunction($variableIWantToPopulateWithSomeParameter)

So someParameter gets passed from the controller to the function name in the model. There is one thing to be aware of though and that is that model function now EXPECTS a parameter and if you don't give it one, ie you call someFunction() you'll get an error. This can be avoided by giving it a default value like this:

function someFunction($myVariable = 1)

What that is going to do is say if I don't get a value passed to me I am going to make $myVariable equal to one, if I do I'll overwrite 1 with the new value. So if you send two calls to that function this is what you can expect:

//$myVariable is going to be 1, the default.

$this->someModel->someFunction();

//$myVariable is going to be 5, the value passed to it

$this->someModel->someFunction(5);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top