This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 5 years ago.
I'm using
location.hash = $(this).attr("id");
to append id into url bar immediately without reloading. the outcome is : http://example.net/index.php/abc#id
then I echo
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
the output I got was : http://example.net/index.php/abc
How can I get the #id (known as fragment id ) in PHP so that it can be used as PHP variable ?
Thank you!
The webserver don't receive the hash fragment, it's client side only. So you cannot get it with PHP or any other server side language. You can hack around by fetching that hash via javascript and sending it via a GET parameter to the server side script but i don't recommend this.
You can not directly send the fragment part from client to the PHP server side. But the fragment id can be accessed through javascript and you can send it to server as a GET parameter.
Use window.location to get the full url including fragment part and window.location.hash to get just the fragment part in javascript.
In case you send the full url (including fragment hash) to the PHP server, you can use parse_url("URL HERE",PHP_URL_FRAGMENT) function to get the fragment hash.
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 7 years ago.
Hi again are there a way that i can get the variable "log" in a jquery code like the example bellow with out using alert, a field id, class and etc.
<script>
var log='1';
</script>
<?php
$run = log
?>
I want to get the data of a certain variable in a javascript and use it as a value of a variable in my php code
You can't do it that way.
Because:
javascript runs in browser.
PHP runs at server.
There are two ways one is form submission and other one is ajax. You have to send the values to the server to get it to echo.
As per your updates, i would suggest you to use ajax/form submit:
Ajax:
var log = '1';
$.ajax({
url: 'path/to/php/file.php',
type:'get',
data:{log:log},
success:function(data){
console.log(data); // logs: The posted value is 1
}
});
here in the data object log before : is the key while log after : is the var which refers to value '1'. So, in php you can do this:
<?php
if(isset($_POST['log'])){
$run = $_POST['log'];
echo 'The posted value is '.$run;
}
?>
You cannot get client side variable on server side without the use of AJAX or some sort of post back. In all calls to a server, the server side code is execute then returned to render client side in the browser. Therefor, the client side code is ALWAYS run after the server side.
You can look at using client side code to make an AJAX request to the server and return a value or post to to a page which will load and render it server side.
You are trying to echo a value here, this can be done client side but if there is complex server side logic to get the value then I would use an AJAX request to get it.
AJAX
http://www.w3schools.com/jquery/jquery_ajax_intro.asp
POST a form
http://php.net/manual/en/tutorial.forms.php
This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 7 years ago.
My Url is
http://www.domain.com/seg_one/seg_two/seg_three#jumper
I want to get #jumper part of current url
In JavaScript, you can get this by simply location.hash property of window object. i.e.
window.location.hash; // will give you #jumper.
From here once you have it on the client side, do anything you want with it. You can send it back to server by even making an ajax call.
The # is called a fragment. The problem is that browsers won't transmit those to the server so there is now way to parse it.
You can get it via javascript (see below) and make an ajax request to use it in the back-end(PHP):
window.location.href
You can condition the ajax call:
address = window.location.href;
index = address.str.indexOf("#");
if(typeof index !='null') {
var term = str.slice(address.str.indexOf("#")+1, address.length);
console.log(term);//will display jumper
//send it via AJAX
}
$third = $this->uri->segment(3);
$thirdArr = explode('#', $third);
$hash = $thirdArr[1];
This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 9 years ago.
I'm trying to extract the last segment of this URL: http://test.com/blog/#segmentIwant (the #segmentIwant is the string I want). The hash tag is generated from a link within the website. Any help is much appreciated!
PHP cannot read the hash, the server doesnt even recieve it at all!
Well while you load a page with # tag, php does not know it as # tags on url are treated by basically javascript and are not sent to server. But with a simple AJAX request you can send the value to server on loading the page and get the response through that AJAX response.
To grab the url with Javascript:
var url = document.URL;
And try basename() function to get the last part of the sent url with PHP code.
echo basename( "http://test.com/blog/#segmentIwant" );
Or a simple function in javascript to do it:
var url = document.URL;
hashed_string = url.split("#");
alert(hashed_string[1]);
This question already has answers here:
How can I pass variables from JavaScript to PHP?
(2 answers)
Closed 9 years ago.
I want to pass a variable from a function in JavaScript to PHP that is also inside the JavaScript function.
For Example:
myfunction('qwerty');
<script>
myfunctionjs(x)
{
<?php echo $x?>
}
</script>
note: code above is just a sample
Your code sample, while it expresses what you want quite clearly, should also show how impossible it is.
PHP is a server-side language whereas javascript is a client-side language. The only way for javascript to "talk" to PHP is to submit a request to the server. You can do this either through a standard HTTP-GET request or a POST request (through a form submission, or more commonly via Ajax).
The PHP isn't inside the JavaScript function. The PHP code is executed on the server.
When the Javascript is executed, the code looks like this:
myfunction('qwerty');
<script>
myfunctionjs(x)
{
alert(x); //or whatever $x is
}
</script>
If you want to pass 'x' to a PHP script, you'll have to make an AJAX request.
You can't have javascript telling PHP to do something inline. JavaScript runs client side, while PHP runs server side.
Instead, you need to have your javascript send the data to your server, and you'll have PHP do something when receiving that second request.
If you are using jQuery, have a look at the ajax method for example on how to send data: http://api.jquery.com/jQuery.ajax/
Passing variable from javascript to php right away is not possible. You will have to use AJAX for this. You have to understand the difference between server-side scripting and client-side scripting.
https://softwareengineering.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming
Read the above link and it will give you a full understanding of the process. if you have any issues let me know.
Read more
Please bear up with me, I'm new to PHP. I'm using a mixture of PHP and jQuery on the same page. I need to pass variables from jQuery to PHP. All examples I came across describe how to pass variables to a PHP file on the server which I don't want. So far I manged to convert object to JSON using jQuery $.toJSON();
My question is: is it possible to pass jQuery data to php code if both jQuery and PHP reside on the same page?
Here is my code so far:
var myObject = {name: 'Tomas', age: 38};
var encoded = $.toJSON( myObject);
var name = $.evalJSON( encoded ).name;
var version = $.evalJSON(encoded).age;
No it is not possible since PHP is not a client side script but rather a server side script. What that means is that by the time the data is given to the client the PHP script would have most likely already finished running. The following demonstrates the relationship between PHP and JavaScript:
Server -> PHP -> Client (Browser) -> Javascript
Therefore, it is impossible to have javascript communicate to PHP on the same page. What you can do is use Ajax to call a server side file like you said - but I'm afraid that is as much as you can do in terms of PHP and JavaScript Communication