This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I am trying to create a site where someone can create an "item" and the database will store an id and php generates a url for that id. So next time when the person comes back with that url it will remember the person's settings (variables). Now the problem is that in my site javascript need to know these variables.
So what is the best solution for this? passing the variables in the superglobal "GET" or maybe cookies? Or is there a better way to pass these variables to javascript?
just use php to print some dynamic javascript
<script>
var myVar = "<?php echo json_encode($_COOKIE['somevalue']);?>";
</script>
There are multiple methods for providing the data to the client, such as:
Echo the variables in your javascript, var userid = <?php echo
$userid; ?>
JSON'fy your variables and supply them to your javascript via AJAX/jQuery: $.getJSON(url, function(data){ var userid = data.userid; });
I typically utilize JSON as much as possible when trying to present server-side data to the client-side, as it helps to separate the different layers.
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:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 8 years ago.
I have a jQuery function, which, on click, itercepts a clicked link:
$("a").click(function(e) {
e.preventDefault();
var link = $(this).prop('href');
alert(link);
});
and it works properly, but how to pass that link to a PHP variable to 'echo' it later?
I unsuccessfully tried:
$("a").click(function(event) {
event.preventDefault();
var link = $(this).prop('href');
<?php $link = .'link'. ?>
});
I know it may be a silly question, I looked all over the web but I didn't find any proper answer.
The only way to do this is to have a script wich stores values passed to it in a session or to a DB and either output the session data later or read from the db
$("a").click(function(event) {
event.preventDefault();
var link = $(this).prop('href');
$.post('saveLink.php', {href : link});
});
the saveLink.php would be something like this
<?php
// Clean your POST variable, always clean any POST variables
// see here for a good discussion on that
// http://stackoverflow.com/questions/4223980/the-ultimate-clean-secure-function
$link = htmlspecialchars($_POST['href']);
$_SESSION['links'] = $link;
?>
then later on you can retrieve all you session data
Note
Session data is not permanent and not shareable between users so if you need to do that you would be better off with a db query also if you need to access a particular link then you will need to send some sort of id with it.
PHP is preprocessor running on a server side. jQuery is running on client side (in user's browser) and it can't comunicate with PHP like this.
Although you can send the data to PHP with Ajax request and process them in another php script, for example store them in a database.
$.ajax({
url: '/ajax_catch.php',
type:'POST',
data: {
link: link
},
success: function(m){
//what to do after its done
}
});
This question already has answers here:
How to access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?> [duplicate]
(6 answers)
Access PHP variable in JavaScript [duplicate]
(3 answers)
Closed 10 years ago.
How to use javascript to access php variable?
Does only one way to write code (like var a={?echo $variable})in javascript?
Recommend some books on php and javascipt(include projects)?I don't know how to use knowledge in projects?
Yes, you were correct.
Because PHP is executed before JavaScript, you can't change it later on, but you could do something like this:
<? $aVar = "whatever"; ?>
...
<script>
var aVar = "<? echo $aVar; ?>"; // note the quotes! (SO's highlighter renders this incorrectly, starting a PHP block inside quotes is valid and will be recognized.)
</script>
That will send this to the client:
<script>
var aVar = "whatever"; // note the quotes!
</script>
var a=<?php echo $variable; ?>
PHP runs in server and js in client side, so Iguess there is not other you can get it. OR you need to use AJAX
I don't see how can you do that in any other method. PHP is server side, while JS is executed on client's PC (not on the web server). Theoretically and practically it's not possible.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to pass JavaScript variables to PHP?
how would I go about moving a javascript variable over to a php script without a form submission?
is it even possible?
ajax is an acceptable option but how
lets say I want to run thisnewoption.php in process sending it javascript variable imgfilename which contains a string
Once your php page has loaded, there's no going back. The best way to go about passing variables ( data ) back to the server is with ajax, so it might be time to brush up on ajax http://api.jquery.com/jQuery.ajax/
Update: Tell you what, here is what a really basic ajax call looks like, notice that is send data to the server here with POST:
$.ajax ( {type : 'POST' ,
url : 'email.php' ,
data : { variable: variable, anotherVariable: anotherVariable } ,
success : function ( data ) { $('#some-div').html ( data ) ; }
} ) ;
You cannot literally pass a javascipt variable as a PHP one as PHP is evaluated on server side where as javascript on client. You can use ajax call to pass it however.