Jquery dialog+ajax problem

Discussion in 'jQuery' started by look4guna, Dec 12, 2010.

  1. #1
    Hi,

    i have two fields in my form like txtuser and txtpass, i've used jquery to show these fields in a dialog.
    i want to submit these fields to php file so that i can verify username and pass, i tried to use ajax with jquery.

    but when i submit these fields after clicking ok button it posts null value. whether the input fields are moved out of form when show the dialog?

    anybody knows the solution for this??
     
    look4guna, Dec 12, 2010 IP
  2. look4guna

    look4guna Active Member

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #2
    Hi friends,

    This is the html code for the two input boxes:
    <div id="log_panel">
    <form name="log_form" method="get">
    <input name="log" id="log" type="button" value="sign in" />
    <input name="txtuser" type="text" class="textpart" id="txtuser" />
    <input name="txtpass" type="text" class="textpart" id="txtpass" />
    <input name="btnlog" type="button" class="predict_button2" id="btnlog" value="login" />
    </div>
    I've used following code to show this log panel in jquery dialog:
    $(document).ready(function(){
    //to show the div as a popup
    $('#log').click(function(){
    $('#log_panel').dialog({
    modal:true,
    width:605,
    height:300,
    title:"Log in"
    });
    });
    //to make ajax call
    $('#btnlog').click(function(){
    $.ajax({
    type : 'GET',
    url : 'auth.php',
    data: {
    name : $('#name').val()
    },
    success : function(data){
    alert(data);
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
    alert('error');
    }
    }); //for ajax
    $('#log_panel').dialog('close');
    }); //to close the click function
    }); // to close the document.ready.

    auth.php contains follwoing code:

    <?php
    echo $_GET['name'];
    ?>

    The problem is when i click the login button it makes the ajax call, but it shows empty values
    after the ajax request.
    I heard some where when we show the form fields in a jquery dialog it's moved out of the form. i think that may be the problem but how can i resolve this..

    anyone knows? Thanks...
     
    look4guna, Dec 12, 2010 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    Here is your problem:

    data: {
    name : $('#name').val()
    },

    There is no input in your HTML with name ID?

    If you do this:

    data: {
    name: $('#txtuser').val()
    },

    Then it is going to send the value in this box:

    <input name="txtuser" type="text" class="textpart" id="txtuser" />

    Make sure you use the ID_OF_INPUT if you are using the ID selector (#).
     
    ThePHPMaster, Dec 13, 2010 IP
  4. look4guna

    look4guna Active Member

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    Thanks PHPMaster for your reply,
    i've corrected the name problem, but when i alert the txtuser.value it resulted in undefined. so ,i changed my approach and using
    $("input").each, i traversed each of the input element in that form , assigned the value of the txtuser by checking this.id.

    Here is the code:
    var user;var pass;
    $("input").each(function(){
    if(this.id=="txtuser")
    { user=this.value; this.value='';}
    else if(this.id=="txtpass")
    { pass=this.value; this.value='';}
    });
    $('#log_panel').dialog('close');
    i triggered this code after clicking the login button.

    Thanks for your timely reply;)
     
    look4guna, Dec 13, 2010 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    Make sure you alert $('#inputID).val() and not $('#inputID').value..
     
    ThePHPMaster, Dec 13, 2010 IP
  6. look4guna

    look4guna Active Member

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    i think using $('#inputID').value won't work, $('#inputID').val() is correct. but when i traversing using "each" method, this.value is working. I think outside that function $('#inputID').value won't work.. is that correct??
     
    Last edited: Dec 13, 2010
    look4guna, Dec 13, 2010 IP