This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
i want to use javascript varible in php..i refered much stackoverflow link and google but i didn't get proper solution..i also know js is client side scripting language and php is serverside..but i want js varible in php and i had also try with ajax call but i want set time zone in this file not other file so ajax call is not working in my situation.any one help me how to get same result for varible in echo and var_dump...
<html>
<script type='text/javascript'>
var timezone = jstz.determine();
var timezone=timezone.name();
console.log(timezone);
// it print 'Asia/Kolkata';
<?php $abc = "<script type='text/javascript'>document.write(timezone)</script>"?>
</script>
<?php
echo $abc;
// it print 'Asia/Kolkata';
var_dump($abc);
// it print string '<script type='text/javascript'>document.write(timezone)</script>' (length=65)
?>
</html>
any help must be appreciated..Thnks
you can use php variables to javascript but not javascript variables to php.. because javascript executes on client side and php executes on server side.. so php is executed before javascript gets executed..
So simply you cant...
the only thing you can do this by using ajax....
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 2 years ago.
1. <script>
2. document.getElementById('description').innerHTML = description;
3. </script>
4. <div id="description"></div>
How to get the value of line 4 into PHP code?. Also when I use the traditional way
<?php $weather = <div id = "description"></div>?>
The weather variable is not displayed wherever I want it to be displayed.
Any help is greatly appreciated. Thank You.
The PHP is processed on the server side, while the Javascript value is processed in the user's browser. So when the page is sent, there is no value there yet.
You can do this:
<?php
$weather = '<div id="description">Details go here</div>'; // define variable
echo $weather; // include / print variable
?>
But if the value does not exist yet that you want to include, then you need to use AJAX to send it to PHP if that's indeed what you need. Although if it doesn't need to go back to the server, it can likely be done just with JavaScript on the client side.
I suggest finding some good books on learning PHP / learning JavasScript / learning HTML.
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
Suppose you are in a php file and you want to run a javascript function that has both javascript and php inside of it. How would you go about doing this?
Ex:
<?php
<script>
function myFunction(){
// php code
document.write("Hello World!");
}
</script>
?>
I know I can easily make a php function for this particular code, but just for future reference, I would like to know how to do this.
I apologize if this has been answered before but it seemed difficult to word in google search.
The PHP code has to output text that is valid inside the javascript. Something like this.
<?php
<script>
function myFunction(){
// php code
var message = "<?php echo "My alert message"; ?>";
window.alert(message);
}
</script>
?>
This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 8 years ago.
<script>
myjsvar= $('#project_start_date').val(); //result is 10-11-2014
</script>
<?php
$myphpvar = //how to get value of myjsvar into php variable?
?>
how to get value of myjsvar into php variable $myphpvar?
thanks
You can't use javascript variable in php code. Php code run's on the serverside and javascript code runs in the client side. You can't ask the browser to run php code.
Your variable loc will have a value only when the code reaches the browser.
If you want to get some value from server and combine it with javascript variables then do the following.
Use an ajax request and send the desired values to server. The server will return with a response. Use that response text and store it in your action variable.
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
i am new to javascript and php and i need your help. I have a javascript function inside a php file. In this javascript function i have a variable that i want to pass it to another php file. Can anyone help me how to do this.
Thanks
There are several ways you can do this. Probably the simplest way is:
var myData = 'whatever';
window.location.href = 'otherscript.php?myData=' + myData;
Then your script 'otherscript.php' can access that variable with $_GET['myData'].
This method requires a page refresh. You can do it without refreshing the page by using ajax, but that is a little bit trickier.
Something like that should work:
<script type="text/javascript">
function yourFunction(yourParameter) {
<?php $abc = "<script>document.write(yourParameter)</script>"?>
}
</script>
<?php
echo $abc;
?>
This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 8 years ago.
I would like to echo some php code in javascript for certain reasons but the code does not get executed, when i use the code inspector i notice that the php code has been commented out.
This is the code:
var code = '<?php echo time_passed(strtotime(' + new Date().toLocaleString() + ')); ?>';
$('#page').html(code);
basically i am passing the javascript datetime value to a php function which should then echo the results into the code variable.
I then output the value of code to a container with an id of page.
PHP is on the server end. In the manner your using, there is no way to for PHP and JavaScript code execution to interact. You would need to user AJAX to communicate between front end execution (JavaScript) and back end execution (PHP).