0

As i would like to determine whether theres a # in the link, and you only can do this in Javascript, I would like to transfer a JS variable to PHP.

So if i have:

if(location.hash){ var hash = location.hash; 

the hash var needs to be turned in to a php $hash var..

I also tried if not possible, sending in post the variable,

$.post('photo.php?mode=grab', { hash: hash }, function(result) { // ..but then i got stuck, how should i transfer to php var from here? 

2 Answers 2

3
$hashVar = $_POST['hash']; 

This what you're after?

$("#trigger").click(function(){ var hash = location.hash; $.ajax ({ type: "POST", url: "file.php", data: hash, //cache: false, success: function(html) { alert(html); } }); return false; }); 
Sign up to request clarification or add additional context in comments.

3 Comments

no i already have this, i want a $hash(PHP variable) that contains the location.hash..
i'm confused. just change $hashVar to $hash? Is that what you mean?
Noo, ok you have the hash variable in javascript right? Now how would you get it out from javascript in to a php variable (lets call it $hash) forget the ajax call you made, i know you can get it to a variable through the ajax call, but then the variable is on file.php and not the current file you are in(e.g index.php)
0

The posted string will be available to PHP in the $_POST variable.

Since you're posting a Javascript object using JQuery, PHP should receive it as a JSON string.

You can convert the JSON string into a PHP array using the PHP function json_decode().

Similarly, if you need to send an array back from PHP to Javascript, use json_encode() in PHP to reverse the process and create a JSON object.

2 Comments

Yes but the $.post will go to photo.php?mode=grab right? and in this file, the variable can get defined, but i want to get the php variable defined on the same page as the javascript variable is..
@Karem - if I'm understanding you correctly, then the answer is to post it using an old-style HTML form that forces a page reload.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.