0

Can some one tell me how to pass variable Yto $('a:eq(0)').click()? I need to alert the server respond to the .click() function. I am using a PHP framework if that's important.

<script>
    $(document).ready(function() {
        function clockIn(str){
            if (window.XMLHttpRequest)
            { // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("message").innerHTML=xmlhttp.responseText;
                    var y = xmlhttp.responseText ; 

                }
            }
            xmlhttp.open("GET","http://localhost/gasstation/index.php/welcome/check_time_clock/"+ str,true);
            xmlhttp.send();
        } 

        $( "input:submit, a", ".login" ).button();

        $("a:eq(0)").click(function(){
            clockIn(<?php echo $emp_id; ?>);
        });

        $("a:eq(1)").click(function(){
            alert('m') ;
        })
    });
</script>
6
  • 1
    could you be more precise as what are you trying to achieve..? Commented Feb 2, 2012 at 4:17
  • If you're trying to alert the server you could use GET or POST and ajax with a URL to a php page that reads these in and then does something server side. Commented Feb 2, 2012 at 4:19
  • Since you've jQuery in your code, you could have used jQuery ajax, that would be lot more easier Commented Feb 2, 2012 at 4:21
  • i dont want to use the jquery .ajax() since iam still learning Commented Feb 2, 2012 at 5:03
  • i think the call back function is what i missed Commented Feb 2, 2012 at 5:04

4 Answers 4

1

Since you are making an ajax call you have to wait until the server responds so you cannot get the value of y inside click hanlder. You can pass a handler to clockIn and then execute it once the ajax response comes. Try this.

function clockIn(str, callback){
   ...
   xmlhttp.onreadystatechange=function()
   {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
        document.getElementById("message").innerHTML=xmlhttp.responseText;
        var y = xmlhttp.responseText ; 
        callback(y);
      }
   }
   ...
}


$("a:eq(0)").click(function(){
   clockIn("<?php echo $emp_id; ?>", function(y){
      //You will get the value of y here.
      alert(y);
   });
});

Since you are using jQuery you should leverage it completly to make your code simple and clean. Instead of creating write the code your own to create xmlhttp request object and bla bla you can smply use $.ajax of jQuery for the same use. Your code will become like this.

function clockIn(str, callback){
   $.ajax({
      url: "http://localhost/gasstation/index.php/welcome/check_time_clock/"+ str,
      success: function(data){
         $("#message").html(data);
         callback(data);
      }
   });
}
Sign up to request clarification or add additional context in comments.

3 Comments

it works fine but i still not understand what you did will you explain it more please " the first code "
I added one more paramater to clockIn function as callback. When you click on a it will call clockIn function passing the emp_id and the callback function. When clockIn function makes ajax call and response comes, we just call the callback function passing the response from server.
@MinaGabriel - Mark this as an answer if this worked for you.
0

Try this:

$("a:eq(0)").click(function(){
        clockIn('<?php echo $emp_id; ?>');

    });

May Be it will Help

Comments

0

I would do something like this since your using jQuery:

<script>
$(document).ready(function() {
    var clockIn = function (str) {
        $.ajax({
            url: "http://localhost/gasstation/index.php/welcome/check_time_clock/"+ str,
            success: function (data) {
                $("#message").html(data);
                var y = data;
            }
        });
    }

    $( "input:submit, a", ".login" ).button();

    $("a:eq(0)").click(function(){
        clockIn('<?php echo $emp_id; ?>');
    });
    $("a:eq(1)").click(function(){
        alert('m') ;
    });

});
</script>

I hope this helps you out

Comments

0
function clockIn(str, cb) {
    $.get("http://localhost/gasstation/index.php/welcome/check_time_clock/"+ str), function(res) {
        // do your stuffs here.
        var y = res;
        cb && cb(y);
    }
}

$("a:eq(0)").click(function(){
    clockIn("hello", function(y){ alert(y); }); // your y here
});

2 Comments

can you tell me exactely what did you do
$.get(url, callback) is jQuery shortcut for the 10 lines+ of your xmlhttp object (since you use $('a:eq(0)') that's mean you already have jQuery anyway so why don't just use it. cb && cb(y); is a way to pass your y value to .click when your ajax completed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.