How to retain a particular item id through various calls?
<p>I am trying to do this:</p>

<p>Have the user click some action.  The action checks whether the user is logged in or not.  If not, open a login dialog box, and if the user logs in correctly, update the original page.</p>

<p>I got almost all this working except the part of after successful login. The problem is that the code seems to not have access to the item_id which I was trying to update. Instead, when I try to set the item id (problem_id in this case) in the login form of the popup box, the id is the number of the id that is the last one on the page, and not the one that was clicked.</p>

<p>Here is what I am trying to do in the jQuery:</p>

<pre><code>&lt;script type="text/javascript"&gt;
$(document).ready(function() 
{
     var $dialog = $('#loginpopup')
       .dialog({
         autoOpen: false,
         title: 'Login Dialog'
       }); 

       var $problemId = $('#theProblemId', '#loginpopup');

        $("#newprofile").click(function () 
        {
          $("#login_div").hide();
          $("#newprofileform").show();
        });

    // Called right away after someone clicks on the vote up link
    $('.vote_up').click(function() 
    {        
        var problem_id = $(this).attr("data-problem_id");

        //alert ("In vote up click, problem_id: " + problem_id );
        voteUp(problem_id);

        //Return false to prevent page navigation
        return false;       
    });

    var voteUp = function(problem_id) 
    {
        alert ("In vote up function, problem_id: " + problem_id );

        var dataString = 'problem_id=' + problem_id + '&amp;vote=+';

        $.ajax({
                type: "POST",
                url: "/problems/vote.php",
                dataType: "json",
                data: dataString,
                success: function(data)
                {           
                    alert ("vote success, data: " + data);
                    // ? :)

                },
                error : function(data) 
                {
                    alert ("vote error");
                    errorMessage = data.responseText;

                    if ( errorMessage == "not_logged_in" )
                    {
                        //set the current problem id to the one within the dialog
                        $problemId.val(problem_id);                 

                        // Try to create the popup that asks user to log in.
                        $dialog.dialog('open');

                        alert ("after dialog was open");

                        // prevent the default action, e.g., following a link
                        return false;
                    }
                    else
                    {
                        alert ("not");
                    }

                    //alert(JSON.stringify(data));
                } // Closing error case
            }); // Closing AJAX call.
    };

    $('.vote_down').click(function() 
    {
        alert("down");

        problem_id = $(this).attr("data-problem_id");

        var dataString = 'problem_id='+ problem_id + '&amp;vote=-';        

        //Return false to prevent page navigation
        return false;
    });    

    $('#loginButton', '#loginpopup').click(function() 
    {
    alert("in login button fnction");
            $.ajax({
                url:'url to do the login',
                success:function() {
                    //now call cote up 
                    voteUp($problemId.val());
                }
            });
        });    
});
&lt;/script&gt;
</code></pre>

<p>and here is the login form:</p>

<pre><code>         &lt;div id="login_div"&gt;
         &lt;form id="login_form" method="post" action=""&gt;
         &lt;p&gt;
             &lt;label for="name"&gt;&lt;span&gt;Your Email:&lt;/span&gt;&lt;/label&gt; &lt;input type="text" name="email" id="email" /&gt;
         &lt;/p&gt;
         &lt;p&gt;
             &lt;label for="name"&gt;&lt;span&gt;Your Password:&lt;/span&gt;&lt;/label&gt; &lt;input type="password" name="user_pass" id="user_pass"&gt;
         &lt;/p&gt;

         &lt;input type="hidden" id="problem_id" name="problem_id" value="&lt;?php echo $problem_id; ?&gt;" /&gt;

&lt;span class="no_such_user" style="color: red; display:none"&gt;The login and password does not match our records.&lt;/span&gt;
&lt;span class="password_error" style="color: red; display:none"&gt;The password much be 5 characters or more.&lt;/span&gt;
&lt;span class="login_success" style="color: green; display:none"&gt;You successfully logged in.&lt;/span&gt;
         &lt;p&gt;
            &lt;input type="submit" value="Log In"  /&gt;
         &lt;/p&gt;
         &lt;/form&gt;

         &lt;/div&gt;
</code></pre>

<p>But the problem_id which is being set in this form isn't the one being clicked on, even though I am trying to save it in my jQuery.</p>

<p>Also, here is the code that gets executed for login:</p>

<pre><code>$(function()
{
    $("#login_div input[type=submit]").click(function()
    {
        var email = $("#email").val();
        var password = $("#user_pass").val();

        //alert("Email: " + email);
        //alert("password: " + password);

        var dataString = 'email='+ email + '&amp;password=' + password;

        if( !email )
        {   
            alert ("1");
            $('.login_success_email_empty').fadeOut(200).hide();
            $('.login_error_email_empty').fadeOut(200).show();
        }       
        else        
        if( !password || password.length &lt; 5)
        {alert ("2");
            $('.password_success').fadeOut(200).hide();
            $('.password_error').fadeOut(200).show();
        }
        else
        {
            $.ajax({
                type: "POST",
                url: "../auth/login_ajax.php",
                dataType: "json",
                data: dataString,
                success: function(json)
                {
                    $('.password_error').fadeOut(200).hide();
                    $('.no_such_user').fadeOut(200).hide(); 
                    $('.login_success_email_empty').fadeOut(200).hide();
                    $('.login_success').fadeIn(200).show();                                 

                    // Closing the dialog bosx
                    $('#loginpopup').dialog('close');

                    // Swapping out the header div
                    $('#header_logged_out').hide();
                    $('#header_logged_in').show();  

                    // Now also need to retrieve the problem_id                 
                    problem_id = $("#problem_id").val();
                    //$problemId = $('#theProblemId', '#loginpopup').val();         


                    var $problemId = $('#theProblemId', '#loginpopup');
                    alert ("After login, problem_id: " + problem_id + " and problemId was: " + $problemId);
                },
                error : function(json)
                {
                    alert ("error");

                    // Output the result.
                    errorMessage = json.responseText;

                    alert ("ErrorMessage: " + errorMessage );

                    if ( errorMessage == 'no_such_user' )
                    {
                        $('.no_such_user').fadeOut(200).hide();
                        $('.no_such_user').fadeOut(200).show();                 
                    }
                }
            });
        }

        return false;
    });
});
</code></pre>

<p>and I am just not sure how to get that problem_id which was set in the original jQuery code to be recognized in the jQuery code that executes after login.</p>

<p>Because what I really need to do is update that particular problem depending on whether the user was logged in.</p>

<p>And how do I know in the original code that processes the votes, whether the login in the dialog box was successful or not?</p>

<p>By the way, I am working on this page: <a href="http://www.problemio.com" rel="nofollow">http://www.problemio.com</a></p>
