This question already has answers here:
Send PHP variable to JavaScript function [duplicate]
(6 answers)
Closed 6 years ago.
I have js code which is inside echo statement (which is retried using ajax call).
I need to send another ajax request based on the first result. So I need to assign a php variable inside a js variable (php variable is retrieved from database during first ajax call)
echo '<script>
echo '<script>// SEINDINGM REQUEST FOR SELECTED DAY
$("#dayName").on("change", function(){
// GRABBING THE SELECTED VALUE
var day = $(this).val();
var uid=//Here I want to assign the php variable //;
alert(uid);
alert(day);
</script>';
replace
var uid=//Here I want to assign the php variable //;
with
var uid='.$myVariable.';
PHP doesn't care if this value will end up in JavaScript code. It just writes the content wherever you want it to be. The document will arrive at the client as usual HTML including the already inserted variable content. Then it will be normally executed by JavaScript.
If the variable's content is a String then you have to write it like:
var uid="'.$myVariable.'";
Here are more information and different options how to include variable content within echo php - insert a variable in an echo string
echo '<script>
$("#dayName").on("change", function(){
// GRABBING THE SELECTED VALUE
var day = $(this).val();
var uid='.$myVariable.'
alert(uid);
alert(day);
</script>';
Related
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 1 year ago.
I am new in PHP and javascript and need help with the following code
I want to get the value from browser URL that tag with "org" and use it in javascript; as example "http://localhost/nmt/index.php?org=table1" I want to put the "org" value inside the URL/ inside the POST $.post("ch1.php?org=table1", instead of table1 put the GET value
This is the part from code
$(document).ready(function () { showGraph1(); }); function showGraph1() { { $.post("ch1.php?org=table1", function (data) { console.log(data);
That If possible replace table1 with the value from URL
You can inject your variable into the javascript.
<script>
var data = <?php echo json_encode($_GET["org"]); ?>;
</script>
You also can get the url parameter directly from js.
This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 5 years ago.
I'm trying to concatenate a link in a javascript function with a php variable. But whatever a try to write after ""+ doesn't get displayed and the whole program stops working. The variable is defined in a different page. I tried REQUEST to access to it, but it's not working.
function popupEvent(){
$('#btnClose').unbind("click");
$('#btnClose').click(function(){
window.location = SITEURL+"cart/rollPdf/?POrderNo="+implode(',', $orderIDArr);
window.open(ord,'winname','directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=350');
return false
});
}
Whenever you want to use PHP variable into JS, you need echo it. Ex, <?php echo implode(',', $orderIDArr);?>
In your code try Like this,
function popupEvent(){
$('#btnClose').unbind("click");
$('#btnClose').click(function(){
window.location = SITEURL+"cart/rollPdf/?POrderNo=<?php echo implode(',', $orderIDArr);?>"; // Changed here
window.open(ord,'winname','directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=350');
return false
});
}
Hope this will help you to solve your problem...
EDIT:
function popupEvent(){
$('#btnClose').unbind("click");
$('#btnClose').click(function(){
ord = SITEURL+"cart/rollPdf/?POrderNo=<?php echo implode(',', $orderIDArr);?>"; // Changed here
window.open(ord,'winname','directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=350');
return false
});
}
This question already has answers here:
javascript variable into php
(3 answers)
Closed 8 years ago.
i have php code like this
<?php
$queryitemkits="select item_id, cost_price, unit_price from items where item_id='JAVASCRIPTVARIABLE'";
$resultqueryitemkit = mysql_query($queryitemkits) or die('Query failed item kits!');
while($rowitem = mysql_fetch_assoc($resultqueryitemkit))
{
$itemcostprice=$rowitem['cost_price'];
$itemunitprice=$rowitem['unit_price'];
}
?>
and i have javascript variable named ui.item.value
how to insert that ui.item.value javascript variable to replace JAVASCRIPTVARIABLE in above PHP code?
Remember PHP is on the server, and the variable "JAVASCRIPTVARIABLE" in the client, you need to submit the variable to the server, also you need input validation, JAVASCRIPTVARIABLE it can be empty when is received on the server.
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 4 months ago.
I have a PHP file that creates a randomized string stored in a variable. I want to access this variable with JavaScript but the methods I've found online doesn't seem to be working. This is the code I have so far:
var test = "<?php echo json_encode($myVariable); ?>";
alert(test);
$myVariable is just a string: "testing".
I thought "testing" would be alerted but instead the code itself is (<?php echo json_encode($myVariable); ?>). When I take away the quotations, nothing happens. I'm very new to JavaScript so I'm not sure what's wrong. Is there another way I can access a PHP variable with JavaScript?
you can transfer variable contents by this
var test = "<?php echo $myVariable; ?>";
alert(test);
There are few way of accessing variables from php with javascript AJAX/Cookies/_SESSION or echo directly to javascript.
AJAX
Is more readable with better separations between layers it also allows for async transfer. BUT it can be very slow adding HTTPrequest can lead the website to slow down alot if there is alot of variable to request from php.
Cookies
I dont recommend it as there will be alot of necessary data store on the client side.
_SESSION
I recommend you using _SESSION as new developer will find it easier to use. it works similar to cookies but the only difference is once the page is closed the data is erase.
Echo directly to javascript
Easy to implement but horrible code practise.
All the above answers I explain quite vague do your research on topics, look for _SESSION to start with then move on to AJAX if appropriate.
Code example
PHP
if you are getting data from mysql
$_SESSION["AnyVariable"] = my_sql_query ("SELECT someData FROM someTable")
Or if its just a variable in PHP
$SESSION["aVariable"] = whateverValue;
JavaScript
<script type="text/javascript">
function someFunction()
{
var someVariable = '<%= Session["phpVariableSessionName"] %>';
alert(someVariable);
}
</script>
json_encode produces output ready for javascript, you must not put it into quotes:
var test = <?php echo json_encode($myVariable); ?>;
Try it the other way around ...
<?php echo "var test = '" . json_encode($myVariable) . "';"; ?>
This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 8 years ago.
I am trying to use a php variable in my javascript code, but i cant seem to get it working. Here is my js and php code:
<?php $s = "woo"; ?>
var images = <?=$s?>;
What i want to achieve, is a php variable in javascript ENCLOSED with ['']; With other words, so that the javascript code reads it like this: ['woo'];
I could really need some help, as i am very new to javascript. Thanks in advance.
I recommend using json_encode for all values "passed" to JavaScript - this will prevent against injection, accidental or otherwise. It also trivially handles quotes and allows complex object graphs to be supplied. If not already, I imagine that images is really, or should be, an array ..
var images = <?= json_encode($s) ?>;
Or
var images = <?php echo json_encode($s); php?>;
Look at the actual HTML to see what is being emitted, and that it is valid - the original yields JavaScript akin to var images = woo;, which will result in a ReferenceError (on woo).
One trick you can use is this, when you are inside a .js file it can be helpful, because php doesnt work.
Put the value of your php value inside a div as content, and then grab that in the javascript.
in the php file:
<div id="sVar" style="display:none;"><?php echo $s; ?></div>
in js:
var images = document.getElementById('sVar').innerHTML;