This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 6 years ago.
I have the php page with javascript code inside it.
If I have php variable retrieved by back-end server, can it be passed in javascript code?
For instance, if I want to check the flag(php variable) inside the javascript code, do I need to hardcode it inside the javascript?
Below are the "checkServiceCategory"? (Should it be Dynamic or static)
<?php
foreach ($result as $id=>$value) {
if($value[0]->isServiceCategory)
return true;
}
OR
isEmptyUploadFile(function(r)
{
var checkServiceCategory=document.getElementById('category-group').value;
if(checkServiceCategory!=85 && (fileList == null || fileList.length == 0))
{
$("#uploadImgError").html('<em><span style="color:red"> <i class="icon-cancel-1 fa"></i> Please Upload at least one image!</span></em>');
location.href = "#uploadImgError";
return false;
}
else
Yes you can use php variables inside the javascript code and php variable is dynamic as
<?php $your_variable = "some value"; ?>
<script>
var js_variable = '<?php echo $your_variable; ?>';
alert(js_variable);
</script>
You can write PHP data into JavaScript before that JavaScript is loaded. So, for example:
<script>
var count = 0;
<?php
echo 'count = 5;';
?>
console.log(count); // Will give 5
</script>
You can do this because the PHP is run on the page before it is ever sent to the client. However, once the code is sent to the client, JavaScript and PHP cannot talk to each other. What you can do at that point is make AJAX requests via JavaScript from your client back to your server, and use PHP to respond to the AJAX request.
You can pass value from php to js like this.
var js_var = '<?php echo $some_php_variable; ?>';
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
I have this code:
<html>
<body>
<script>
var x;
if (confirm("Press a button!") == true) {
x = "You pressed OK!";
<?php $kk="ok"; ?>
} else {
x = "You pressed Cancel!";
<?php $kk="not ok"; ?>
}
document.getElementById("demo").innerHTML = x;
</script>
<?php
echo $kk;
?>
</body>
</html>
When I echo $kk, I obtain always not ok
But I want to print OK or NOT OK. Any helps please?
What you are asking for is absolutely impossible as is, because all PHP code is completely executed before the html is sent to the browser, where the javascript is executed in its turn, hence after.
If you need the result on the http server, you must describe a form in the html page and create a server PHP script at the URL of the form action, then the PHP can get the form elements.
If you need the result in the browser (eg. to change some elements in the page displayed), you must code in javascript.
If you want to exchange between PHP and javascript while staying in the same page, you can use Ajax, or you can use directly the javascript XMLHttpRequest object which can call a server script from the browser javascript. See some examples at w3schools.com
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
I am facing issue regarding passing javascript variables to php within same function. My code looks like this
else if(msg_type[i] == 'code' ){
var code_action = 'test';
<?php
function foobar_func(){
return "<script>document.writeln(action[i]);</script>";
}
add_shortcode( 'foobar', 'foobar_func' );
?>
}
what i am doing is passing that code_action variable of javascript and returning it to function without using ajax or jquery...is there any possible method to do so...??
any possibilities will be appreciated. Thank you
When you make a request to invoke this page, your <?PHP ?> block is executed in the server. It creates a processed output in the web page (in your code) and send it to the browser. Now the browser executes your <script> </script> blocks.
I hope you understand that passing variables from <script></script> to <?PHP ?> is impossible since the latter happened in the past.
But you can use it other way around by trying to pass variables from the PHP to JS.
When you write
<?PHP
$fromServer = "from php";
echo "<script> var fromServer = " . $fromServer . " </script>
?>
.. it passes data from your PHP to JS.
You can, however, use HTTP POST or GET method to pass data from JS to PHP to be used in the next session.
This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 8 years ago.
when I click the detail link the value of $record[id] has been sent to showUser the value store in variable e in javascript I need this value of e in php how can I get the value of e in php.
echo
"<td>"."
<a href='#?value=$record[id]' onClick='showUser($record[id])'; >Detail</a>
"."</td>";
<script>
function showUser(str) {
var e=str;
}
</script>
Try this
echo "<td>"."<a href='#?value=".$record[id]."' onClick='showUser(".$record[id])."'; >Detail</a> "."</td>";
That should get the variable inside the showUser onClick function.
Javascript variable can hold php varaible's value since js loads on client
var a = <?php echo $vari;?>
but php variable cant hold JavaScript(unless you are not sending with ajax to server) as php loads first and manipulated on server not on client
In your case you can pass $record[id]) in onClick function
"<td>"."<a href='#?value=$record[id]' onClick='showUser(".$record[id])."';
You cannot pass javascript variable value to the php variables on the same page, as PHP code runs at the server side and it knows nothing what is going on client side.
You should pass variables to PHP code from html-form using some other way, such as Ajax.
Ajax is the best way to do it.
Thanks....
you can pass it through an ajax call in this way:
- step 1:
$.get('name_of_php_file_you_want_to_passvalue.php?e='+e,function(data) {
//you can do anything here with the php returned value.
})
- step 2:
And then in the php file to get that variable:
$e=$_GET['e'];
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I have a config file in PHP.
return array(
'window_width' => 1000,
'window_height' => 800
//etc
);
How can I read these vars into my javascript? Should I echo them to the page, in perhaps a data attribute and pick them up from there? Or is there a better way?
<script>
var settings = <?php echo json_encode($your_array); ?>;
alert(settings.window_width);
</script>
Echo them directly where you need them in javascript. You can echo a javascript object to reflect the data structure. Something like this should work:
var x = <?php echo '{window_width:"1000", window_height:"800"}'; ?>;
No. You should echo them to the page.
PHP runs on server, but JavaScript on client machine, browser. In PHP you should echo something like this:
<?php
echo('<script>var myvar = '.$myvar.';</script>');
?>
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
I've been trying to find out the response for this because I think it's not difficult to do, but I cannot find it.
I basically want to "recall" a mysql query that works. I have an HTML button and a span so I call a function in javascript which has a php code with the Mysql query and the result that is store in my span, works fine, but... Why I cannot click the button again to get the update results from Mysql table if I changed it ?
The syntax is something like ( in a single php file for test):
<script type="text/javascript">
function test(){
<?php include 'dbconnect.php';
$query = 'SELECT * FROM data';
$results = mysql_query($query);
while ($row = mysql_fetch_assoc($results)) {
$data = $data . "row[name]<br>";
}
mysql_close();
?>
testspan.innerHTML = <?php echo $data ?>';
}
</script>
<HTML><BODY>
<input type='button' onclick='test();'/><br>
<span id="testspan"></span
</BODY></HTML>
Resuming: when I click the first time the data appears but not the second time onwards so the Mysql query can be call just first time? anything related with the variables ?
Php is ServerSide... JS is ClientSide... you can't do code php in code JS you must use AJAX :https://api.jquery.com/jQuery.ajax/