Pass a variable from a javascript function to another php file [duplicate] - javascript

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;
?>

Related

How can one call a PHP function from JavaScript? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
I have a piece of code, and I need to call a PHP code snippet when this function is called. Basically, it would be like this:
function jsFunc(){
//do PHP stuff
}
I don't know if this is a trivial thing, and I'm just not getting it, but I would appreciate any help.
If you want to do that in a .js file you can organize with Ajax calls
Here great example https://www.w3schools.com/js/js_ajax_php.asp
or if you use jQuery library
https://www.w3schools.com/jquery/jquery_ajax_load.asp
https://www.w3schools.com/jquery/jquery_ajax_get_post.asp
Do the following:
function jsFunc(){
var php_code = <?php
// your php code
?>;
}
EDIT: To call an object within the php code, use
echo $php_object
as suggested by noobcode.
Run it like:-
function jsFunc(){
//do PHP stuff
$pp = <?php echo "hello"; ?>;
console.log($pp);
}
Now you can easily use your function where i have used echo.

PHP - Javascript integration? [duplicate]

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>
?>

use Js Variable in php [duplicate]

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....

How to use PHP in Javascript functions [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
Can I please have some help in using a PHP variable in some Javascript code.
i want to use php code in javascript function how to use php scripts in javascript function
for example i want to use php code in this javascript function
function rank()
{
<?php echo'hello';?>
}
Try using like this
function rank()
{
var someVariable = "<?php echo 'hello';?>";
}
try this (better & shorter)
function rank()
{
var variable="<?='hello';?>"
}
You can use like this:
function rank()
{
var rankVar;
rankVar = "<?php echo'hello'?>";
}

Injecting PHP data into JavaScript code [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
What's the preferred way of outputting PHP code within apostrophes? For example this JavaScript code would break and the second line will be totally empty:
var jsString = '<div id="content">'+'<?php echo $something['1'] ?>'+'</div>';
Thanks! I know this is kind of basic and I know how to fix it, but I'm wondering how to do it the right way?
[edit]
I know it looks obvious, but $something["1"] won't work. The same goes for $something[\'1\'].
This example is working well for me :
<?php
$something['1']='hhhh0';
?>
<script type='text/javascript'>
var jsString = '<div id="content">'+'<?php echo $something['1'] ;?>'+'</div>';
alert(jsString);
</script>

Categories

Resources