Petrus
Petrus

Reputation: 41

Pass $_GET value to web page loaded with jquery

I have a page that loads messages for a messaging system. This page loads external_content (another page) every second using AJAX. The conversation ID is stored in the URL, so my method of transferring it for use in external_content was very basic:

$got = $_GET['conversation_id'];
$_SESSION['id'] = $got;

However, there is a problem. When multiple conversations are open at the same time on someone's account, each time one resets the other does too (the session variable is reset), so I need to prevent that from happening.

Thus, I somehow need to pass this GET variable through to another page where I could not simply use $_GET['conversation_id'], but I need to do it in a way so that the variable can be set more than once. External_content is shown in a div, so I'm not sure about passing it along with $_POST or something.

Big n00bie. Please help.

Upvotes: 0

Views: 217

Answers (3)

nickbwatson
nickbwatson

Reputation: 162

I assume in the script that the ajax request is sent to is looking for $_SESSION['id']? And that's where your issue is when you have the multiple tabs open, they're all looking at the same variable.

So whenever you send that ajax request, attach the conversation_id

$.get( "YOURSCRIPT.php", {
    convo_id: "<?= $_GET['conversation_id']; ?>",
});

And then in YOURSCRIPT.php, instead of grabbing $_SESSION['id'], you can grab $_GET['convo_id']

EDIT

@Dr. McKay's way is the same as what I've just said here. Sorry I missed that one.

Upvotes: 1

MehmetDemirkan
MehmetDemirkan

Reputation: 1

When your user on focus textarea on the current page, you can trigger onfocus event to make an ajax call to set Session superglobal for current conversation id.

Upvotes: 0

Dr. McKay
Dr. McKay

Reputation: 2977

Are you saying that you need to pass the conversation ID in the Ajax request? To do that, add it to a script element as a variable in your page head.

<script>
var g_conversationID = "<?php echo $_GET['conversation_id']; ?>";
</script>

Then pass it in your request:

$.get("mypage.php", {"conversation_id": g_conversationID}, function(data) {
    // Handle content
});

You could potentially parse it out of the URL using JavaScript, but this method is generally easier.

Upvotes: 2

Related Questions