This question already has an answer here:
Pass Javascript var into PHP var
(1 answer)
Closed 9 years ago.
Is this how you would pass a var from JS to PHP? or is there a better solution. Please could you help with the easiest and most efficient way.
<!DOCTYPE html>
<html>
<body>
<script>
var name="John Doe";
document.write(name + "<br>");
</script>
<?php
$name = <script>document.write(name);</script>
?>
</body>
</html>
I know this won't work, but how would I do it?
<?php
$name = <script>document.write(name);</script>
?>
You can not do this
As php is a server side language and js client side,
First it run in server then it output to client were it executes js. So you can not pass values from js to php like in the code above.
If you want to do that you need to use AJAX.
please try this:
<?php
echo "<script language=\"javascript\">
var name=\"John Doe\";
document.write(name + \"<br>\");
</script>";
?>
i hope this would be useful
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
i have this php and js code:
<script type="text/javascript">
function StartScript(script)
{
<?php require("StartScript.php"); ?>
}
</script>
Start
How can i get the "script" from the javascript function onto the end of the php file?
for example rather than:
<?php require("StartScript.php"); ?>
to have:
<?php require("StartScript.php?script=scriptname"); ?>
The full excerpt (provided that StartScript.php at the same level of current page, otherwise add absolute path):
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function StartScript(script) {
jQuery.getScript('StartScript.php?script=' + escape(script));
}
</script>
This will call StartScript.php with the parameter script. StartScript.php will generate the JavaScript code which will be executed in the client browser.
Reference: http://api.jquery.com/jQuery.getScript/
It's impossible to write server-side code (PHP) with a client-side code (Javascript). This will not work unless you use something like Ajax.
Maybe I misunderstood your question but this seems pretty easy...I would include all the js in the php file so the html side of things would have only....
<?
$script = "Page1";
include("StartScript.php");
?>
and the php would be...
<? if ($script == "Page1"){ ?>
function StartScript(script)
{
alert("IN");
}
</script>
<? } ?>
The problem is that you're mixing up the client (JS) and the server (PHP). PHP is executed on the server and produces some HTML which gets sent to the browser ("the client"). Browsers cannot run PHP because it's a server-side language.
You should evaluate what the desired interaction is between the client and the server here. For example, if you just need to execute some PHP to pass data to JavaScript, you can build up a JavaScript object:
<script type="text/javascript">
<?php // include script that gives you back some data, e.g.: ?>
<?php $somePhpData = array('red', 'yellow', 'blue'); ?>
<?php $jsonData = json_encode($somePhpData); ?>
var dataFromPhpScript = <?php echo $jsonData; ?>
// do something with the data
</script>
Otherwise, if you really need JavaScript to trigger a PHP script running, you're essentially doing AJAX. You'll likely want some sort of REST API. The idea is that you expose a URL from PHP that the JavaScript can call:
<script type="text/javascript">
jQuery(document).ready(function() {
function startScript(scriptName, success) {
$.get('/path/StartScript.php?script-name=' + scriptName)
.done(success)
.fail(fail);
}
startScript('name-of-php-script', function(data) {
// trigger some JavaScript that relies on the output of the PHP script
});
});
</script>
This question already has answers here:
passing PHP objects to javascript [duplicate]
(4 answers)
Closed 9 years ago.
I need to change the value of a PHP variable to JavaScript code.
The value of the PHP variable has to be something out of (JavaScript) localStorage.
So it is something like this.
$variable = localStorage.anameofalocalstorage; //The localstorage part is js
How can I do this?
This is an example code using jquery for the ajax call.
<?php
$variable_that_should_be_set=isset($_GET['shoudBeSet'])?$_GET['shoudBeSet']:'notSet';
?>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<div id="div1"><?php echo $variable_that_should_be_set;?></div>
Click here to Set
<script>
$("#button").click( function () {
$.get("?shouldBeSet=this_is_set")
})
</script>
</body>
</html>
This question already has answers here:
How to get JavaScript variable value in PHP
(10 answers)
Closed 9 years ago.
I just want to know if sending value of a javascript to php is possible.
here is the simple code of what i am trying to say.
<script language="javascript" >
var id = "data"
</script>
<?php
$getthevalueofid = var id;
?>
thanks!
Not the way your code sample is structured, no. PHP is a server-side language and Javascript is a client-side language. PHP is out of scope by the time any client-side script executes.
If you need to pass an object from the browser to your server you can use XMLHttpRequest (commonly referred to as AJAX).
To send a Javascript variable back to PHP, you need to do an AJAX request:
<script language="javascript" >
xmlhttp.open("GET","getvalue.php?id="+id,true);
xmlhttp.send();
</script>
And in getvalue.php you'd have:
<?php
$getthevalueofid = $_GET['id'];
?>
No way to do this. Use Ajax to pass javascript variable to php
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I'm tring to pass a session variable to javascript but not have success.
Searching stackoverflow i have found this discussion:
Passing Session variables to javascript
in my custom.js i have on header
<?php session_start(); ?>
.. /// js code
strActionPage = CurrentPath + "upload_file.php?ation=store&session_user_id=<?php echo $_SESSION['session_user_id']; ?> "; //the ActionPage's file path
but i cant get session variable from upload_file.php page
where do I wrong?
Tks to all
Wait, you're using PHP on a JavaScript file (that's what your OP says)? Not gonna work. You're going to have to pass it to a JavaScript function as a parameter like so:
<?php
session_start();
// HTML and stuff
<script type="text/javascript" src="custom.js"></script>
<script type="text/javascript">
passSession("<? echo $_SESSION['session_user_id']; ?>");
</script>
It's hard to say without knowing more about your particular error, but I did notice what seems like a typo in the code you posted:
strActionPage = CurrentPath + "upload_file.php?ation=store&session_user_id=<?php echo $_SESSION['session_user_id']; ?> "; //the ActionPage's file path
You have ation-store -- did you mean to write action=store?
This is how I might do.
<script type="text/javascript">
var user_id=<?php echo $_SESSION['session_user_id']; ?>;
strActionPage = CurrentPath + "upload_file.php?action=store&session_user_id="+user_id;
</script>
There is also a possible mistake. You are using "ation" . Should that be 'action'?
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I got a html5 upload script from
http://tutorialzine.com/2011/09/html5-file-upload-jquery-php/
can i pass php variable to js?
in this html5 upload script
the script.js call the post_file.php to upload file
in post_file.php
$rand = time();
i set the rand is the filename
for example uploaded filename: 1331956640.jpg
can i pass this $rand to script.js?
because i can't print the result in php, only can print something in script.js
this is the html5 upload script download link from tutorialzine
enter link description here
sorry my english not good, thank
<script>
var my_javascript_var = <?php echo $rand; ?>
</script>
or
<input id="my_rand_value" type="hidden" value="<?php echo $rand;?>" />
in js do so
var my_javascript_var = $("#my_rand_value").val():
It's not actually about "passing" a variable from PHP to JavaScript.
Remember that PHP is a server-side scripting language, and JavaScript resides on a client's browser.
So, you could actually... write directly any javascript you wish from your PHP script.
Let's say, you've got a $a variable... then you could simply enter it in your javascript code like this :
<script type='text/javascript'>
var a = <?php echo $a; ?>
</script>
However :
If what you mean is to actually use the $a var while the page has loaded, or retrieve the result in some way, WITHOUT reloading, then what you probably need is Ajax.
To use AJAX, I would either suggest :
the jQuery load method
using some ready-made AJAX object
You can do this
<script>
var javascriptvar = <?=$rand ?>
</script>
You dont need to ....
<script>
var rand = new Date().getTime();
</script>
this uses only JavaScript to get the same result
You can do it this way:
<script language="javascript" type="text/javascript" src="your.js"></script>
...
<img src="your_button.gif" />