Welcome Guest, Not a member yet? Register   Sign In
ajax not working after form submit
#1

[eluser]TerryE[/eluser]
I have a ajax call on a input field to get the users id before the form is submitted. It works as long as the form is not submitted. the ajax call goes to index.php/login/test and the form gets submitted to index.php/login/validate_user. Not sure why this is happening.
#2

[eluser]CroNiX[/eluser]
You might want to post some code.
#3

[eluser]TerryE[/eluser]
Code:
[removed]
        $( document ).ready(function() {
      
      $('#username').focusout(function() {
        var name =  $.trim($(this).val());
        if(name == ""){
         alert('empty');
        }else{
          
                   $.ajax({
                              type:"POST",
                              url: "index.php/login/test",
                              data: {name:name},
                              dataType: "json",
                              cache: false,

                               success: function(msg) {
                                         alert(msg['userid']);
        
                               },
                              error: function(){
                                             alert('nothing returned');
    
                              }
                        });
        }
                 return false;
       });

        });
  
    [removed]
#4

[eluser]TerryE[/eluser]
Sorry about the messy code, but every time i try to post code it does not work.I did run firebug and the error i got after the form was submitted was 404 page not found and the ajax call is looking index.php/login/index.php/login/test
#5

[eluser]CroNiX[/eluser]
That's very hard to follow with the formatting.
The URL probably can probably be solved by adding a / before it, like
'/index.php/login/test' so it starts from the site root instead of relative (appending to current url).
#6

[eluser]TerryE[/eluser]
Code:
$('#username').focusout(function() {
        var name =  $.trim($(this).val());
        var url = " http://localhost:8888/IBEW/public_html/ibewAdmin/index.php/login/test"
               if(name == ""){
                   alert('empty');
                }else{
                      $.ajax({
                                type:"POST",
                                url: url,
                                data: {name:name},
                                dataType: "json",
                                cache: false,

                       success: function(msg) {
                               alert(msg['userid']);

                        },
                         error: function(){
                                 alert('nothing returned');
                         }
                      });
                    }
                     return false;
       });


by adding the var url fixed the problem
#7

[eluser]CroNiX[/eluser]
Yes, but when you change to a different server you will have to manually update all of those urls.
In my template head, I do:
Code:
<skript type="text/javascript">var base_url = "&lt;?=base_url(); ?&gt;";</skript>
Then when using,
Code:
var url = base_url + 'controller/method/etc';

So when you update your application and set the base_url, it changes in your js as well as the links in your app, assuming you used the URL helpers.
#8

[eluser]jonez[/eluser]
Why define the URL at all? Pull it from the form that triggers the event.

Code:
&lt;form id="myForm" action="&lt;?=base_url( '/submit/path' )?&gt;" data-user-validate="&lt;?=base_url( '/validate/user/path' )?&gt;" method="post"&gt;
...
&lt;/form&gt;

var form = document.getElementById( 'myForm' );

$.ajax({
  url: form.getAttribute( 'data-user-validate' ),
  type: form.method,
  data: $( form ).serialize( ),
  success: function( response ) {
    alert( 'thunderbirds are go' );
  },
  error: function( ) {
    alert( 'i need these peach baskets back' );
  }
});

Just realized you're validating the username not submitting the form, updated example. For that you can either use a relative path from the site root (everything except the domain) or set a data attribute on the form and read that as the URL (if you want to pass it through base_url).
#9

[eluser]M Arfan[/eluser]
here is the complete solution
http://www.learnipoint.com/forum/showthr...=10#post10

if you have any problem with codeigniter you can post on above link within 1 hour you will get your solution

thanks




Theme © iAndrew 2016 - Forum software by © MyBB