0

First of all let me tell you all I'm very new to js, jQuery & ajax. But I'm very well with PHP.

I got a jQuery script from the internet:

function getMessages (){
        $.get('php/chatGetter.php', function(data){
            $("#msgbox").html(data);
        });
    }

    setInterval(function(){
        getMessages();
    }, 500);
});

As you can see it gets data from chatGetter.php page and shows in div whose id is msgbox every 500 millisecond.

I want to send a php variable ($abc ="something";) through function getMessages to chatGetter.php page every time the getMessages function is executed. But I'm not getting how to do that.

2 Answers 2

1

Make these changes in your jQuery code:

function getMessages (){
        var something = 'something';
        $.get('php/chatGetter.php?abc=' + something, function(data){
            $("#msgbox").html(data);
        });
    }

 setInterval(function(){  
      getMessages();
 }, 500);

And then, in the chatGetter.php file, add this on the top:

if(isset($_GET['abc'])) {
     // do whatever you wanna do, here...
}

Here's the working example: http://jsfiddle.net/mnL5Lsmz/

Sign up to request clarification or add additional context in comments.

Comments

0

If your JS code is in your PHP file do this.

function getMessages (someVariable){
        $.get('php/chatGetter.php?abc=' + someVariable, function(data){
            $("#msgbox").html(data);
        });
    }

    setInterval(function(){
      var someVariable = '<?php echo($abc);?>';  
      getMessages(someVariable);
    }, 500);
});

2 Comments

yes js is in php file and variable i want to send is already declared and initiated in php above js
@ShaaNMalik Then the above code should work, make sure you update the variable names

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.