Welcome Guest, Not a member yet? Register   Sign In
jquery ajax request
#1

[eluser]Unknown[/eluser]
Hi all!!!

I am creating a registration form, which will allow users to select their username. So I have an ajax request which is supposed to return true or false if the name is available. For some reason what is being returned is the HTML of a page.

form code
Code:
<div id="registerFormContainer">
    
    &lt;?php
        $attributes = array(
            'id' => 'vendRegForm'
        );
        echo form_open_multipart('vendor/register_vendor',$attributes);
    ?&gt;
        
    &lt;input id="formType" name="formType" value="A" type="hidden"/&gt;
    &lt;input id="branchNum" name="branchNum" value="1" type="hidden"/&gt;
    
        <fieldset>
            <legend>Login Details</legend>
            <div class="row">
                <span class="label">Username:</span>
                <span class="formw">&lt;input id="username" name="username" type="text" /&gt;&lt;img id="scanVendUsrNameIcon" src="&lt;?php echo base_url();?&gt;assets/username_search.png"> <img id="resultIcon" src="&lt;?php echo base_url();?&gt;assets/username_tick.png"  class="hidden" /></span>
            </div>
            <div class="row">
                <span class="label">Password:</span>
                <span class="formw">&lt;input id="password" name="password" type="password" /&gt;&lt;/span>
            </div>
            <div class="row">
                <span class="label">Re-enter Password:</span>
                <span class="formw">&lt;input id="password2" name="password2" type="password" /&gt;&lt;/span>
            </div>
        </fieldset>

        <div class="row">
            <span class="label">&nbsp;</span>
            <span class="formw">&lt;input id="submitBtn" name="submitBtn" type="submit" value="Register" /&gt;&lt;/span>
        </div>
    &lt;/form&gt;

</div>

JS function
Code:
function verifyUsername(){

    var userData = {};
    userData['reqName'] = $('#username').val();
    
    $.ajax({
        url: '/index.php/ajax/verify_name',
        type: 'POST',
        data: {reqName: $('#username').val()},
        success: function(data, textStatus, jqXHR) {
                    alert('working ' + data);
                 },
        error: function(xmlhttp, status){
                    alert("logout failure ");
                }
    });
}
}

Controller function
Code:
function verify_name(){
        
        $this->load->model('vendor_model');
        $username = $this->input->post('reqName');
        if($this->vendor_model->verify_name_availability($username)){
            echo '1';
        }
    }

Model
Code:
function verify_name_availability($reqName){
        $this->db->where('username', $reqName);
        $res = $this->db->get('vendorpasswords');

        if($res->num_rows == 1){
            return false;
        }else{
            return true;
        }
    }
#2

[eluser]danmontgomery[/eluser]
Code:
return 'lawd have mercy';

You can't return values in an ajax call, you have to output them
#3

[eluser]Unknown[/eluser]
Thanks for the response noctrum...

That line isn't actually in the final draft. In my trying to figure out the issue myself, I tried a lot of things. That line was initially commented out, but in formatting the code to post into the forum i removed the commenting by accident. I have since removed it. Could you see if anything else seems wrong?
#4

[eluser]danmontgomery[/eluser]
Not without more information... What page is being returned? Is it a 404 page, or a valid controller/method response? Using firebug, grab the URL that it's actually requesting. Is it what you expect? What do you see when you go to that url in your browser?
#5

[eluser]GreatSlovakia[/eluser]
Dunno what you are doing incorrectly with this code, although I would advise you to add an else statement to the controller function and next look into the developer tools of your browser what the ajax request is returning.

Alternatively you could use http://ellislab.com/forums/viewthread/191441/ to directly call your model from javascript. (disclaimer, that's written by me Tongue)




Theme © iAndrew 2016 - Forum software by © MyBB