faizan
faizan

Reputation: 45

How can i use ajax data variable use under submit button

When I click submit button, I cannot receive the value yearend. Instead I got an undefined value. How can I get yearend value when I click submit button?

My code:

$("#company").change(function() {

    $("#dFrom").val("");
    $("#dTo").val("");
    var pass_code = $("#company").val();
    var callpage = "dentrycomp.php?pass_code=" + pass_code
    var yearend= null;
    $.ajax({
        type: "GET",
        url: callpage,
        async: false,
        success: function(data) {
            yearend= data,
            $("#content").html("")
            $("#content").html(data)
        }
    });
    //var yearend = "<?php echo $_SESSION['yearend'] ; ?>"
    alert(yearend +"company");
    this alert box getting the right value yearend.i want that value recieve in under submit button                         
    return true;
});


$('#submit').live('click',function() {
//var yearend = "<?php echo $_SESSION['yearend'] ; ?>"
alert("this is submit button"+yearend);

Upvotes: 2

Views: 68

Answers (4)

Sumesh S
Sumesh S

Reputation: 758

var yearend = null;

$("#company").change(function() {

    $("#dFrom").val("");
    $("#dTo").val("");
    var pass_code = $("#company").val();
    var callpage = "dentrycomp.php?pass_code=" + pass_code

    $.ajax({
        type: "GET",
        url: callpage,
        async: false,
        success: function(data) {
            yearend = data,
            $("#content").html("")
            $("#content").html(data)
        }
    });

    //var yearend = "<?php echo $_SESSION['yearend'] ; ?>"
    alert(yearend + "company");
    this alert box getting the right value yearend.i want that value recieve in under submit button
    return true;
});

$('#submit').live('click', function() {
//var yearend = "<?php echo $_SESSION['yearend'] ; ?>"
alert("this is submit button" + yearend); });

You should declare yearend globally i.e. at the top of the code.

Upvotes: 2

pwnz22
pwnz22

Reputation: 469

I think it's because of var scope. You must define variable before the request and then redefine it and get it after the AJAX request.

Upvotes: 1

Muhammad Ramzan
Muhammad Ramzan

Reputation: 288

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
   $.ajax({
                              type: "GET",
                              url: callpage,
                              async: false,
                              success: function(data) {
                              yearend= data,
                              $("#content").html("")
                              $("#content").html(data)
                              $('#submit').live('click',function(){

                               alert("this is submit button"+yearend);
                               });

                              }

                            });
});
</script>
</head>
<body>

</body>
</html>

Upvotes: 0

Nishanth Matha
Nishanth Matha

Reputation: 6081

That's the expected behavior with ajax outside your ajax call yearned is undefined. Put your on click function in ajax call itself. So, once your ajax call is finished and successful you'll attach your event listener in the success function. Use this logic instead:

$.ajax({
                              type: "GET",
                              url: callpage,
                              async: false,
                              success: function(data) {
                              yearend= data,
                              $("#content").html("")
                              $("#content").html(data)
                              $('#submit').live('click',function(){
                              //var yearend = "<?php echo $_SESSION['yearend'] ; ?>"

                               alert("this is submit button"+yearend);
                               });

                              }


                            }); 

Upvotes: 0

Related Questions