Assign JavaScript variable to Smarty variable [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to assign a javascript variable to a smarty variable
I want to assign a textfield value to Smarty variable using JavaScript and call Smarty function but I could not do it.
Please guide me.

You cannot do that in an easy way. PHP, and by extension Smarty, is parsed and run on the server side before the browser get the data. JavaScript is run on the client side when the browser parses the HTML, CSS and Javascript that the server sent.
You will have to make a new HTTP request in some way, sending the new data. You can do that by reloading the entire web page and sending stuff in the querystring (after ? in the URL), or slightly more advanced by doing an Ajax call from your JS code and make JS do the changes of the page that you desire. The latter is more complex and requires some knowledge of Javascript, but the page does not need to be reloaded in its entirety.

Did you mean something like this?
<script>
var foo_bar = {$foo.bar|escape:javascript};
</script>
Note that, as mentioned above, the value is computed server-side.
UPD. I get it now, you wanted to pass value the other way around. No, that’s not possible.

Related

passing variable javascript into php [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I want to pass a variable javascript into variable php. I can't use $_GET or $_POST because is all loaded by the method load in jquery and I don't have any form or possbility to implement it.
This is my code:
<script type="text/javascript">
var id= "test";
</script>
<?php $_SESSION['my_page'] =?> + id;
How can I resolve It?
2 solutions
you use AJAX to send your value, see jQuery.
you set that session variable from a submitted form as a value and process it on your script.
Eighter way you CANNOT mix javascript with PHP like that. Javascript is client side, PHP is server side.
for jQuery:
<script type="text/javascript">
var id = "test";
$.get('yourScript.php?id='+id);
</script>
and in yourScript.php
<?php
session_start();
$_SESSION['my_page'] = (int)$_GET['id'];
?>
The truth is you can't really pass them. Javascript is client-side meaning it happens on each individual computer. PHP is server-side so it happens on the server. You would have to somehow pass that variable from Javascript into the server and you could do that AJAX.
Another idea to consider is using the standard way of passing variables into different PHP documents: forms. You can make a form and use javascript to insert what you want to be submitted into the PHP server and from there you can use the $_GET or $_POST. Hope this helps.
Consider using AJAX. Here is a link to some tutorials although if you are using JQuery, it can handle most of the coding for you.
http://www.w3schools.com/ajax/default.asp
From JavaScript, you can call the PHP script after the page has loaded asynchronously (meaning that it will open a connection in the background rather than changing the page). Once the PHP script has finished executing, it will return a status code that you can check for through the JavaScript. You can access the data resulting from the PHP scripts execution in the JavaScript.
If you are coding a mobile website, be aware that not all mobile phones support AJAX.
EDIT: If you would rather use JQuery, then here is a link to a good tutorial http://www.sitepoint.com/ajax-jquery/

passing javascript value to php function [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to pass JS variable to php?
pass a js variable to a php variable
I'm asking if it is possible passing value obtained through javascript function in a php function in the same file.for instance: I have a javascript function that calculate my position and pass this value to php function.is it possible?
[Edit]
Well I think I should be more specific...What I want to do specifically is to calculate my position through geolocation library of google API and return the value of latitude and longitude to a php variable in the way I can use it inside my php page. BUT this page it is not a normal php page it is a codeigniter Controller since I'm using MVC model to handle my project
Yes it is, try to look into AJAX. With help from a library as jQuery and it's ajax methods should get you going.
To call back to a server-side script from the page, you can use an XMLHttpRequest (or a suitable wrapper such as jQuery).
simpler way would be to create hidden form field and set its value in js and get it through php.
Yes, there are two ways
send the data directly using an ajax request, which doesn't require the whole page to be submitted
use javascript to write the value to a hidden form element/cookie which will then get passed to your php script (either in $_POST or $_COOKIE) when you request the page again
No, php is a server-side language as opposed to javascript, which is a client-side language.
However, you can send an AJAX response with the position variable to a PHP script (try using the jQuery post function: http://api.jquery.com/jQuery.post/)

Is it possible to use JavaScript's variable in PHP? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Access a JavaScript variable from PHP
theVariable = String.fromCharCode(theVariable).toLowerCase();
is it possible to use theVariable in PHP codes ? How?
Not really... you can send it to a PHP script using AJAX or an HTML form. I strongly recommend you get a clearer understanding of what JavaScript is vs. PHP, particularly what it means that JavaScript runs on the client-side (i.e. in the browser), and PHP runs server-side.
Only if the the variable is sent via ajax to a PHP page. You see PHP is executed first, then the page is served to the client. Then the client executes the javascript. So the javascript is executed long after the PHP has been executed.
To use a variable that is in JavaScript in PHP, you have to send an Ajax request to a PHP page passing that variable in. This probably isn't the effect you're looking for.
JavaScript executes on the viewer's computer. PHP executes on the server. To send a JavaScript variable to PHP, you need to send some packet of information from the client to the server. This is done as a GET/POST request.
The only way to pass a variable from JavaScript to PHP is to pass it by an AJAX call.

using anchors #value in URL, can I call and use this value in php? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can PHP read the hash portion of the URL?
Hi,
I want to go to a part of my web page where a div called bla bla bla is located.
Using this: http://www.mysite.com/mypage#28 I get there.
But I also need that number to process in php. Does the # work like a ? ($_get) as well?
How do I do that otherwise?
Thanks
I agree with middaparka, Hash values is a client side values can not be loaded from server side, as Facebook Dynamic Url Loading technique
so you can read it is value from a function, and call that function onload of page to do what you need.
In theory you can use the parse_url function to obtain this, via the PHP_URL_FRAGMENT option. However, in practice I'm pretty sure that there's no guarantee that the browser will pass this information to the server. (i.e.: It won't show up in $_SERVER['REQUEST_URI'], so there's no way to pass this information into parse_url in the first place.)
As such, you'd need to obtain this client-side via JavaScript and forward it to the server. To do this you can use window.location.hash.
e.g.: <script>alert('The hash value is: '+window.location.hash);</script>
There is JQuery Plugin "hashchange" using that you can send Ajax request when the hash is changed everytime. You can even bookmark it.
http://benalman.com/projects/jquery-hashchange-plugin/
Check out the demo at http://benalman.com/code/projects/jquery-hashchange/examples/hashchange/

How do I assign javascript variable to php variable? With out submitting the form

hi friends can some one tell me How do I assign javascript variable to php variable?
I am actually getting a value through javascript and I want the javascript values to be assigned to php variables. With out submitting the form.
Unless you are doing something really esoteric (such as running server side JS and PHP):
The PHP runs, generates a response, sends it to the browser. If that response includes JavaScript then that JS will then be executed.
You can't get data from the JS back to the PHP without issuing a new HTTP request.
This could be done by setting document.location, adding an <img> element to the document with the data passed via the src attribute (in both cases including the data in a query string, posting a <form> to an <iframe>, using an XMLHttpRequest object and a host of other methods.
It really depends on what you want to achieve.
You don't, directly.
You have to use AJAX, but beware that is far more complicated than "just assigning it".
Use the jQuery selector to do that. OR use a hidden form if you don't want the user to see it.
For example, do this.
<input type="hidden" name="YOUR_OWN_UNIQUE_NAME" id="UNIQUE_ID" value >
After that, just use jQuery selector
$("#UNIQUE_ID").val("VALUE")
That's about it, not much problems!
You need to send an AJAX request containing the information to a separate PHP script that processes the data.
You can assign PHP variable to a JavaScript one:
var some = <?php echo $other; ?>
But you can't do this backwards - it is a matter of that php is run BEFORE javascript. ;]
Best way will be to send the javascript variable value to the server asynchronously before the form is submitted using AJAX, but be aware that you will only be able to bring back a variable or store the data server side via a db or text file.
what do you want to do with the data that is sent to the server?

Categories